Skip to content

Commit

Permalink
Refactor transform_subdir
Browse files Browse the repository at this point in the history
  • Loading branch information
ggodlewski committed Sep 17, 2023
1 parent 4dd9f36 commit be7f756
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 40 deletions.
4 changes: 2 additions & 2 deletions apps/ui/src/components/DriveTools.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
</a>
</li>

<li class="list-group-item" v-if="treeEmpty && !isGDocsPreview">
<li class="list-group-item" v-if="selectedFolder.contentDir && treeEmpty && !isGDocsPreview">
Markdown tree empty
<a class="btn btn-outline-secondary me-2" @click.prevent="syncAll">Sync All</a>
</li>
<li class="list-group-item" v-else-if="treeVersion && treeVersion !== GIT_SHA">
<li class="list-group-item" v-else-if="selectedFolder.contentDir && treeVersion && treeVersion !== GIT_SHA">
Markdowns were generated with version: <em>{{treeVersion}}</em>.<br/>
WikiGDrive is now running: <em>{{GIT_SHA}}</em>.<br/>
<a class="btn btn-outline-secondary me-2" @click.prevent="transformAll">Update your entire tree now?</a>
Expand Down
3 changes: 1 addition & 2 deletions apps/ui/src/components/GitSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

<div class="card flex-column order-0 flex-grow-1 flex-shrink-1 overflow-scroll border-left-0-not-first">
<slot name="header">
<!-- <div class="card-header">Git</div>-->
</slot>
<div class="card-body">
<div class="form-group">
Expand Down Expand Up @@ -43,7 +42,7 @@
<button class="btn btn-primary" type="button" @click="save">Save</button>
<button v-if="remote_url && treeEmpty" class="btn btn-secondary" type="button" @click="saveAndReset">Save and reset to remote</button>
</div>
<button class="btn btn-danger float-end" type="button" @click="regenerateKey">Regenerate</button>
<button class="btn btn-danger float-end" type="button" @click="regenerateKey">Regenerate key</button>
</div>
</div>
</div>
Expand Down
22 changes: 14 additions & 8 deletions apps/ui/src/components/UserSettings.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
<template>
<div class="container mainbar__content-height" v-if="user_config">
<StatusToolBar :active-tab="activeTab" />
<slot name="toolbar">
<StatusToolBar :active-tab="activeTab" />
</slot>

<div class="overflow-scroll d-flex flex-row mt-3">
<SettingsSidebar />
<slot name="sidebar">
<SettingsSidebar />
</slot>

<div class="card flex-grow-1 flex-shrink-1 overflow-scroll border-left-0">
<div class="card flex-grow-1 flex-shrink-1 overflow-scroll border-left-0-not-first">
<slot name="header">
</slot>
<div class="card-body">
<form>
<div class="form-group">
<label :class="!user_config.transform_subdir ? 'text-danger' : ''">Content subdirectory</label>
<input class="form-control" rows="10" v-model="user_config.transform_subdir" />
</div>

<div class="form-group">
<label>Theme</label>
<select class="form-control" @change="changeTheme($event.target.value)">
Expand All @@ -24,11 +35,6 @@
<img v-if="user_config.hugo_theme.preview_img" :src="user_config.hugo_theme.preview_img" style="height: 250px;" :alt="user_config.hugo_theme.id" />
</div>

<div class="form-group">
<label>Content subdirectory</label>
<input class="form-control" rows="10" v-model="user_config.transform_subdir" />
</div>

<div class="form-group">
<label>Autosync</label>
<select class="form-control" @change="user_config.auto_sync = !user_config.auto_sync">
Expand Down
23 changes: 20 additions & 3 deletions apps/ui/src/pages/FolderView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,23 @@
<span></span>
</template>
</GitSettings>

<UserSettings v-if="!contentDir && !(activeTab === 'drive_config' || activeTab === 'drive_config_git')" :activeTab="activeTab">
<template v-slot:header>
<div class="card-header alert-danger">
Content subdirectory must be set and start with /
</div>
</template>
<template v-slot:toolbar>
<span></span>
</template>
<template v-slot:sidebar>
<span></span>
</template>
</UserSettings>

</div>
<IframePreview v-else-if="(activeTab === 'html' || activeTab === 'markdown') && !selectedFolder" :folder-path="folderPath" :activeTab="activeTab" :selectedFile="selectedFile" />
<IframePreview v-else-if="(activeTab === 'html' || activeTab === 'markdown') && !selectedFolder.path" :folder-path="folderPath" :activeTab="activeTab" :selectedFile="selectedFile" />
</div>
</div>
</template>
Expand Down Expand Up @@ -170,7 +185,9 @@ export default {
this.fetch();
this.rootFolder = this.$root.drive;
this.emitter.on('tree:changed', () => {
this.fetch();
this.$nextTick(() => {
this.fetch();
});
});
},
watch: {
Expand Down Expand Up @@ -215,7 +232,7 @@ export default {
const file = this.files.find(file => (file.realFileName || file.fileName) === baseName) || {
path: filePath.replace('/' + driveId + this.contentDir, '')
};
this.selectedFolder = null;
this.selectedFolder = {};
this.selectedFile = file || {};
} else {
parts.push(baseName);
Expand Down
2 changes: 1 addition & 1 deletion src/containers/action/ActionRunnerContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class ActionRunnerContainer extends Container {
const driveIdTransform: string = path.basename(generatedFileService.getRealPath());

const contentDir = config.transform_subdir ?
`/${driveIdTransform}/${config.transform_subdir}` :
`/${driveIdTransform}${ !config.transform_subdir.startsWith('/') ? '/' : '' }${config.transform_subdir}` :
`/${driveIdTransform}`;
const docker = new Docker({socketPath: '/var/run/docker.sock'});

Expand Down
2 changes: 1 addition & 1 deletion src/containers/server/ServerContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export class ServerContainer extends Container {
const backlinksController = new BackLinksController('/api/backlinks', this.filesService);
app.use('/api/backlinks', authenticate(this.logger), await backlinksController.getRouter());

const configController = new ConfigController('/api/config', this.filesService, <FolderRegistryContainer>this.engine.getContainer('folder_registry'));
const configController = new ConfigController('/api/config', this.filesService, <FolderRegistryContainer>this.engine.getContainer('folder_registry'), this.engine);
app.use('/api/config', authenticate(this.logger), await configController.getRouter());

const logsController = new LogsController('/api/logs', this.logger);
Expand Down
22 changes: 19 additions & 3 deletions src/containers/server/routes/ConfigController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {FileContentService} from '../../../utils/FileContentService';
import {GitScanner} from '../../../git/GitScanner';
import {UserConfigService} from '../../google_folder/UserConfigService';
import {FolderRegistryContainer} from '../../folder_registry/FolderRegistryContainer';
import {ContainerEngine} from "../../../ContainerEngine";

export interface ConfigBody {
config: {
Expand Down Expand Up @@ -38,7 +39,7 @@ async function loadHugoThemes(filesService: FileContentService) {

export class ConfigController extends Controller {

constructor(subPath: string, private readonly filesService: FileContentService, private folderRegistryContainer: FolderRegistryContainer) {
constructor(subPath: string, private readonly filesService: FileContentService, private folderRegistryContainer: FolderRegistryContainer, private engine: ContainerEngine) {
super(subPath);
}

Expand Down Expand Up @@ -89,8 +90,16 @@ export class ConfigController extends Controller {
if (body.config?.config_toml) {
userConfigService.config.config_toml = body.config?.config_toml;
}
if (body.config?.transform_subdir) {
userConfigService.config.transform_subdir = body.config?.transform_subdir;
let modified = false;
if ('string' === typeof body.config?.transform_subdir) {
let trimmed = body.config?.transform_subdir.trim();
if (trimmed.length > 0 && !trimmed.startsWith('/')) {
trimmed = '/' + trimmed;
}
if (userConfigService.config.transform_subdir !== trimmed) {
modified = true;
}
userConfigService.config.transform_subdir = trimmed;
}
if (body.config?.actions_yaml) {
userConfigService.config.actions_yaml = body.config?.actions_yaml;
Expand All @@ -107,6 +116,13 @@ export class ConfigController extends Controller {
await gitScanner.setRemoteUrl('');
}

if (modified) {
this.engine.emit(driveId, 'toasts:added', {
title: 'Config modified',
type: 'tree:changed'
});
}

return {
...await this.returnConfig(userConfigService),
remote_url: await gitScanner.getRemoteUrl()
Expand Down
14 changes: 11 additions & 3 deletions src/containers/server/routes/DriveController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,21 @@ export class DriveController extends Controller {
const tocFile = transformedTree.find(item => item.path === '/toc.md');
const navFile = transformedTree.find(item => item.path === '/.navigation.md' || item.path === '/navigation.md');

const contentPrefix = userConfigService.config.transform_subdir ? `/${userConfigService.config.transform_subdir}` : '';
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: tocFile ? contentPrefix + tocFile.path : null,
navFilePath: navFile ? contentPrefix + navFile.path : null
tocFilePath,
navFilePath
};
}

Expand Down
9 changes: 7 additions & 2 deletions src/containers/server/routes/DriveUiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ export class DriveUiController extends Controller {
await markdownTreeProcessor.load();
const [file, drivePath] = await markdownTreeProcessor.findById(fileId);
if (file && drivePath) {
const transformSubDir = userConfigService.config.transform_subdir ? '/' + userConfigService.config.transform_subdir : '';
this.res.redirect(`/drive/${driveId}${transformSubDir || ''}/${drivePath}`);
if (userConfigService.config.transform_subdir.length > 0) {
const transformSubDir = (!userConfigService.config.transform_subdir.startsWith('/') ? '/' : '')
+ userConfigService.config.transform_subdir;
this.res.redirect(`/drive/${driveId}${transformSubDir}/${drivePath}`);
} else {
this.res.redirect(`/drive/${driveId}`);
}
return;
} else {
this.res.redirect(`/drive/${driveId}`);
Expand Down
20 changes: 9 additions & 11 deletions src/containers/server/routes/FolderController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,16 @@ export default class FolderController extends Controller {
this.res.setHeader('wgd-drive-empty', googleTreeProcessor.getTree().length === 0 ? 'true' : 'false');
this.res.setHeader('wgd-tree-empty', markdownTreeProcessor.getTree().length === 0 ? 'true' : 'false');
this.res.setHeader('wgd-tree-version', treeVersion);
const contentDir = userConfigService.config.transform_subdir ? '/' + userConfigService.config.transform_subdir : '/';
this.res.setHeader('wgd-content-dir', contentDir);
this.res.setHeader('wgd-content-dir', userConfigService.config.transform_subdir || '');

if (!await transformedFileSystem.exists(filePath)) {
this.res.status(404).send({ message: 'Not exist in transformedFileSystem' });
return;
}

if (!userConfigService.config.transform_subdir || inDir('/' + userConfigService.config.transform_subdir, filePath)) {
const contentFilePath = !userConfigService.config.transform_subdir ?
filePath :
filePath.replace('/' + userConfigService.config.transform_subdir, '') || '/';
if ((userConfigService.config.transform_subdir || '').startsWith('/') && inDir(userConfigService.config.transform_subdir, filePath)) {
const prefixed_subdir = userConfigService.config.transform_subdir;
const contentFilePath = filePath.replace(prefixed_subdir, '') || '/';

const [treeItem] = contentFilePath === '/'
? await markdownTreeProcessor.getRootItem(driveId)
Expand Down Expand Up @@ -284,15 +282,15 @@ export default class FolderController extends Controller {
const changes = await getCachedChanges(this.logger, transformedFileSystem, contentFileService, googleFileSystem);
const subDir = await transformedFileSystem.getSubFileService(filePath);
const map1: Map<string, TreeItem> = new Map(
(await this.generateChildren(subDir, driveId, '/' + userConfigService.config.transform_subdir, filePath))
(await this.generateChildren(subDir, driveId, prefixed_subdir, filePath))
.map(element => [element.realFileName, element])
);
const map2: Map<string, TreeItem> = new Map(
treeItem.children.map(addPreviewUrl(userConfigService.config.hugo_theme, driveId))
.map(element => [element.realFileName, element])
);
treeItem.children = Object.values({ ...Object.fromEntries(map1), ...Object.fromEntries(map2) });
await addGitData(treeItem.children, changes, userConfigService.config.transform_subdir ? '/' + userConfigService.config.transform_subdir + '' : '');
await addGitData(treeItem.children, changes, prefixed_subdir);
await outputDirectory(this.res, treeItem);
return;
} else {
Expand Down Expand Up @@ -322,7 +320,7 @@ export default class FolderController extends Controller {
realFileName: '',
title: '',
mimeType: MimeTypes.FOLDER_MIME,
children: await this.generateChildren(subDir, driveId, '/' + userConfigService.config.transform_subdir, filePath)
children: await this.generateChildren(subDir, driveId, userConfigService.config.transform_subdir || '/', filePath)
};

const changes = await getCachedChanges(this.logger, transformedFileSystem, contentFileService, googleFileSystem);
Expand All @@ -347,15 +345,15 @@ export default class FolderController extends Controller {
}
}

async generateChildren(transformedFileSystem: FileContentService, driveId: FileId, transform_subdir: string, dirPath: string) {
async generateChildren(transformedFileSystem: FileContentService, driveId: FileId, subdir: string, dirPath: string) {
const scanner = new DirectoryScanner();
const files = await scanner.scan(transformedFileSystem);

return Object.values(files)
.map(file => {
return {
fileName: file.fileName,
id: transform_subdir === dirPath + file.fileName ? driveId : 'UNKNOWN',
id: subdir === dirPath + file.fileName ? driveId : 'UNKNOWN',
parentId: 'UNKNOWN',
path: dirPath + file.fileName,
realFileName: file.fileName,
Expand Down
11 changes: 8 additions & 3 deletions src/containers/server/routes/GoogleDriveController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export class GoogleDriveController extends Controller {

const treeItem = addPreviewUrl(userConfigService.config.hugo_theme, driveId)(foundTreeItem);

const contentDir = userConfigService.config.transform_subdir ? '/' + userConfigService.config.transform_subdir : '/';
const contentDir = (userConfigService.config.transform_subdir || '').startsWith('/') ? userConfigService.config.transform_subdir : undefined;

this.res.setHeader('wgd-content-dir', contentDir);
this.res.setHeader('wgd-content-dir', contentDir || '');
this.res.setHeader('wgd-google-parent-id', treeItem.parentId || '');
this.res.setHeader('wgd-google-id', treeItem.id || '');
this.res.setHeader('wgd-google-version', treeItem.version || '');
Expand All @@ -76,6 +76,11 @@ export class GoogleDriveController extends Controller {
this.res.setHeader('wgd-preview-url', treeItem.previewUrl || '');
this.res.setHeader('wgd-last-author', treeItem.lastAuthor || '');

if (!contentDir) {
this.res.status(404).send('Content subdirectory must be set and start with /');
return;
}

const treeProcessor = new GoogleTreeProcessor(googleFileSystem);
await treeProcessor.load();
const [leaf] = await treeProcessor.findById(foundTreeItem.id);
Expand All @@ -86,7 +91,7 @@ export class GoogleDriveController extends Controller {
}

const changes = await getCachedChanges(this.logger, transformedFileSystem, contentFileService, googleFileSystem);
const change = changes.find(change => change.path === (contentDir + treeItem.path).replace(/^\//, ''));
const change = changes.find(change => change.path === (contentDir.substring(1) + treeItem.path).replace(/^\//, ''));
if (change) {
if (change.state.isNew) {
treeItem['status'] = 'N';
Expand Down
2 changes: 1 addition & 1 deletion src/containers/server/routes/SearchController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class SearchController extends Controller {
const userConfigService = new UserConfigService(googleFileSystem);
await userConfigService.load();

const prefix = userConfigService.config.transform_subdir ? `/${userConfigService.config.transform_subdir}` : '';
const prefix = (userConfigService.config.transform_subdir || '').startsWith('/') ? `${userConfigService.config.transform_subdir}` : '';

const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', '');

Expand Down
4 changes: 4 additions & 0 deletions src/containers/transform/TransformContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ export class TransformContainer extends Container {
}

async run(rootFolderId: FileId) {
if (!this.userConfigService.config.transform_subdir.startsWith('/')) {
this.logger.warn('Content subdirectory must be set and start with /');
return;
}
const contentFileService = await getContentFileService(this.generatedFileService, this.userConfigService);

const queueTransformer = new QueueTransformer(this.logger);
Expand Down
8 changes: 8 additions & 0 deletions src/utils/FileContentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export class FileContentService extends FileService {
constructor(protected readonly rootPath: string = '/', virtualPath = '/') {
super(rootPath);
this.virtualPath = virtualPath || '/';
if (!this.virtualPath.endsWith('/')) {
this.virtualPath += '/';
}
}

async readFile(filePath: string): Promise<string> {
Expand Down Expand Up @@ -44,6 +47,11 @@ export class FileContentService extends FileService {
if (!subPath) {
throw new Error('Empty subPath');
}

if (subPath.startsWith('/')) { // this.virtualPath always ends with '/'
subPath = subPath.substring(1);
}

const subFileService = new FileContentService(
pathResolve(this.rootPath, subPath),
virtualPath !== undefined ? virtualPath : this.virtualPath + subPath + '/'
Expand Down

0 comments on commit be7f756

Please sign in to comment.