UNPKG

@mieweb/wikigdrive

Version:

Google Drive to MarkDown synchronization

208 lines (207 loc) 8.78 kB
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, RoutePost, RouteUse } from './Controller.js'; import { GitScanner } from '../../../git/GitScanner.js'; import { UserConfigService } from '../../google_folder/UserConfigService.js'; import { initJob } from '../../job/JobManagerContainer.js'; export default class GitController extends Controller { constructor(subPath, filesService, jobManagerContainer, engine) { super(subPath); Object.defineProperty(this, "filesService", { enumerable: true, configurable: true, writable: true, value: filesService }); Object.defineProperty(this, "jobManagerContainer", { enumerable: true, configurable: true, writable: true, value: jobManagerContainer }); Object.defineProperty(this, "engine", { enumerable: true, configurable: true, writable: true, value: engine }); } async getHistory(ctx) { const driveId = await ctx.routeParamPath('driveId'); const filePath = ctx.req.originalUrl.replace('/api/git/' + driveId + '/history', '') || '/'; const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', ''); const gitScanner = new GitScanner(ctx.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com'); await gitScanner.initialize(); const googleFileSystem = await this.filesService.getSubFileService(driveId, ''); const userConfigService = new UserConfigService(googleFileSystem); const userConfig = await userConfigService.load(); const history = await gitScanner.history(filePath, userConfig.remote_branch); return history; } async getDiff(ctx) { const driveId = await ctx.routeParamPath('driveId'); const filePath = ctx.req.originalUrl.replace('/api/git/' + driveId + '/diff', '') || '/'; const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', ''); const gitScanner = new GitScanner(ctx.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com'); await gitScanner.initialize(); const history = await gitScanner.diff(filePath); return history; } async getCommit(ctx) { const driveId = await ctx.routeParamPath('driveId'); const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', ''); const gitScanner = new GitScanner(ctx.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com'); await gitScanner.initialize(); const changes = await gitScanner.changes(); return { changes }; } async postCommit(ctx) { const driveId = await ctx.routeParamPath('driveId'); const body = await ctx.routeParamBody(); const user = await ctx.routeParamUser(); const message = body.message; const filePaths = Array.isArray(body.filePaths) ? body.filePaths : (body.filePaths ? [body.filePaths] : []); await this.jobManagerContainer.schedule(driveId, { ...initJob(), type: 'git_commit', title: 'Git Commit', payload: JSON.stringify({ message, filePaths, user }) }); return { driveId, message }; } async postCmd(ctx) { const driveId = await ctx.routeParamPath('driveId'); const body = await ctx.routeParamBody(); const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', ''); const gitScanner = new GitScanner(ctx.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com'); await gitScanner.initialize(); const output = await gitScanner.cmd(body.cmd, body.arg || ''); return output; } async fetch(ctx) { const driveId = await ctx.routeParamPath('driveId'); await this.jobManagerContainer.schedule(driveId, { ...initJob(), type: 'git_fetch', title: 'Git Fetch' }); return { driveId }; } async pull(ctx) { const driveId = await ctx.routeParamPath('driveId'); await this.jobManagerContainer.schedule(driveId, { ...initJob(), type: 'git_pull', title: 'Git Pull' }); return { driveId }; } async push(ctx) { const driveId = await ctx.routeParamPath('driveId'); await this.jobManagerContainer.schedule(driveId, { ...initJob(), type: 'git_push', title: 'Git Push' }); return { driveId }; } async resetRemote(ctx) { const driveId = await ctx.routeParamPath('driveId'); await this.jobManagerContainer.schedule(driveId, { ...initJob(), type: 'git_reset', title: 'Git Reset to Remote', payload: 'remote' }); return { driveId, payload: 'remote' }; } async resetLocal(ctx) { const driveId = await ctx.routeParamPath('driveId'); await this.jobManagerContainer.schedule(driveId, { ...initJob(), type: 'git_reset', title: 'Git Reset to Local', payload: 'local' }); return { driveId, payload: 'local' }; } async removeUntracked(ctx) { const driveId = await ctx.routeParamPath('driveId'); try { const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', ''); const gitScanner = new GitScanner(ctx.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com'); await gitScanner.initialize(); await gitScanner.removeUntracked(); return {}; } catch (err) { ctx.logger.error(err.stack ? err.stack : err.message); if (err.message.indexOf('Failed to retrieve list of SSH authentication methods') > -1) { return { error: 'Failed to authenticate' }; } throw err; } } async removeCached(ctx) { const driveId = await ctx.routeParamPath('driveId'); const body = await ctx.routeParamBody(); try { const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', ''); const gitScanner = new GitScanner(ctx.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com'); await gitScanner.initialize(); await gitScanner.removeCached(body.filePath); return {}; } catch (err) { ctx.logger.error(err.stack ? err.stack : err.message); if (err.message.indexOf('Failed to retrieve list of SSH authentication methods') > -1) { return { error: 'Failed to authenticate' }; } throw err; } } } __decorate([ RouteUse('/:driveId/history') ], GitController.prototype, "getHistory", null); __decorate([ RouteUse('/:driveId/diff') ], GitController.prototype, "getDiff", null); __decorate([ RouteGet('/:driveId/commit') ], GitController.prototype, "getCommit", null); __decorate([ RoutePost('/:driveId/commit') ], GitController.prototype, "postCommit", null); __decorate([ RoutePost('/:driveId/cmd') ], GitController.prototype, "postCmd", null); __decorate([ RoutePost('/:driveId/fetch') ], GitController.prototype, "fetch", null); __decorate([ RoutePost('/:driveId/pull') ], GitController.prototype, "pull", null); __decorate([ RoutePost('/:driveId/push') ], GitController.prototype, "push", null); __decorate([ RoutePost('/:driveId/reset_remote') ], GitController.prototype, "resetRemote", null); __decorate([ RoutePost('/:driveId/reset_local') ], GitController.prototype, "resetLocal", null); __decorate([ RoutePost('/:driveId/remove_untracked') ], GitController.prototype, "removeUntracked", null); __decorate([ RoutePost('/:driveId/remove_cached') ], GitController.prototype, "removeCached", null);