@mieweb/wikigdrive
Version:
Google Drive to MarkDown synchronization
127 lines (126 loc) • 5.39 kB
JavaScript
import { Container } from '../../ContainerEngine.js';
import { GoogleDriveService } from '../../google/GoogleDriveService.js';
import { QueueDownloader } from './QueueDownloader.js';
import { TaskFetchFolder } from './TaskFetchFolder.js';
import { MimeTypes } from '../../model/GoogleFile.js';
import { GoogleTreeProcessor } from './GoogleTreeProcessor.js';
import { UserConfigService } from './UserConfigService.js';
const __filename = globalThis[Symbol.for("import-meta-ponyfill-esmodule")](import.meta).filename;
export class GoogleFolderContainer extends Container {
constructor(params, paramsArr = {}) {
super(params, paramsArr);
Object.defineProperty(this, "params", {
enumerable: true,
configurable: true,
writable: true,
value: params
});
Object.defineProperty(this, "paramsArr", {
enumerable: true,
configurable: true,
writable: true,
value: paramsArr
});
Object.defineProperty(this, "logger", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "googleDriveService", {
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, "filterFilesIds", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "forceDownloadFilters", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "progressNotifyCallback", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.filterFilesIds = paramsArr['filesIds'] || [];
this.forceDownloadFilters = false;
}
setForceDownloadFilters(value) {
this.forceDownloadFilters = value;
}
async init(engine) {
await super.init(engine);
this.logger = engine.logger.child({ filename: __filename, driveId: this.params.name, jobId: this.params.jobId });
const googleApiContainer = this.engine.getContainer('google_api');
this.googleDriveService = new GoogleDriveService(this.logger, googleApiContainer.getQuotaLimiter());
this.auth = googleApiContainer.getAuth();
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async destroy() {
}
async run() {
const downloader = new QueueDownloader(this.logger);
const filterFoldersIds = [];
if (this.filterFilesIds.length > 0) {
filterFoldersIds.push(this.params.folderId);
await this.buildFolderFilter(this.filterFilesIds, filterFoldersIds);
}
downloader.onProgressNotify(({ total, completed, failed }) => {
if (this.progressNotifyCallback) {
this.progressNotifyCallback({ total, completed, failed });
}
});
switch (this.params.cmd) {
case 'pull': {
const taskFetchFolder = new TaskFetchFolder(this.logger, this.googleDriveService, this.auth, this.filesService, { id: this.params.folderId, name: this.params.folderId, mimeType: MimeTypes.FOLDER_MIME }, this.forceDownloadFilters, { filterFilesIds: this.filterFilesIds, filterFoldersIds });
const folderId = this.params.name;
const googleFileSystem = await this.filesService.getSubFileService(folderId, '/');
const userConfigService = new UserConfigService(googleFileSystem);
await userConfigService.load();
taskFetchFolder.setUseGoogleMarkdowns(userConfigService.config.use_google_markdowns);
downloader.addTask(taskFetchFolder);
}
}
await downloader.finished();
const folderRegistryContainer = this.engine.getContainer('folder_registry');
const folderData = await this.filesService.readJson('.folder.json');
if (folderData?.name) {
await folderRegistryContainer.rename(this.params.folderId, folderData.name);
}
const treeProcessor = new GoogleTreeProcessor(this.filesService);
await treeProcessor.load();
await treeProcessor.regenerateTree();
await treeProcessor.save();
this.engine.emit(this.params.folderId, 'gdrive:changed', null);
}
async buildFolderFilter(filesIds, folderFilterIds) {
for (const fileId of filesIds) {
const file = await this.googleDriveService.getFile(this.auth, fileId);
if (!file.parentId || file.parentId === this.params.folderId) {
continue;
}
if (folderFilterIds.indexOf(file.parentId) === -1) {
folderFilterIds.push(file.parentId);
await this.buildFolderFilter([file.parentId], folderFilterIds);
}
}
}
onProgressNotify(callback) {
this.progressNotifyCallback = callback;
}
}