2024-01-10 00:03:34 +02:00
|
|
|
import { ImportExportResult, ImportOptions } from './types';
|
2021-09-23 14:15:31 +02:00
|
|
|
import importEnex from '../../import-enex';
|
2021-01-22 19:41:11 +02:00
|
|
|
import InteropService_Importer_Base from './InteropService_Importer_Base';
|
|
|
|
import Folder from '../../models/Folder';
|
2024-01-10 00:03:34 +02:00
|
|
|
import { FolderEntity } from '../database/types';
|
|
|
|
import { fileExtension, rtrimSlashes } from '../../path-utils';
|
|
|
|
import shim from '../../shim';
|
2020-11-05 18:58:23 +02:00
|
|
|
const { filename } = require('../../path-utils');
|
2018-02-26 21:25:54 +02:00
|
|
|
|
2024-01-10 00:03:34 +02:00
|
|
|
const doImportEnex = async (destFolder: FolderEntity, sourcePath: string, options: ImportOptions) => {
|
|
|
|
if (!destFolder) {
|
|
|
|
const folderTitle = await Folder.findUniqueItemTitle(filename(sourcePath));
|
|
|
|
destFolder = await Folder.save({ title: folderTitle });
|
|
|
|
}
|
|
|
|
|
|
|
|
await importEnex(destFolder.id, sourcePath, options);
|
|
|
|
};
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-01-10 00:03:34 +02:00
|
|
|
export const enexImporterExec = async (result: ImportExportResult, destinationFolder: FolderEntity, sourcePath: string, fileExtensions: string[], options: any) => {
|
|
|
|
sourcePath = rtrimSlashes(sourcePath);
|
2018-02-26 21:25:54 +02:00
|
|
|
|
2024-01-10 00:03:34 +02:00
|
|
|
if (await shim.fsDriver().isDirectory(sourcePath)) {
|
|
|
|
const stats = await shim.fsDriver().readDirStats(sourcePath);
|
|
|
|
for (const stat of stats) {
|
|
|
|
const fullPath = `${sourcePath}/${stat.path}`;
|
|
|
|
if (!fileExtensions.includes(fileExtension(fullPath).toLowerCase())) continue;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await doImportEnex(null, fullPath, options);
|
|
|
|
} catch (error) {
|
|
|
|
result.warnings.push(`When importing "${fullPath}": ${error.message}`);
|
|
|
|
}
|
2018-02-26 21:25:54 +02:00
|
|
|
}
|
2024-01-10 00:03:34 +02:00
|
|
|
} else {
|
|
|
|
await doImportEnex(destinationFolder, sourcePath, options);
|
|
|
|
}
|
2018-02-26 21:25:54 +02:00
|
|
|
|
2024-01-10 00:03:34 +02:00
|
|
|
return result;
|
|
|
|
};
|
2018-02-26 21:25:54 +02:00
|
|
|
|
2024-01-10 00:03:34 +02:00
|
|
|
export default class InteropService_Importer_EnexToMd extends InteropService_Importer_Base {
|
|
|
|
public async exec(result: ImportExportResult) {
|
|
|
|
return enexImporterExec(
|
|
|
|
result,
|
|
|
|
this.options_.destinationFolder,
|
|
|
|
this.sourcePath_,
|
|
|
|
this.metadata().fileExtensions,
|
|
|
|
this.options_,
|
|
|
|
);
|
2018-02-26 21:25:54 +02:00
|
|
|
}
|
2024-01-10 00:03:34 +02:00
|
|
|
|
2018-02-26 21:25:54 +02:00
|
|
|
}
|