Skip to content

TagSpaces is an offline, open source, document manager with tagging support

fix: issue with persisting of file id on opening

Ilian Sapundshiev10146dfdd217
diff --git a/src/renderer/components/dialogs/hooks/ResolveConflictContextProvider.tsx b/src/renderer/components/dialogs/hooks/ResolveConflictContextProvider.tsx
index b3b35276b..7f690ca48 100644
--- a/src/renderer/components/dialogs/hooks/ResolveConflictContextProvider.tsx
+++ b/src/renderer/components/dialogs/hooks/ResolveConflictContextProvider.tsx
@@ -144,7 +144,12 @@ export const ResolveConflictContextProvider = ({
if (!location?.isReadOnly) {
// write revisions
if (Pro && revisionsEnabled && !isMeta(fileOpen.path)) {
- const id = await getMetadataID(fileOpen.path, fileOpen.uuid, location);
+ const id = await getMetadataID(
+ fileOpen.path,
+ fileOpen.uuid,
+ location,
+ fileOpen.isFile,
+ );
const backupDir = getBackupFileDir(fileOpen.path, fileOpen.uuid);
location.listDirectoryPromise(backupDir, []).then(async (backup) => {
const haveBackup = backup.some((b) =>
diff --git a/src/renderer/components/menus/DirectoryMenu.tsx b/src/renderer/components/menus/DirectoryMenu.tsx
index 18c9f34d8..abf826ad2 100644
--- a/src/renderer/components/menus/DirectoryMenu.tsx
+++ b/src/renderer/components/menus/DirectoryMenu.tsx
@@ -150,14 +150,17 @@ function DirectoryMenu(props: Props) {
selectedEntries[0] ? selectedEntries[0].name : currentDirectoryPath,
currentLocation?.getDirSeparator(),
);
- return getMetadataID(entryPath, selectedEntries[0]?.uuid, tmpLoc).then(
- (id) => {
- return {
- url: generateSharingLink(locationID, undefined, relativePath, id),
- name: folderName,
- };
- },
- );
+ return getMetadataID(
+ entryPath,
+ selectedEntries[0]?.uuid,
+ tmpLoc,
+ false,
+ ).then((id) => {
+ return {
+ url: generateSharingLink(locationID, undefined, relativePath, id),
+ name: folderName,
+ };
+ });
});
}
diff --git a/src/renderer/components/menus/FileMenu.tsx b/src/renderer/components/menus/FileMenu.tsx
index 092773b08..f2df9b664 100644
--- a/src/renderer/components/menus/FileMenu.tsx
+++ b/src/renderer/components/menus/FileMenu.tsx
@@ -134,6 +134,7 @@ function FileMenu(props: Props) {
selectedEntries[0].path,
selectedEntries[0].uuid,
currentLocation,
+ selectedEntries[0].isFile,
).then((id) =>
generateSharingLink(currentLocationId, relativePath, undefined, id),
);
diff --git a/src/renderer/hooks/IOActionsContextProvider.tsx b/src/renderer/hooks/IOActionsContextProvider.tsx
index 3723e3438..9f6acd816 100644
--- a/src/renderer/hooks/IOActionsContextProvider.tsx
+++ b/src/renderer/hooks/IOActionsContextProvider.tsx
@@ -179,11 +179,13 @@ type IOActionsContextData = {
path: string,
id: string,
location: CommonLocation,
+ isFile: boolean,
) => Promise<TS.FileSystemEntryMeta>;
getMetadataID: (
path: string,
id: string,
location: CommonLocation,
+ isFile: boolean,
) => Promise<string>;
saveFsEntryMeta: (
entry: TS.FileSystemEntry,
@@ -1849,8 +1851,9 @@ export const IOActionsContextProvider = ({
path: string,
id: string,
location: CommonLocation,
+ isFile: boolean,
): Promise<string> {
- return getMetadata(path, id, location)
+ return getMetadata(path, id, location, isFile)
.then((metaData) => metaData.id)
.catch((err) => {
console.log('Error getting metadata id: ' + err);
@@ -1862,50 +1865,37 @@ export const IOActionsContextProvider = ({
path: string,
id: string,
location: CommonLocation,
+ isFile: boolean,
): Promise<TS.FileSystemEntryMeta> {
+ const isMetaPath =
+ path.indexOf(location.getDirSeparator() + AppConfig.metaFolder) !== -1 ||
+ path.indexOf(AppConfig.metaFolder + location.getDirSeparator()) !== -1;
+
return location
.loadMetaDataPromise(path)
.then((fsEntryMeta: TS.FileSystemEntryMeta) => {
- if (!fsEntryMeta) {
- if (
- path.indexOf(location.getDirSeparator() + AppConfig.metaFolder) ===
- -1 &&
- path.indexOf(AppConfig.metaFolder + location.getDirSeparator()) ===
- -1 &&
- !location.isReadOnly
- ) {
- // create new meta id to not be changed -> next time listDirectory will get the same id for the file from meta
- const metaData = { id: id };
- const metaFilePath = path.endsWith(location.getDirSeparator())
- ? getMetaFileLocationForDir(path, location.getDirSeparator())
- : getMetaFileLocationForFile(path, location.getDirSeparator());
-
- return saveTextFilePromise(
- { path: metaFilePath, locationID: location.uuid },
- JSON.stringify(metaData),
- false,
- )
- .then(() => metaData)
- .catch((e) => {
- console.error(e);
- return metaData;
- });
- } else {
- return { id: id };
- }
- } else if (fsEntryMeta?.id) {
+ if (fsEntryMeta?.id) {
return fsEntryMeta;
- } else if (!fsEntryMeta?.id && !location.isReadOnly) {
- return saveFsEntryMeta(location.toFsEntry(path, fsEntryMeta.isFile), {
+ } else if (!location.isReadOnly) {
+ // meta exists but has no id — save it with the id
+ return saveFsEntryMeta(location.toFsEntry(path, isFile), {
...fsEntryMeta,
id: id,
- }).then((fsEntryMeta) => fsEntryMeta);
+ })
+ .then((meta) => meta ?? { id: id })
+ .catch(() => ({ id: id }));
} else {
return { id: id };
}
})
- .catch((e) => {
- console.log('Error getting metadata for: ' + path + ' - ' + e);
+ .catch(() => {
+ // loadMetaDataPromise throws when no meta file exists yet
+ if (!isMetaPath && !location.isReadOnly) {
+ // create new meta sidecar so id is persisted across sessions
+ return saveFsEntryMeta(location.toFsEntry(path, isFile), { id: id })
+ .then((meta) => meta ?? { id: id })
+ .catch(() => ({ id: id }));
+ }
return { id: id };
});
}
@@ -1925,7 +1915,7 @@ export const IOActionsContextProvider = ({
meta.description !== undefined
) {
const uuid = entry.isFile ? entry.uuid : entry.meta?.id;
- getMetadataID(entry.path, uuid, location).then((id) => {
+ getMetadataID(entry.path, uuid, location, entry.isFile).then((id) => {
if (fsEntryMeta && fsEntryMeta.description) {
const backupDir = getBackupDir(entry);
location.listDirectoryPromise(backupDir, []).then((backup) => {
@@ -2356,7 +2346,7 @@ export const IOActionsContextProvider = ({
currentDirPath: string,
dir: TS.OrderVisibilitySettings,
): Promise<TS.FileSystemEntryMeta> {
- return getMetadata(currentDirPath, dir.uuid, currentLocation)
+ return getMetadata(currentDirPath, dir.uuid, currentLocation, false)
.then((fsEntryMeta) => {
const customOrder: TS.CustomOrder = fsEntryMeta.customOrder
? fsEntryMeta.customOrder
diff --git a/src/renderer/hooks/OpenedEntryContextProvider.tsx b/src/renderer/hooks/OpenedEntryContextProvider.tsx
index 0af768bf2..8944bc0bc 100644
--- a/src/renderer/hooks/OpenedEntryContextProvider.tsx
+++ b/src/renderer/hooks/OpenedEntryContextProvider.tsx
@@ -596,6 +596,7 @@ export const OpenedEntryContextProvider = ({
fsEntry.path,
fsEntry.uuid,
loc,
+ fsEntry.isFile,
);
if (meta) {
entryForOpening.uuid = meta.id;