Disallow folders to share the same name as existing files. (#1279)

This commit is contained in:
adfost
2021-12-07 12:33:30 -08:00
committed by GitHub
parent 35855daa12
commit a7ab26c81e
2 changed files with 33 additions and 4 deletions

View File

@@ -26,6 +26,8 @@ import { connect } from "react-redux";
import { setFileModeEnabled } from "../../../../ObjectBrowser/actions"; import { setFileModeEnabled } from "../../../../ObjectBrowser/actions";
import history from "../../../../../../history"; import history from "../../../../../../history";
import { decodeFileName, encodeFileName } from "../../../../../../common/utils"; import { decodeFileName, encodeFileName } from "../../../../../../common/utils";
import { setModalErrorSnackMessage } from "../../../../../../actions";
import { BucketObject } from "./types";
interface ICreateFolder { interface ICreateFolder {
classes: any; classes: any;
@@ -33,7 +35,9 @@ interface ICreateFolder {
bucketName: string; bucketName: string;
folderName: string; folderName: string;
setFileModeEnabled: typeof setFileModeEnabled; setFileModeEnabled: typeof setFileModeEnabled;
setModalErrorSnackMessage: typeof setModalErrorSnackMessage;
onClose: () => any; onClose: () => any;
existingFiles: BucketObject[];
} }
const styles = (theme: Theme) => const styles = (theme: Theme) =>
@@ -54,7 +58,9 @@ const CreateFolderModal = ({
bucketName, bucketName,
onClose, onClose,
setFileModeEnabled, setFileModeEnabled,
setModalErrorSnackMessage,
classes, classes,
existingFiles,
}: ICreateFolder) => { }: ICreateFolder) => {
const [pathUrl, setPathUrl] = useState(""); const [pathUrl, setPathUrl] = useState("");
const [isFormValid, setIsFormValid] = useState<boolean>(false); const [isFormValid, setIsFormValid] = useState<boolean>(false);
@@ -73,6 +79,15 @@ const CreateFolderModal = ({
? decodedFolderName ? decodedFolderName
: `${decodedFolderName}/`; : `${decodedFolderName}/`;
} }
const sharesName = (record: BucketObject) =>
record.name === folderPath + pathUrl;
if (existingFiles.findIndex(sharesName) !== -1) {
setModalErrorSnackMessage({
errorMessage: "Folder cannot have the same name as an existing file",
detailedError: "",
});
return;
}
const newPath = `/buckets/${bucketName}/browse/${encodeFileName( const newPath = `/buckets/${bucketName}/browse/${encodeFileName(
`${folderPath}${pathUrl}` `${folderPath}${pathUrl}`
)}/`; )}/`;
@@ -138,6 +153,7 @@ const CreateFolderModal = ({
const mapDispatchToProps = { const mapDispatchToProps = {
setFileModeEnabled, setFileModeEnabled,
setModalErrorSnackMessage,
}; };
const connector = connect(null, mapDispatchToProps); const connector = connect(null, mapDispatchToProps);

View File

@@ -490,7 +490,6 @@ const ListObjects = ({
? decodedPath ? decodedPath
: decodedPath + "/"; : decodedPath + "/";
} }
api api
.invoke( .invoke(
"GET", "GET",
@@ -531,10 +530,23 @@ const ListObjects = ({
setFileModeEnabled(false); setFileModeEnabled(false);
setLoading(false); setLoading(false);
} else { } else {
// This is an empty folder. // This code prevents the program from opening a file when a substring of that file is entered as a new folder.
// Previously, if there was a file test1.txt and the folder test was created with the same prefix, the program
// would open test1.txt instead
let found = false;
let pathPrefixChopped = pathPrefix.slice(
0,
pathPrefix.length - 1
);
for (let i = 0; i < res.objects.length; i++) {
if (res.objects[i].name === pathPrefixChopped) {
found = true;
}
}
if ( if (
res.objects.length === 1 && (res.objects.length === 1 &&
res.objects[0].name.endsWith("/") res.objects[0].name.endsWith("/")) ||
!found
) { ) {
setFileModeEnabled(false); setFileModeEnabled(false);
} else { } else {
@@ -1045,6 +1057,7 @@ const ListObjects = ({
bucketName={bucketName} bucketName={bucketName}
folderName={internalPaths} folderName={internalPaths}
onClose={closeAddFolderModal} onClose={closeAddFolderModal}
existingFiles={records}
/> />
)} )}
{rewindSelect && ( {rewindSelect && (