@mieweb/wikigdrive
Version:
Google Drive to MarkDown synchronization
122 lines (121 loc) • 5.59 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 process from 'node:process';
import { Controller, RouteErrorHandler, RouteGet, RouteResponse } from './Controller.js';
import { ShareErrorHandler } from './FolderController.js';
import { filterParams } from '../../../google/driveFetch.js';
import { GoogleDriveService } from '../../../google/GoogleDriveService.js';
import { MarkdownTreeProcessor } from '../../transform/MarkdownTreeProcessor.js';
import { UserConfigService } from '../../google_folder/UserConfigService.js';
import { UserAuthClient } from '../../../google/AuthClient.js';
import { getContentFileService } from '../../transform/utils.js';
export class DriveUiController extends Controller {
constructor(subPath, queryLogger, filesService, googleApiContainer) {
super(subPath);
Object.defineProperty(this, "queryLogger", {
enumerable: true,
configurable: true,
writable: true,
value: queryLogger
});
Object.defineProperty(this, "filesService", {
enumerable: true,
configurable: true,
writable: true,
value: filesService
});
Object.defineProperty(this, "googleApiContainer", {
enumerable: true,
configurable: true,
writable: true,
value: googleApiContainer
});
}
async getFolder(ctx) {
const state = ctx.routeParamQuery('state');
if (!state) {
throw new Error('No state query parameter');
}
const obj = JSON.parse(state);
const action = obj.action;
const ids = obj.ids;
if (action === 'open' && ids.length > 0) {
const fileId = ids[0];
const googleDriveService = new GoogleDriveService(ctx.logger, this.googleApiContainer.getQuotaLimiter());
const auth = this.googleApiContainer.getAuth();
const drives = await this.googleApiContainer.listDrives();
const driveIds = drives.map(drive => drive.id);
let dir = await googleDriveService.getFile(auth, fileId);
while (dir.parentId) {
dir = await googleDriveService.getFile(auth, dir.parentId);
}
if (!dir.id || !driveIds.includes(dir.id)) {
ctx.res.send({ not_shared: 1 });
return;
}
const driveId = dir.id;
const googleFileSystem = await this.filesService.getSubFileService(driveId, '/');
const userConfigService = new UserConfigService(googleFileSystem);
await userConfigService.load();
const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', '');
const contentFileService = await getContentFileService(transformedFileSystem, userConfigService);
const markdownTreeProcessor = new MarkdownTreeProcessor(contentFileService);
await markdownTreeProcessor.load();
const [file, drivePath] = await markdownTreeProcessor.findById(fileId);
if (file && drivePath) {
if (userConfigService.config.transform_subdir.length > 0) {
const transformSubDir = (!userConfigService.config.transform_subdir.startsWith('/') ? '/' : '')
+ userConfigService.config.transform_subdir;
ctx.res.redirect(`/drive/${driveId}${transformSubDir}/${drivePath}`);
}
else {
ctx.res.redirect(`/drive/${driveId}`);
}
return;
}
else {
ctx.res.redirect(`/drive/${driveId}`);
}
}
else {
ctx.res.send({ invalid_action: 1 });
return;
}
}
async getInstalled() {
return { installed: true };
}
async getInstall(ctx) {
const serverUrl = process.env.AUTH_DOMAIN || process.env.DOMAIN;
const state = new URLSearchParams(filterParams({
driveui: 1,
instance: process.env.AUTH_INSTANCE
// driveId: driveId !== 'none' ? (driveId || '') : '',
// redirectTo
})).toString();
const authClient = new UserAuthClient(process.env.GOOGLE_AUTH_CLIENT_ID, process.env.GOOGLE_AUTH_CLIENT_SECRET);
const authUrl = await authClient.getWebDriveInstallUrl(`${serverUrl}/auth`, state);
if (process.env.VERSION === 'dev') {
console.debug(authUrl);
}
ctx.res.redirect(authUrl);
}
}
__decorate([
RouteGet('/'),
RouteErrorHandler(new ShareErrorHandler()),
RouteResponse('stream')
], DriveUiController.prototype, "getFolder", null);
__decorate([
RouteGet('/installed'),
RouteErrorHandler(new ShareErrorHandler())
], DriveUiController.prototype, "getInstalled", null);
__decorate([
RouteGet('/install'),
RouteErrorHandler(new ShareErrorHandler()),
RouteResponse('stream')
], DriveUiController.prototype, "getInstall", null);