add logs and fix issues

This commit is contained in:
Abhinav 2023-04-25 14:41:59 +05:30
parent 102f13d7e5
commit 6c15dca704
3 changed files with 56 additions and 13 deletions

View file

@ -173,7 +173,7 @@ export default function ExportModal(props: Props) {
const exportRecord = await exportService.getExportRecord( const exportRecord = await exportService.getExportRecord(
exportFolder exportFolder
); );
if (!exportRecord) { if (!exportRecord?.stage) {
setExportStage(ExportStage.INIT); setExportStage(ExportStage.INIT);
return null; return null;
} }

View file

@ -55,7 +55,7 @@ import { FILE_TYPE, TYPE_JPEG, TYPE_JPG } from 'constants/file';
import { ExportStage } from 'constants/export'; import { ExportStage } from 'constants/export';
import { ElectronAPIs } from 'types/electron'; import { ElectronAPIs } from 'types/electron';
import { CustomError } from 'utils/error'; import { CustomError } from 'utils/error';
import { addLogLine } from 'utils/logging'; import { addLocalLog, addLogLine } from 'utils/logging';
import { eventBus, Events } from './events'; import { eventBus, Events } from './events';
import { getCollectionNameMap } from 'utils/collection'; import { getCollectionNameMap } from 'utils/collection';
@ -244,9 +244,11 @@ class ExportService {
const user: User = getData(LS_KEYS.USER); const user: User = getData(LS_KEYS.USER);
const localFiles = await getLocalFiles(); const localFiles = await getLocalFiles();
const userPersonalFiles = localFiles const userPersonalFiles = mergeMetadata(
.filter((file) => file.ownerID === user?.id) localFiles
.sort((fileA, fileB) => fileA.id - fileB.id); .filter((file) => file.ownerID === user?.id)
.sort((fileA, fileB) => fileA.id - fileB.id)
);
const collections = await getLocalCollections(); const collections = await getLocalCollections();
const nonEmptyCollections = getNonEmptyCollections( const nonEmptyCollections = getNonEmptyCollections(
@ -274,6 +276,7 @@ class ExportService {
renamedCollections?.length > 0 && renamedCollections?.length > 0 &&
this.checkAllElectronAPIsExists() this.checkAllElectronAPIsExists()
) { ) {
addLogLine(`renaming ${renamedCollections.length} collections`);
this.collectionRenamer( this.collectionRenamer(
exportDir, exportDir,
collectionIDPathMap, collectionIDPathMap,
@ -287,6 +290,9 @@ class ExportService {
); );
if (deletedExportedCollections?.length > 0) { if (deletedExportedCollections?.length > 0) {
addLogLine(
`removing ${deletedExportedCollections.length} collections`
);
await this.collectionRemover( await this.collectionRemover(
deletedExportedCollections, deletedExportedCollections,
exportDir exportDir
@ -299,6 +305,7 @@ class ExportService {
); );
if (filesToExport?.length > 0) { if (filesToExport?.length > 0) {
addLogLine(`exporting ${filesToExport.length} files`);
await this.fileExporter( await this.fileExporter(
filesToExport, filesToExport,
collectionIDNameMap, collectionIDNameMap,
@ -311,7 +318,8 @@ class ExportService {
exportRecord exportRecord
); );
if (removedFileUIDs?.length) { if (removedFileUIDs?.length > 0) {
addLogLine(`removing ${removedFileUIDs.length} files`);
await this.fileRemover(removedFileUIDs, exportDir); await this.fileRemover(removedFileUIDs, exportDir);
} }
} catch (e) { } catch (e) {
@ -325,6 +333,10 @@ class ExportService {
renamedCollections: Collection[] renamedCollections: Collection[]
) { ) {
for (const collection of renamedCollections) { for (const collection of renamedCollections) {
addLocalLog(
() =>
`renaming collection ${collection.name} with id ${collection.id}`
);
const oldCollectionFolderPath = collectionIDPathMap.get( const oldCollectionFolderPath = collectionIDPathMap.get(
collection.id collection.id
); );
@ -357,6 +369,10 @@ class ExportService {
exportRecord.exportedCollectionPaths exportRecord.exportedCollectionPaths
); );
for (const collectionID of deletedExportedCollectionIDs) { for (const collectionID of deletedExportedCollectionIDs) {
addLocalLog(
() =>
`removing collection with id ${collectionID} from export folder`
);
const collectionFolderPath = collectionIDPathMap.get(collectionID); const collectionFolderPath = collectionIDPathMap.get(collectionID);
await this.electronAPIs.removeFolder(collectionFolderPath); await this.electronAPIs.removeFolder(collectionFolderPath);
await this.removeCollectionExportedRecord( await this.removeCollectionExportedRecord(
@ -381,6 +397,14 @@ class ExportService {
total: files.length, total: files.length,
}); });
for (const file of files) { for (const file of files) {
addLocalLog(
() =>
`exporting file ${file.metadata.title} with id ${
file.id
} from collection ${collectionIDNameMap.get(
file.collectionID
)}`
);
if (this.stopExport) { if (this.stopExport) {
break; break;
} }
@ -445,6 +469,7 @@ class ExportService {
exportRecord.exportedFilePaths exportRecord.exportedFilePaths
); );
for (const fileUID of removedFileUIDs) { for (const fileUID of removedFileUIDs) {
addLocalLog(() => `removing file with id ${fileUID}`);
if (this.stopExport) { if (this.stopExport) {
break; break;
} }
@ -525,7 +550,7 @@ class ExportService {
exportRecord.exportedCollectionPaths = Object.fromEntries( exportRecord.exportedCollectionPaths = Object.fromEntries(
Object.entries(exportRecord.exportedCollectionPaths).filter( Object.entries(exportRecord.exportedCollectionPaths).filter(
([key]) => key === collectionID.toString() ([key]) => key !== collectionID.toString()
) )
); );
@ -541,7 +566,7 @@ class ExportService {
const exportRecord = await this.getExportRecord(folder); const exportRecord = await this.getExportRecord(folder);
exportRecord.exportedFilePaths = Object.fromEntries( exportRecord.exportedFilePaths = Object.fromEntries(
Object.entries(exportRecord.exportedFilePaths).filter( Object.entries(exportRecord.exportedFilePaths).filter(
([key]) => key === fileUID ([key]) => key !== fileUID
) )
); );
await this.updateExportRecord(exportRecord, folder); await this.updateExportRecord(exportRecord, folder);
@ -658,7 +683,6 @@ class ExportService {
collectionPath: string collectionPath: string
): Promise<string> { ): Promise<string> {
try { try {
file.metadata = mergeMetadata([file])[0].metadata;
const fileSaveName = getUniqueFileSaveName( const fileSaveName = getUniqueFileSaveName(
collectionPath, collectionPath,
file.metadata.title, file.metadata.title,

View file

@ -13,6 +13,7 @@ import { splitFilenameAndExtension } from 'utils/file';
import { ENTE_METADATA_FOLDER } from 'constants/export'; import { ENTE_METADATA_FOLDER } from 'constants/export';
import sanitize from 'sanitize-filename'; import sanitize from 'sanitize-filename';
import { formatDateTimeShort } from 'utils/time/format'; import { formatDateTimeShort } from 'utils/time/format';
import { addLocalLog } from 'utils/logging';
export const getExportRecordFileUID = (file: EnteFile) => export const getExportRecordFileUID = (file: EnteFile) =>
`${file.id}_${file.collectionID}_${file.updationTime}`; `${file.id}_${file.collectionID}_${file.updationTime}`;
@ -21,10 +22,11 @@ export const getCollectionsCreatedAfterLastExport = (
collections: Collection[], collections: Collection[],
exportRecord: ExportRecord exportRecord: ExportRecord
) => { ) => {
if (!exportRecord?.exportedCollectionPaths) {
return collections;
}
const exportedCollections = new Set( const exportedCollections = new Set(
Object.keys(exportRecord?.exportedCollectionPaths ?? {}).map((x) => Object.keys(exportRecord?.exportedCollectionPaths).map((x) => Number(x))
Number(x)
)
); );
const unExportedCollections = collections.filter((collection) => { const unExportedCollections = collections.filter((collection) => {
if (!exportedCollections.has(collection.id)) { if (!exportedCollections.has(collection.id)) {
@ -85,8 +87,19 @@ export const getDeletedExportedCollections = (
exportRecord: ExportRecord exportRecord: ExportRecord
) => { ) => {
const presentCollections = new Set( const presentCollections = new Set(
collections?.map((collection) => collection.id) collections.map((collection) => collection.id)
); );
addLocalLog(
() => `
presentCollections: ${collections.map((c) => c.id)}
exportRecord?.exportedCollectionPaths: ${JSON.stringify(
exportRecord?.exportedCollectionPaths
)}
`
);
if (!exportRecord?.exportedCollectionPaths) {
return [];
}
const deletedExportedCollections = Object.keys( const deletedExportedCollections = Object.keys(
exportRecord?.exportedCollectionPaths exportRecord?.exportedCollectionPaths
) )
@ -121,6 +134,9 @@ export const getExportedFiles = (
allFiles: EnteFile[], allFiles: EnteFile[],
exportRecord: ExportRecord exportRecord: ExportRecord
) => { ) => {
if (!exportRecord?.exportedFilePaths) {
return [];
}
const exportedFileIds = new Set( const exportedFileIds = new Set(
Object.keys(exportRecord?.exportedFilePaths) Object.keys(exportRecord?.exportedFilePaths)
); );
@ -140,6 +156,9 @@ export const getDeletedExportedFiles = (
const presentFileUIDs = new Set( const presentFileUIDs = new Set(
allFiles?.map((file) => getExportRecordFileUID(file)) allFiles?.map((file) => getExportRecordFileUID(file))
); );
if (!exportRecord?.exportedFilePaths) {
return [];
}
const deletedExportedFiles = Object.keys( const deletedExportedFiles = Object.keys(
exportRecord?.exportedFilePaths exportRecord?.exportedFilePaths
).filter((fileUID) => { ).filter((fileUID) => {