@mieweb/wikigdrive
Version:
Google Drive to MarkDown synchronization
154 lines (153 loc) • 7.53 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Controller, RouteGet, RouteResponse } from './Controller.js';
import { GitScanner } from '../../../git/GitScanner.js';
import { UserConfigService } from '../../google_folder/UserConfigService.js';
import { GoogleDriveService } from '../../../google/GoogleDriveService.js';
import { MarkdownTreeProcessor } from '../../transform/MarkdownTreeProcessor.js';
import { googleMimeToExt } from '../../transform/TaskLocalFileTransform.js';
import { GoogleTreeProcessor } from '../../google_folder/GoogleTreeProcessor.js';
import { getContentFileService } from '../../transform/utils.js';
import { redirError } from '../auth.js';
export class DriveController extends Controller {
constructor(subPath, filesService, folderRegistryContainer, authContainer) {
super(subPath);
Object.defineProperty(this, "filesService", {
enumerable: true,
configurable: true,
writable: true,
value: filesService
});
Object.defineProperty(this, "folderRegistryContainer", {
enumerable: true,
configurable: true,
writable: true,
value: folderRegistryContainer
});
Object.defineProperty(this, "authContainer", {
enumerable: true,
configurable: true,
writable: true,
value: authContainer
});
}
async getDrives(ctx) {
const user = await ctx.routeParamUser();
if (!user?.google_access_token) {
throw redirError(ctx.req, 'Not authenticated');
}
const folders = await this.folderRegistryContainer.getFolders();
const googleDriveService = new GoogleDriveService(ctx.logger, null);
const drives = await googleDriveService.listDrives(user.google_access_token);
return drives.map(drive => {
return {
id: drive.id,
folderId: drive.id,
name: drive.name,
exists: !!folders[drive.id]
};
});
}
async getDrive(ctx) {
const driveId = await ctx.routeParamPath('driveId');
const folders = await this.folderRegistryContainer.getFolders();
const drive = folders[driveId] || await this.folderRegistryContainer.registerFolder(driveId);
const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', '');
const googleFileSystem = await this.filesService.getSubFileService(driveId, '');
const userConfigService = new UserConfigService(googleFileSystem);
const userConfig = await userConfigService.load();
const gitScanner = new GitScanner(ctx.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com');
await gitScanner.initialize();
const contentFileService = await getContentFileService(transformedFileSystem, userConfigService);
const markdownTreeProcessor = new MarkdownTreeProcessor(contentFileService);
await markdownTreeProcessor.load();
const transformedTree = markdownTreeProcessor.getTree();
const tocFile = transformedTree.find(item => item.path === '/toc.md');
const navFile = transformedTree.find(item => item.path === '/.navigation.md' || item.path === '/navigation.md');
let tocFilePath = null;
let navFilePath = null;
if (userConfigService.config.transform_subdir && userConfigService.config.transform_subdir.length > 0) {
const contentPrefix = (!userConfigService.config.transform_subdir.startsWith('/') ? '/' : '')
+ userConfigService.config.transform_subdir;
tocFilePath = tocFile ? contentPrefix + tocFile.path : null;
navFilePath = navFile ? contentPrefix + navFile.path : null;
}
return {
...drive,
gitStats: await gitScanner.getStats(userConfig),
tocFilePath,
navFilePath
};
}
async downloadOdt(ctx) {
const driveId = await ctx.routeParamPath('driveId');
const fileId = await ctx.routeParamPath('fileId');
try {
const driveFileSystem = await this.filesService.getSubFileService(driveId, '');
const googleTreeProcessor = new GoogleTreeProcessor(driveFileSystem);
await googleTreeProcessor.load();
const [file, drivePath] = await googleTreeProcessor.findById(fileId);
if (file && drivePath) {
const odtPath = drivePath + '.odt';
if (await driveFileSystem.exists(odtPath)) {
driveFileSystem.createReadStream(odtPath).pipe(ctx.res);
return;
}
}
ctx.res.status(404).json({});
}
catch (err) {
if (err.message === 'Drive not shared with wikigdrive') {
const authConfig = this.authContainer['authConfig'];
ctx.res.status(404).json({ not_registered: true, share_email: authConfig.share_email });
return;
}
throw err;
}
}
async downloadTransformed(ctx) {
const driveId = await ctx.routeParamPath('driveId');
const fileId = await ctx.routeParamPath('fileId');
try {
const driveFileSystem = await this.filesService.getSubFileService(driveId, '');
const googleTreeProcessor = new GoogleTreeProcessor(driveFileSystem);
await googleTreeProcessor.load();
const [file, drivePath] = await googleTreeProcessor.findById(fileId);
if (file && drivePath) {
const filePath = `${drivePath}.${googleMimeToExt(file.mimeType, '')}`;
if (await driveFileSystem.exists(filePath)) {
ctx.res.header('Content-Disposition', `attachment; filename="${file['name']}.${googleMimeToExt(file.mimeType, '')}"`);
driveFileSystem.createReadStream(filePath).pipe(ctx.res);
return;
}
}
ctx.res.status(404).json({});
}
catch (err) {
if (err.message === 'Drive not shared with wikigdrive') {
const authConfig = this.authContainer['authConfig'];
ctx.res.status(404).json({ not_registered: true, share_email: authConfig.share_email });
return;
}
throw err;
}
}
}
__decorate([
RouteGet('/')
], DriveController.prototype, "getDrives", null);
__decorate([
RouteGet('/:driveId')
], DriveController.prototype, "getDrive", null);
__decorate([
RouteGet('/:driveId/file/(:fileId).odt'),
RouteResponse('stream', {}, 'application/vnd.oasis.opendocument.text')
], DriveController.prototype, "downloadOdt", null);
__decorate([
RouteGet('/:driveId/transformed/(:fileId)'),
RouteResponse('stream', {}, 'image')
], DriveController.prototype, "downloadTransformed", null);