Skip to content

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

Add-check-for-valid-names-in-the-rename-field-of-the-entry-properties (#1751)

Stanimir947092293d98
diff --git a/app/components/EntryProperties.tsx b/app/components/EntryProperties.tsx
index fb2338541..535df04ea 100644
--- a/app/components/EntryProperties.tsx
+++ b/app/components/EntryProperties.tsx
@@ -61,7 +61,12 @@ import AppConfig from '@tagspaces/tagspaces-platforms/AppConfig';
import TagDropContainer from './TagDropContainer';
import MoveCopyFilesDialog from './dialogs/MoveCopyFilesDialog';
import i18n from '../services/i18n';
-import { enhanceOpenedEntry, convertMarkDown } from '-/services/utils-io';
+import {
+ enhanceOpenedEntry,
+ convertMarkDown,
+ fileNameValidation,
+ dirNameValidation
+} from '-/services/utils-io';
import { parseGeoLocation } from '-/utils/geo';
import { Pro } from '../pro';
import PlatformIO from '../services/platform-facade';
@@ -87,6 +92,7 @@ import InfoIcon from '-/components/InfoIcon';
import { ProTooltip } from '-/components/HelperComponents';
import PerspectiveSelector from '-/components/PerspectiveSelector';
import { connect } from 'react-redux';
+import FormHelperText from '@mui/material/FormHelperText';
const ThumbnailChooserDialog =
Pro && Pro.UI ? Pro.UI.ThumbnailChooserDialog : false;
@@ -178,6 +184,8 @@ function EntryProperties(props: Props) {
const sharingLinkRef = useRef<HTMLInputElement>(null);
const objectStorageLinkRef = useRef<HTMLInputElement>(null);
const fileDescriptionRef = useRef<HTMLInputElement>(null);
+ const disableConfirmButton = useRef<boolean>(true);
+ const fileNameError = useRef<boolean>(false);
const directoryPath = props.openedEntry.isFile
? extractContainingDirectoryPath(
@@ -288,6 +296,7 @@ function EntryProperties(props: Props) {
const deactivateEditNameField = () => {
setEditName(undefined);
+ fileNameError.current = false;
if (fileNameRef) {
fileNameRef.current.value = entryName;
}
@@ -435,6 +444,17 @@ function EntryProperties(props: Props) {
const { value, name } = target;
if (name === 'name') {
+ const initValid = disableConfirmButton.current;
+ let noValid;
+ if (currentEntry.isFile) {
+ noValid = fileNameValidation(value);
+ } else {
+ noValid = dirNameValidation(value);
+ }
+ disableConfirmButton.current = noValid;
+ if (noValid || initValid !== noValid) {
+ fileNameError.current = noValid;
+ }
setEditName(value);
}
};
@@ -620,6 +640,7 @@ function EntryProperties(props: Props) {
<Grid container>
<Grid item xs={12}>
<TextField
+ error={fileNameError.current}
label={
currentEntry.isFile
? i18n.t('core:fileName')
@@ -645,6 +666,7 @@ function EntryProperties(props: Props) {
data-tid="confirmRenameEntryTID"
color="primary"
onClick={renameEntry}
+ disabled={disableConfirmButton.current}
>
{i18n.t('core:confirmSaveButton')}
</Button>
@@ -685,6 +707,14 @@ function EntryProperties(props: Props) {
}}
onChange={handleFileNameChange}
/>
+ {fileNameError.current && (
+ <FormHelperText>
+ {i18n.t(
+ 'core:' +
+ (currentEntry.isFile ? 'fileNameHelp' : 'directoryNameHelp')
+ )}
+ </FormHelperText>
+ )}
</Grid>
<Grid item xs={12} style={{ marginTop: 10 }}>
<TagDropContainer entryPath={currentEntry.path}>
diff --git a/app/components/dialogs/RenameEntryDialog.tsx b/app/components/dialogs/RenameEntryDialog.tsx
index 01f992a1d..9e4e5cb51 100644
--- a/app/components/dialogs/RenameEntryDialog.tsx
+++ b/app/components/dialogs/RenameEntryDialog.tsx
@@ -37,6 +37,7 @@ import i18n from '-/services/i18n';
import { actions as AppActions, getLastSelectedEntry } from '-/reducers/app';
import PlatformIO from '-/services/platform-facade';
import DialogCloseButton from '-/components/dialogs/DialogCloseButton';
+import { dirNameValidation, fileNameValidation } from '-/services/utils-io';
interface Props {
open: boolean;
@@ -119,25 +120,16 @@ function RenameEntryDialog(props: Props) {
const handleValidation = () => {
const initValid = disableConfirmButton.current;
- if (name.current.length > 0) {
- const rg1 = /^[^#\\/*?"<>|]+$/; // forbidden characters # \ / * ? " < > |
- if (isFile) {
- // https://stackoverflow.com/a/11101624/2285631
- const rg2 = /^\./; // cannot start with dot (.)
- const rg3 = /^(nul|prn|con|lpt[0-9]|com[0-9])(\.|$)/i; // forbidden file names
- disableConfirmButton.current = !(
- rg1.test(name.current) &&
- !rg2.test(name.current) &&
- !rg3.test(name.current)
- );
- } else disableConfirmButton.current = !rg1.test(name.current);
- setInputError(disableConfirmButton.current);
+ let noValid;
+ if (isFile) {
+ noValid = fileNameValidation(name.current);
} else {
- disableConfirmButton.current = true;
+ noValid = dirNameValidation(name.current);
}
- if (initValid !== disableConfirmButton.current) {
- setInputError(disableConfirmButton.current);
- forceUpdate();
+ disableConfirmButton.current = noValid;
+
+ if (noValid || initValid !== noValid) {
+ setInputError(noValid);
}
};
diff --git a/app/services/utils-io.ts b/app/services/utils-io.ts
index 6472392ab..20e1c73c2 100644
--- a/app/services/utils-io.ts
+++ b/app/services/utils-io.ts
@@ -1187,3 +1187,26 @@ export function convertMarkDown(mdContent: string, directoryPath: string) {
return marked.parse(DOMPurify.sanitize(mdContent));
}
+
+/**
+ * forbidden characters # \ / * ? " < > |
+ * @param dirName
+ */
+export function dirNameValidation(dirName): boolean {
+ if (dirName.length > 0) {
+ const rg1 = /^[^#\\/*?"<>|]+$/; // forbidden characters # \ / * ? " < > |
+ return !rg1.test(dirName);
+ }
+ return true;
+}
+
+export function fileNameValidation(fileName): boolean {
+ if (fileName.length > 0) {
+ const rg1 = /^[^#\\/*?"<>|]+$/;
+ // https://stackoverflow.com/a/11101624/2285631
+ const rg2 = /^\./; // cannot start with dot (.)
+ const rg3 = /^(nul|prn|con|lpt[0-9]|com[0-9])(\.|$)/i; // forbidden file names
+ return !(rg1.test(fileName) && !rg2.test(fileName) && !rg3.test(fileName));
+ }
+ return true;
+}