@mieweb/wikigdrive
Version:
Google Drive to MarkDown synchronization
186 lines (185 loc) • 9.29 kB
JavaScript
import { INITIAL_RETRIES, QueueTask } from './QueueTask.js';
import { TaskFetchDiagram } from './TaskFetchDiagram.js';
import { TaskFetchDocument } from './TaskFetchDocument.js';
import { TaskFetchBinary } from './TaskFetchBinary.js';
import { TaskFetchAsset } from './TaskFetchAsset.js';
import { MimeTypes } from '../../model/GoogleFile.js';
import { StopWatch } from '../../utils/StopWatch.js';
export class TaskFetchFolder extends QueueTask {
constructor(logger, googleDriveService, auth, fileService, file, forceDownloadFilters = false, filters) {
super(logger);
Object.defineProperty(this, "logger", {
enumerable: true,
configurable: true,
writable: true,
value: logger
});
Object.defineProperty(this, "googleDriveService", {
enumerable: true,
configurable: true,
writable: true,
value: googleDriveService
});
Object.defineProperty(this, "auth", {
enumerable: true,
configurable: true,
writable: true,
value: auth
});
Object.defineProperty(this, "fileService", {
enumerable: true,
configurable: true,
writable: true,
value: fileService
});
Object.defineProperty(this, "file", {
enumerable: true,
configurable: true,
writable: true,
value: file
});
Object.defineProperty(this, "forceDownloadFilters", {
enumerable: true,
configurable: true,
writable: true,
value: forceDownloadFilters
});
Object.defineProperty(this, "filters", {
enumerable: true,
configurable: true,
writable: true,
value: filters
});
Object.defineProperty(this, "useGoogleMarkdowns", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
}
setUseGoogleMarkdowns(value) {
this.useGoogleMarkdowns = value;
}
async run() {
if (this.filters.filterFoldersIds.length > 0) {
if (this.filters.filterFoldersIds.indexOf(this.file.id) === -1) {
return [];
}
}
const stopWatch = new StopWatch();
if (this.retries < INITIAL_RETRIES) {
await new Promise(resolve => setTimeout(resolve, 1000));
this.logger.info('Listening (retry): ' + this.file.id);
}
else {
this.logger.info('Listening: ' + this.file.id);
}
// const rootFolderId = urlToFolderId(this.drive_config['drive']);
const tasks = [];
const file = await this.googleDriveService.getFile(this.auth, this.file.id);
if (file.mimeType === MimeTypes.FOLDER_MIME) {
const oldFiles = await this.fileService.readJson('.folder-files.json') || [];
await this.fileService.writeJson('.folder.json', file);
const files = await this.googleDriveService.listFiles(this.auth, { folderId: this.file.id });
await this.deleteUnused(files);
const filesToSave = [];
for (const file of files) {
const oldFile = oldFiles.find(oldFile => oldFile.id === file.id);
if (this.filters.filterFilesIds.length > 0) {
if (this.filters.filterFilesIds.indexOf(file.id) === -1 && this.filters.filterFoldersIds.indexOf(file.id) === -1) {
if (oldFile) {
filesToSave.push(oldFile);
}
continue;
}
}
filesToSave.push(file);
/*
const oldFile = oldFiles.find(oldFile => oldFile.id === file.id);
if (modifiedTime && oldFile.modifiedTime !== file.modifiedTime) {
const localFiles = await this.fileService.list();
for (const localFile of localFiles) {
if (localFile.startsWith(oldFile.id)) {
await this.fileService.remove(localFile);
}
}
}
*/
const forceDownload = this.forceDownloadFilters || oldFile?.modifiedTime !== file.modifiedTime;
switch (file.mimeType) {
case MimeTypes.FOLDER_MIME:
{
const task = new TaskFetchFolder(this.logger, this.googleDriveService, this.auth, await this.fileService.getSubFileService(file.id), file, this.forceDownloadFilters, this.filters);
task.setUseGoogleMarkdowns(this.useGoogleMarkdowns);
tasks.push(task);
}
break;
case MimeTypes.DRAWING_MIME:
tasks.push(new TaskFetchDiagram(this.logger, this.googleDriveService, this.auth, await this.fileService, file, forceDownload));
break;
case MimeTypes.DOCUMENT_MIME:
if (!this.useGoogleMarkdowns) {
tasks.push(new TaskFetchDocument(this.logger, this.googleDriveService, this.auth, await this.fileService, file, forceDownload));
}
else {
tasks.push(new TaskFetchBinary(this.logger, this.googleDriveService, this.auth, await this.fileService, file, forceDownload, MimeTypes.MARKDOWN, 'md'));
}
break;
case MimeTypes.SPREADSHEET_MIME:
tasks.push(new TaskFetchBinary(this.logger, this.googleDriveService, this.auth, await this.fileService, file, forceDownload, 'application/vnd.oasis.opendocument.spreadsheet', 'ods'));
tasks.push(new TaskFetchBinary(this.logger, this.googleDriveService, this.auth, await this.fileService, file, forceDownload, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xlsx'));
tasks.push(new TaskFetchBinary(this.logger, this.googleDriveService, this.auth, await this.fileService, file, forceDownload, 'text/csv', 'csv'));
break;
case MimeTypes.PRESENTATION_MIME:
tasks.push(new TaskFetchBinary(this.logger, this.googleDriveService, this.auth, await this.fileService, file, forceDownload, 'application/vnd.oasis.opendocument.presentation', 'odp'));
tasks.push(new TaskFetchBinary(this.logger, this.googleDriveService, this.auth, await this.fileService, file, forceDownload, 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'pptx'));
tasks.push(new TaskFetchBinary(this.logger, this.googleDriveService, this.auth, await this.fileService, file, forceDownload, 'application/pdf', 'pdf'));
break;
case MimeTypes.FORM_MIME:
tasks.push(new TaskFetchBinary(this.logger, this.googleDriveService, this.auth, await this.fileService, file, forceDownload, 'application/zip', 'zip'));
break;
case MimeTypes.APPS_SCRIPT:
tasks.push(new TaskFetchBinary(this.logger, this.googleDriveService, this.auth, await this.fileService, file, forceDownload, 'application/vnd.google-apps.script+json', 'gs'));
break;
case MimeTypes.SHORTCUT:
break;
default:
tasks.push(new TaskFetchAsset(this.logger, this.googleDriveService, this.auth, await this.fileService, file, forceDownload));
break;
}
}
await this.fileService.writeJson('.folder-files.json', filesToSave);
}
const timeString = stopWatch.toString(1000);
if (timeString) {
this.logger.info('Slow listening: ' + this.file.id + ' ' + timeString);
}
return tasks;
}
async deleteUnused(files) {
const localFiles = await this.fileService.list();
for (const localFile of localFiles) {
if (localFile === '.logs')
continue;
if (localFile === '.jobs.json')
continue;
if (localFile === '.changes.json')
continue;
if (localFile === '.private')
continue;
if (localFile === '.folder.json')
continue;
if (localFile === '.folder-files.json')
continue;
if (localFile === '.rendered_preview_time')
continue;
if (localFile === '.user_config.json')
continue;
const presentFile = files.find(file => localFile.startsWith(file.id));
if (presentFile) {
continue;
}
await this.fileService.remove(localFile);
}
}
}