@mieweb/wikigdrive
Version:
Google Drive to MarkDown synchronization
189 lines (155 loc) • 6.54 kB
text/typescript
import {Controller, RouteGet, RouteParamBody, RouteParamPath, RouteParamUser, RoutePost, RouteUse} from './Controller.ts';
import {GitScanner} from '../../../git/GitScanner.ts';
import {UserConfigService} from '../../google_folder/UserConfigService.ts';
import {FileContentService} from '../../../utils/FileContentService.ts';
import {initJob, JobManagerContainer} from '../../job/JobManagerContainer.ts';
import {ContainerEngine} from '../../../ContainerEngine.ts';
interface CommitPost {
message: string;
filePaths: string[];
}
interface CmdPost {
cmd: string;
arg?: string;
}
interface RemovePath {
filePath: string;
}
export default class GitController extends Controller {
constructor(subPath: string, private readonly filesService: FileContentService,
private jobManagerContainer: JobManagerContainer, private engine: ContainerEngine) {
super(subPath);
}
async getHistory( driveId: string) {
const filePath = this.req.originalUrl.replace('/api/git/' + driveId + '/history', '') || '/';
const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', '');
const gitScanner = new GitScanner(this.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( driveId: string) {
const filePath = this.req.originalUrl.replace('/api/git/' + driveId + '/diff', '') || '/';
const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', '');
const gitScanner = new GitScanner(this.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com');
await gitScanner.initialize();
const history = await gitScanner.diff(filePath);
return history;
}
async getCommit( driveId: string) {
const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', '');
const gitScanner = new GitScanner(this.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com');
await gitScanner.initialize();
const changes = await gitScanner.changes();
return { changes };
}
async postCommit( driveId: string, body: CommitPost, user) {
const message = body.message;
const filePaths: string[] = 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( driveId: string, body: CmdPost) {
const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', '');
const gitScanner = new GitScanner(this.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com');
await gitScanner.initialize();
const output = await gitScanner.cmd(body.cmd, body.arg || '');
return output;
}
async fetch( driveId: string) {
await this.jobManagerContainer.schedule(driveId, {
...initJob(),
type: 'git_fetch',
title: 'Git Fetch'
});
return { driveId };
}
async pull( driveId: string) {
await this.jobManagerContainer.schedule(driveId, {
...initJob(),
type: 'git_pull',
title: 'Git Pull'
});
return { driveId };
}
async push( driveId: string) {
await this.jobManagerContainer.schedule(driveId, {
...initJob(),
type: 'git_push',
title: 'Git Push'
});
return { driveId };
}
async resetRemote( driveId: string) {
await this.jobManagerContainer.schedule(driveId, {
...initJob(),
type: 'git_reset',
title: 'Git Reset to Remote',
payload: 'remote'
});
return { driveId, payload: 'remote'};
}
async resetLocal( driveId: string) {
await this.jobManagerContainer.schedule(driveId, {
...initJob(),
type: 'git_reset',
title: 'Git Reset to Local',
payload: 'local'
});
return { driveId, payload: 'local'};
}
async removeUntracked( driveId: string) {
try {
const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', '');
const gitScanner = new GitScanner(this.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com');
await gitScanner.initialize();
await gitScanner.removeUntracked();
return {};
} catch (err) {
this.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( driveId: string, body: RemovePath) {
try {
const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', '');
const gitScanner = new GitScanner(this.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com');
await gitScanner.initialize();
await gitScanner.removeCached(body.filePath);
return {};
} catch (err) {
this.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;
}
}
}