@mieweb/wikigdrive
Version:
Google Drive to MarkDown synchronization
123 lines (122 loc) • 4.89 kB
JavaScript
import { Container } from '../../ContainerEngine.js';
import { QuotaLimiter } from '../../google/QuotaLimiter.js';
import { GoogleDriveService } from '../../google/GoogleDriveService.js';
import { UserAuthClient, ServiceAuthClient, getCliCode } from '../../google/AuthClient.js';
import { AuthError } from '../server/auth.js';
const __filename = globalThis[Symbol.for("import-meta-ponyfill-esmodule")](import.meta).filename;
export class GoogleApiContainer extends Container {
constructor(params, authConfig) {
super(params);
Object.defineProperty(this, "params", {
enumerable: true,
configurable: true,
writable: true,
value: params
});
Object.defineProperty(this, "authConfig", {
enumerable: true,
configurable: true,
writable: true,
value: authConfig
});
Object.defineProperty(this, "logger", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "oldSave", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "auth", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "quotaLimiter", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
async init(engine) {
await super.init(engine);
this.logger = engine.logger.child({ filename: __filename });
const initial_quota_jobs = await this.filesService.readJson('quota.json') || [];
this.quotaLimiter = new QuotaLimiter(initial_quota_jobs, this.logger);
this.quotaLimiter.setInitialLimit(95, 10); // 950, 100 doesn't work as expected
this.quotaLimiter.setSaveHandler(async (jobs) => {
const str = JSON.stringify(jobs);
if (this.oldSave === str) {
return;
}
await this.filesService.writeJson('quota.json', jobs);
this.oldSave = str;
});
if (this.authConfig.service_account) {
this.auth = new ServiceAuthClient(this.authConfig.service_account);
}
if (this.authConfig.user_account) {
const authClient = new UserAuthClient(this.authConfig.user_account.client_id, this.authConfig.user_account.client_secret);
const google_auth = await this.filesService.readJson('auth_token.json');
if (google_auth) {
authClient.setCredentials(google_auth);
}
else {
const code = await getCliCode(this.authConfig.user_account.client_id);
const google_auth = await authClient.authorizeResponseCode(code, 'urn:ietf:wg:oauth:2.0:oob');
await this.filesService.writeJson('auth_token.json', google_auth);
}
await authClient.authorizeUserAccount();
this.auth = authClient;
}
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async run() {
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async destroy() {
}
async listDrives() {
if (!this.auth) {
throw new AuthError('Not authenticated', 401);
}
const googleDriveService = new GoogleDriveService(this.logger, this.quotaLimiter);
const accessToken = await this.auth.getAccessToken();
return await googleDriveService.listDrives(accessToken);
}
async getDrive(driveId) {
if (!this.auth) {
throw new AuthError('Not authenticated', 401);
}
const googleDriveService = new GoogleDriveService(this.logger, this.quotaLimiter);
const accessToken = await this.auth.getAccessToken();
return await googleDriveService.getDrive(accessToken, driveId);
}
async getFolder(fileId) {
if (!this.auth) {
throw new AuthError('Not authenticated', 401);
}
const googleDriveService = new GoogleDriveService(this.logger, this.quotaLimiter);
return await googleDriveService.getFile(this.auth, fileId);
}
async shareDrive(driveId, shareEmail) {
if (!this.auth) {
throw new AuthError('Not authenticated', 401);
}
const googleDriveService = new GoogleDriveService(this.logger, this.quotaLimiter);
const accessToken = await this.auth.getAccessToken();
return await googleDriveService.shareDrive(accessToken, driveId, shareEmail);
}
getAuth() {
return this.auth;
}
getQuotaLimiter() {
return this.quotaLimiter;
}
}