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 history from "../../../../../../history";
import { decodeFileName, encodeFileName } from "../../../../../../common/utils";
import { setModalErrorSnackMessage } from "../../../../../../actions";
import { BucketObject } from "./types";
interface ICreateFolder {
classes: any;
@@ -33,7 +35,9 @@ interface ICreateFolder {
bucketName: string;
folderName: string;
setFileModeEnabled: typeof setFileModeEnabled;
setModalErrorSnackMessage: typeof setModalErrorSnackMessage;
onClose: () => any;
existingFiles: BucketObject[];
}
const styles = (theme: Theme) =>
@@ -54,7 +58,9 @@ const CreateFolderModal = ({
bucketName,
onClose,
setFileModeEnabled,
setModalErrorSnackMessage,
classes,
existingFiles,
}: ICreateFolder) => {
const [pathUrl, setPathUrl] = useState("");
const [isFormValid, setIsFormValid] = useState<boolean>(false);
@@ -73,6 +79,15 @@ const CreateFolderModal = ({
? 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(
`${folderPath}${pathUrl}`
)}/`;
@@ -138,6 +153,7 @@ const CreateFolderModal = ({
const mapDispatchToProps = {
setFileModeEnabled,
setModalErrorSnackMessage,
};
const connector = connect(null, mapDispatchToProps);

View File

@@ -490,7 +490,6 @@ const ListObjects = ({
? decodedPath
: decodedPath + "/";
}
api
.invoke(
"GET",
@@ -531,10 +530,23 @@ const ListObjects = ({
setFileModeEnabled(false);
setLoading(false);
} 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 (
res.objects.length === 1 &&
res.objects[0].name.endsWith("/")
(res.objects.length === 1 &&
res.objects[0].name.endsWith("/")) ||
!found
) {
setFileModeEnabled(false);
} else {
@@ -1045,6 +1057,7 @@ const ListObjects = ({
bucketName={bucketName}
folderName={internalPaths}
onClose={closeAddFolderModal}
existingFiles={records}
/>
)}
{rewindSelect && (