Skip to content

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

fix Importing mac tags broken

sytolk0595b1afbf20
diff --git a/src/main/mainEvents.ts b/src/main/mainEvents.ts
index 2c42b1354..a77d6be93 100644
--- a/src/main/mainEvents.ts
+++ b/src/main/mainEvents.ts
@@ -49,6 +49,11 @@ import { ApiResponse } from './types';
const progress = {};
let wsc;
+function isSafePath(filePath) {
+ if (typeof filePath !== 'string') return false;
+ return true;
+}
+
export default function loadMainEvents() {
ipcMain.on('reloadWindow', () => {
const mainWindow = BrowserWindow.getAllWindows();
@@ -183,9 +188,12 @@ export default function loadMainEvents() {
}
return false;
});*/
+
ipcMain.handle('readMacOSTags', async (event, filename) => {
- const results = await readMacOSTags(filename);
- return results;
+ if (!isSafePath(filename)) {
+ throw new Error('Invalid filename');
+ }
+ return await readMacOSTags(filename);
});
ipcMain.on('ondragstart', (event, filePath) => {
// https://www.electronjs.org/docs/latest/api/web-contents#contentsstartdragitem
diff --git a/src/main/util.ts b/src/main/util.ts
index e5f1e8504..39a6a9730 100644
--- a/src/main/util.ts
+++ b/src/main/util.ts
@@ -5,7 +5,7 @@ import { BrowserWindow } from 'electron';
import webpackPaths from '../../.erb/configs/webpack.paths';
import fs from 'fs';
import chalk from 'chalk';
-import { execSync } from 'child_process';
+import { execFile, execSync } from 'child_process';
import http from 'http';
import settings from './settings';
@@ -283,47 +283,22 @@ export function ollamaPostRequest(
* @param filename
* @returns {Promise<TS.Tag[]>}
*/
-export function readMacOSTags(filename) {
- const cmdArr = [
- 'mdls',
- '-raw',
- '-name',
- 'kMDItemUserTags',
- '"' + filename + '"',
- ];
- const cmd = cmdArr.join(' ');
-
+export async function readMacOSTags(filename) {
return new Promise((resolve, reject) => {
- const foundTags = [];
- const { exec } = require('child_process');
- exec(cmd, (error, stdout, stderr) => {
- if (error) {
- console.error(error);
- reject(error);
- }
- if (stderr) {
- console.log(stderr);
- reject(stderr);
- }
- if (stdout && stdout !== '(null)') {
- stdout
- .toString()
- .replace(/^\(|\)$/g, '')
- .split(',')
- .map((item) => {
- const newTag = {
- // id: uuidv1(),
- title: item.trim(),
- };
- foundTags.push(newTag);
- return newTag;
- });
+ const args = ['-raw', '-name', 'kMDItemUserTags', filename];
+ execFile('mdls', args, (error, stdout, stderr) => {
+ if (error) return reject(error);
+ if (stderr) return reject(new Error(stderr));
- resolve(foundTags);
- // console.log('Tags in file "' + filename + '": ' + JSON.stringify(foundTags));
- } else {
- resolve(foundTags);
+ const text = stdout.trim();
+ if (!text || text === '(null)') {
+ return resolve([]);
}
+ const tags = text
+ .replace(/^\(|\)$/g, '')
+ .split(',')
+ .map((item) => ({ title: item.trim() }));
+ resolve(tags);
});
});
}
diff --git a/src/renderer/components/dialogs/ProgressDialog.tsx b/src/renderer/components/dialogs/ProgressDialog.tsx
index e92c4c1f6..f509e80bc 100644
--- a/src/renderer/components/dialogs/ProgressDialog.tsx
+++ b/src/renderer/components/dialogs/ProgressDialog.tsx
@@ -17,6 +17,7 @@
*/
import React from 'react';
+import { useTranslation } from 'react-i18next';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import Dialog from '@mui/material/Dialog';
@@ -26,32 +27,42 @@ import DialogCloseButton from '-/components/dialogs/DialogCloseButton';
interface Props {
open: boolean;
+ title?: string;
onClose: () => void;
}
function ProgressDialog(props: Props) {
+ const { t } = useTranslation();
+ const { open, onClose, title } = props;
return (
<Dialog
aria-labelledby="draggable-dialog-title"
PaperComponent={DraggablePaper}
- open={props.open}
- onClose={props.onClose}
+ open={open}
+ onClose={onClose}
>
<DialogTitle
style={{ cursor: 'move' }}
data-tid="progressDialogTitle"
id="draggable-dialog-title"
>
- <DialogCloseButton testId="closeProgressTID" onClose={props.onClose} />
+ {title && t(title)}
+ <DialogCloseButton testId="closeProgressTID" onClose={onClose} />
</DialogTitle>
<DialogContent
style={{
- marginLeft: 'auto',
- marginRight: 'auto',
- flexGrow: 1,
+ minWidth: 300,
+ display: 'flex',
+ justifyContent: 'center',
+ alignItems: 'center',
}}
>
- <CircularProgress size={24} />
+ <CircularProgress
+ size={24}
+ sx={{
+ display: 'contents',
+ }}
+ />
</DialogContent>
</Dialog>
);
diff --git a/src/renderer/components/dialogs/hooks/ProgressDialogContextProvider.tsx b/src/renderer/components/dialogs/hooks/ProgressDialogContextProvider.tsx
index 768118870..d97e40a17 100644
--- a/src/renderer/components/dialogs/hooks/ProgressDialogContextProvider.tsx
+++ b/src/renderer/components/dialogs/hooks/ProgressDialogContextProvider.tsx
@@ -20,7 +20,7 @@ import React, { createContext, useMemo, useReducer, useRef } from 'react';
import LoadingLazy from '-/components/LoadingLazy';
type ProgressDialogContextData = {
- openProgressDialog: () => void;
+ openProgressDialog: (title?: string) => void;
closeProgressDialog: () => void;
};
@@ -41,16 +41,19 @@ export const ProgressDialogContextProvider = ({
children,
}: ProgressDialogContextProviderProps) => {
const open = useRef<boolean>(false);
+ const title = useRef<string>(undefined);
const [ignored, forceUpdate] = useReducer((x) => x + 1, 0, undefined);
- function openDialog() {
+ function openDialog(t) {
open.current = true;
+ title.current = t;
forceUpdate();
}
function closeDialog() {
open.current = false;
+ title.current = undefined;
forceUpdate();
}
@@ -71,7 +74,11 @@ export const ProgressDialogContextProvider = ({
return (
<ProgressDialogContext.Provider value={context}>
- <ProgressDialogAsync open={open.current} onClose={closeDialog} />
+ <ProgressDialogAsync
+ open={open.current}
+ title={title.current}
+ onClose={closeDialog}
+ />
{children}
</ProgressDialogContext.Provider>
);
diff --git a/src/renderer/components/menus/DirectoryMenu.tsx b/src/renderer/components/menus/DirectoryMenu.tsx
index 6a3ed3587..5821f248c 100644
--- a/src/renderer/components/menus/DirectoryMenu.tsx
+++ b/src/renderer/components/menus/DirectoryMenu.tsx
@@ -84,7 +84,8 @@ function DirectoryMenu(props: Props) {
const { openEntry } = useOpenedEntryContext();
const { selectedEntries, setSelectedEntries } = useSelectedEntriesContext();
const { addTags } = useTaggingActionsContext();
- const { openProgressDialog } = useProgressDialogContext();
+ const { openProgressDialog, closeProgressDialog } =
+ useProgressDialogContext();
const { openNewFileDialog } = useNewFileDialogContext();
const { currentLocation, getLocationPath, findLocation } =
useCurrentLocationContext();
@@ -249,7 +250,7 @@ Do you want to continue?`)
) {
return false;
}
- openProgressDialog();
+ openProgressDialog('importingMacTags');
const entryCallback = (entry) => {
readMacOSTags(entry.path)
@@ -269,7 +270,7 @@ Do you want to continue?`)
entryCallback,
)
.then(() => {
- openProgressDialog();
+ closeProgressDialog();
console.log('Import tags succeeded ' + directoryPath);
showNotification(
'Tags from ' + directoryPath + ' are imported successfully.',
@@ -280,7 +281,7 @@ Do you want to continue?`)
})
.catch((err) => {
console.log('Error importing tags: ' + directoryPath, err);
- openProgressDialog();
+ closeProgressDialog();
});
} else {
showNotification(
diff --git a/src/renderer/locales/en/core.json b/src/renderer/locales/en/core.json
index 7703e4580..4de2c1e9d 100644
--- a/src/renderer/locales/en/core.json
+++ b/src/renderer/locales/en/core.json
@@ -973,5 +973,6 @@
"renameDisableTooltip": "Exit the file edit mode in order to rename it",
"confirmReindex": "Index not available",
"confirmReindexContent": "Do you want to create it now?",
+ "importingMacTags": "Importing MacOS tags...",
"-": "-"
}