Skip to content

Commit

Permalink
Fix links to files with removed access
Browse files Browse the repository at this point in the history
Resolves #346
  • Loading branch information
ggodlewski committed Aug 24, 2023
1 parent fa53157 commit aab1f8e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/containers/transform/LocalLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,17 @@ export class LocalLog {
this.rows = this.rows.filter(logRow => logRow.filePath !== filePath);
return originalLength !== this.rows.length;
}

async getDirFiles(prefix: string): Promise<LogRow[]> {
const list = this.rows
.filter(row => row.filePath.startsWith(prefix) && row.filePath.substring(prefix.length).indexOf('/') === -1)
.filter(row => row.type === 'md');

const lastOnes: {[key: string]: LogRow} = {};
for (const item of list) {
lastOnes[item.filePath] = item;
}

return Object.values(lastOnes).filter(item => item.event !== 'removed');
}
}
21 changes: 19 additions & 2 deletions src/containers/transform/TransformContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {convertToRelativeMarkDownPath, convertToRelativeSvgPath} from '../../Lin
import {LocalFilesGenerator} from './LocalFilesGenerator';
import {QueueTransformer} from './QueueTransformer';
import {generateNavigationHierarchy, NavigationHierarchy} from './generateNavigationHierarchy';
import {ConflictFile, LocalFile, RedirFile} from '../../model/LocalFile';
import {ConflictFile, LocalFile, LocalFileMap, RedirFile} from '../../model/LocalFile';
import {TaskLocalFileTransform} from './TaskLocalFileTransform';
import {GoogleFile, MimeTypes} from '../../model/GoogleFile';
import {generateDirectoryYaml, parseDirectoryYaml} from './frontmatters/generateDirectoryYaml';
Expand Down Expand Up @@ -240,6 +240,7 @@ export class TransformContainer extends Container {
const destinationScanner = new DirectoryScanner();
const destinationFiles = await destinationScanner.scan(destinationDirectory);
await addBinaryMetaData(destinationFiles, destinationDirectory);
await this.removeOutdatedLogEntries(destinationDirectory, destinationFiles);

const localFilesGenerator = new LocalFilesGenerator();
const filesToGenerate: LocalFile[] = await localFilesGenerator.generateLocalFiles(googleFolderFiles);
Expand Down Expand Up @@ -421,7 +422,7 @@ export class TransformContainer extends Container {
const newContent = content.replace(/(gdoc:[A-Z0-9_-]+)/ig, (str: string) => {
const fileId = str.substring('gdoc:'.length);
const lastLog = this.localLog.findLastFile(fileId);
if (lastLog) {
if (lastLog && lastLog.event !== 'removed') {
if (fileName.endsWith('.svg')) {
return convertToRelativeSvgPath(lastLog.filePath, destinationDirectory.getVirtualPath() + fileName);
} else {
Expand Down Expand Up @@ -524,4 +525,20 @@ export class TransformContainer extends Container {
onProgressNotify(callback: ({total, completed, warnings}: { total?: number; completed?: number, warnings?: number }) => void) {
this.progressNotifyCallback = callback;
}

async removeOutdatedLogEntries(destinationDirectory: FileContentService, destinationFiles: LocalFileMap) {
const prefix = destinationDirectory.getVirtualPath();
const logFiles = await this.localLog.getDirFiles(prefix);
for (const logEntry of logFiles) {
const fileName = logEntry.filePath.substring(prefix.length);
if (!destinationFiles[fileName]) {
this.localLog.append({
filePath: logEntry.filePath,
id: logEntry.id,
type: logEntry.type,
event: 'removed',
});
}
}
}
}

0 comments on commit aab1f8e

Please sign in to comment.