Skip to content

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

fix: opening assigned files from the file manager

Ilian Sapundshieveb9ff7028316
diff --git a/src/renderer/hooks/CurrentLocationContextProvider.tsx b/src/renderer/hooks/CurrentLocationContextProvider.tsx
index 4cfc353a1..86909ff7d 100644
--- a/src/renderer/hooks/CurrentLocationContextProvider.tsx
+++ b/src/renderer/hooks/CurrentLocationContextProvider.tsx
@@ -39,6 +39,7 @@ import {
canMoveUp,
findLocalLocation as findLocalLocationUtil,
findLocationById,
+ findLocationContainingPath,
getDirSeparatorForLocation,
getFirstReadWriteLocation,
getLocationPathString,
@@ -67,6 +68,7 @@ type CurrentLocationContextData = {
persistTagsInSidecarFile: boolean;
getLocationPath: (location: CommonLocation) => Promise<string>;
findLocation: (locationID?: string) => CommonLocation | undefined;
+ findLocationByPath: (entryPath: string) => CommonLocation | undefined;
getDirSeparator: (locationID?: string) => string;
findLocalLocation: () => CommonLocation;
changeLocation: (location: CommonLocation, skipInitDirList?: boolean) => void;
@@ -109,6 +111,7 @@ export const CurrentLocationContext = createContext<CurrentLocationContextData>(
persistTagsInSidecarFile: true,
getLocationPath: undefined,
findLocation: undefined,
+ findLocationByPath: undefined,
getDirSeparator: undefined,
findLocalLocation: undefined,
changeLocation: () => {},
@@ -295,6 +298,10 @@ export const CurrentLocationContextProvider = ({
);
}
+ function findLocationByPath(entryPath: string): CommonLocation | undefined {
+ return findLocationContainingPath(allLocations.current, entryPath);
+ }
+
function getDirSeparator(locationID?: string): string {
const loc = findLocation(locationID);
return getDirSeparatorForLocation(loc);
@@ -565,6 +572,7 @@ export const CurrentLocationContextProvider = ({
persistTagsInSidecarFile,
getLocationPath,
findLocation,
+ findLocationByPath,
getDirSeparator,
findLocalLocation,
changeLocation,
diff --git a/src/renderer/hooks/OpenedEntryContextProvider.tsx b/src/renderer/hooks/OpenedEntryContextProvider.tsx
index 3e0aa502b..0dbb7dc1d 100644
--- a/src/renderer/hooks/OpenedEntryContextProvider.tsx
+++ b/src/renderer/hooks/OpenedEntryContextProvider.tsx
@@ -164,8 +164,13 @@ export const OpenedEntryContextProvider = ({
const dispatch: AppDispatch = useDispatch();
const { t } = useTranslation();
- const { findLocation, openLocation, getLocationPath, getFirstRWLocation } =
- useCurrentLocationContext();
+ const {
+ findLocation,
+ findLocationByPath,
+ findLocalLocation,
+ openLocation,
+ getLocationPath,
+ } = useCurrentLocationContext();
const { getMetadata } = useIOActionsContext();
const {
currentDirectoryPath,
@@ -209,7 +214,12 @@ export const OpenedEntryContextProvider = ({
}, 1000);
} else if (cmdOpen) {
setTimeout(() => {
- openLink('ts://?cmdopen=' + cmdOpen, { fullWidth: true });
+ // Re-encode: cmdOpen is an already-decoded absolute path and may
+ // contain characters (#, &, ?, =, spaces) that would otherwise break
+ // URL parsing in parseTsLink and truncate the path.
+ openLink('ts://?cmdopen=' + encodeURIComponent(cmdOpen), {
+ fullWidth: true,
+ });
}, 1000);
}
}
@@ -734,10 +744,40 @@ export const OpenedEntryContextProvider = ({
};
if (parsed.kind === 'cmd' && parsed.cmdOpen) {
- const locationId = parsed.lid || getFirstRWLocation()?.uuid;
- getAllPropertiesPromise(parsed.cmdOpen, locationId)
+ // A file/folder handed over by the OS (Finder double-click / CLI) is an
+ // absolute path with no location id. Resolve it to a configured location
+ // so the file manager has a current location + directory to render into
+ // — without this the entry is loaded but there is no view to show it in
+ // (empty window). Prefer an explicit lid, then the location that
+ // contains the path, then the first writable location as a fallback.
+ const cmdPath = parsed.cmdOpen;
+ // A cmd/Finder path is always a local filesystem path, so it can only be
+ // hosted by a *local* location (a cloud S3/WebDAV location can't read a
+ // local absolute path). Prefer the location that contains the path, then
+ // any local location as a generic IO host. An explicit lid is honored
+ // as-is to allow cloud targets passed deliberately via the CLI.
+ const location =
+ (parsed.lid && findLocation(parsed.lid)) ||
+ findLocationByPath(cmdPath) ||
+ findLocalLocation();
+ if (!location) {
+ showNotification(t('core:noLocationToOpenFile'), 'warning', true);
+ return;
+ }
+ getAllPropertiesPromise(cmdPath, location.uuid)
.then((fsEntry: TS.FileSystemEntry) => {
- if (fsEntry) {
+ if (!fsEntry) {
+ showNotification(t('Missing file or folder'), 'warning', true);
+ return true;
+ }
+ if (!currentLocation || currentLocation.uuid !== location.uuid) {
+ openLocation(location, true);
+ }
+ const sep = location.getDirSeparator();
+ const dirPath = fsEntry.isFile
+ ? extractContainingDirectoryPath(fsEntry.path, sep)
+ : fsEntry.path;
+ return openDirectory(dirPath, undefined, location).then(() => {
if (fsEntry.isFile) {
openFsEntry(
fsEntry,
@@ -746,17 +786,13 @@ export const OpenedEntryContextProvider = ({
setSelectedEntries([fsEntry]);
setEntryInFullWidth(options.fullWidth);
} else {
- // Navigate the file manager AND also set the folder as the
- // opened entry so the details panel (description tab, etc.)
- // refreshes to the new folder. Matches the behavior of the
- // 'ts' kind path for folder targets.
- openDirectory(fsEntry.path).then(() => {
- openEntry(fsEntry);
- return true;
- });
+ // Folder target: navigation above already listed it; also set it
+ // as the opened entry so the details panel refreshes. Matches the
+ // behavior of the 'ts' kind path for folder targets.
+ openEntry(fsEntry);
}
- }
- return true;
+ return true;
+ });
})
.catch(() => {
showNotification(t('Missing file or folder'), 'warning', true);
diff --git a/src/renderer/locales/en/core.json b/src/renderer/locales/en/core.json
index 5cb66cc07..62b7482bb 100644
--- a/src/renderer/locales/en/core.json
+++ b/src/renderer/locales/en/core.json
@@ -568,6 +568,7 @@
"userName": "Username",
"openLink": "Open Link",
"invalidLink": "Invalid Link",
+ "noLocationToOpenFile": "Cannot open the file - please add a local location first.",
"link": "Link",
"desktopFolder": "Desktop",
"documentsFolder": "Documents",
diff --git a/src/renderer/utils/locationUtils.ts b/src/renderer/utils/locationUtils.ts
index 1fd568d58..223bf5bce 100644
--- a/src/renderer/utils/locationUtils.ts
+++ b/src/renderer/utils/locationUtils.ts
@@ -49,6 +49,41 @@ export function getFirstReadWriteLocation(
);
}
+/**
+ * Find the (local) location whose root path contains the given absolute path.
+ * Used when opening a file handed over by the OS (Finder double-click / CLI)
+ * where we only have an absolute filesystem path and no location id.
+ * Returns the deepest matching location (longest path prefix) so nested
+ * locations resolve to the most specific one.
+ * @param locations - Array of locations to search
+ * @param entryPath - Absolute filesystem path of the file/folder
+ * @returns Matching local location, or undefined if none contains the path
+ */
+export function findLocationContainingPath(
+ locations: CommonLocation[],
+ entryPath: string,
+): CommonLocation | undefined {
+ if (!locations || !entryPath) return undefined;
+ const { locationType } = require('@tagspaces/tagspaces-common/misc');
+ const norm = (p: string) => (p || '').replace(/\\/g, '/').replace(/\/+$/, '');
+ const target = norm(entryPath);
+ let match: CommonLocation | undefined;
+ let matchLen = -1;
+ for (const location of locations) {
+ if (location.type !== locationType.TYPE_LOCAL) continue;
+ const root = norm(location.path);
+ if (!root) continue;
+ if (
+ (target === root || target.startsWith(root + '/')) &&
+ root.length > matchLen
+ ) {
+ match = location;
+ matchLen = root.length;
+ }
+ }
+ return match;
+}
+
/**
* Find location by type
* @param locations - Array of locations to search