Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 6adb2c6

Browse files
authored
FF-472 remove leading backslashes when needed (#185)
1 parent e11eec7 commit 6adb2c6

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

src/background/methods/filesystem.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@ export const getPathWithoutName = (
88
return pathWithName.substr(0, pathWithName.lastIndexOf(name.toLowerCase()));
99
};
1010

11-
1211
export const removeTrailingBackslash = (path: string): string => {
1312
if (path.lastIndexOf("/") + 1 === path.length) {
1413
return path.substr(0, path.length - 1);
1514
}
1615
return path;
1716
};
1817

18+
export const removeLeadingBackslash = (path: string): string => {
19+
if (path.indexOf("/") === 0) {
20+
return path.substr(1, path.length);
21+
}
22+
return path;
23+
};
1924

2025
export const isFsEntityInFolder = (fsEntity: FsEntity, path: string) => {
2126
let fsEntityPath = getPathWithoutName(fsEntity.path, fsEntity.name);
@@ -29,8 +34,10 @@ export const getFileExtension = (fileName: string): string => {
2934
return reverseString(reverseString(fileName).substr(0, positionOfPoint));
3035
};
3136

32-
export const isFileNameValid = (name: string) =>{
33-
return !(!name ||
37+
export const isFileNameValid = (name: string) => {
38+
return !(
39+
!name ||
3440
name.includes("/") ||
35-
name.match("[~#@*+:!?&%<>|\"^\\\\]"))
36-
}
41+
name.match('[~#@*+:!?&%<>|"^\\\\]')
42+
);
43+
};

src/components/pages/filesytem/upload/UploadZone.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import {
1616
import { FsEntity } from "../../../../background/api/filesystemTypes";
1717
import { UploadDecisionsModalContent } from "./UploadDecisionsModalContent";
1818
import { divideArrayByCondition } from "../../../../background/methods/dataTypes/arrays";
19-
import { getPathWithoutName, isFileNameValid } from "../../../../background/methods/filesystem";
19+
import {
20+
getPathWithoutName,
21+
isFileNameValid,
22+
removeLeadingBackslash
23+
} from "../../../../background/methods/filesystem";
2024
import {
2125
EditableEntityError,
2226
EditableFileWithPreflightInfo,
@@ -202,7 +206,6 @@ export const preflightResultReducer: Reducer<
202206
(e) => e.path === action.payload.path
203207
);
204208
let oldPath = elementToReplace.newPath ?? elementToReplace.path;
205-
206209
elementToReplace.newPath =
207210
oldPath.substring(
208211
0,
@@ -249,14 +252,18 @@ export const preflightResultReducer: Reducer<
249252
elementToReplace.prefNewName ?? elementToReplace.name;
250253
} else {
251254
console.log(
255+
"PREFLIGHT_UPDATE_NAME",
252256
elementToReplace.newPath,
253257
elementToReplace.prefNewPath,
254-
elementToReplace.path
258+
elementToReplace.path,
259+
elementToReplace.newName
255260
);
256261
let prevOldPath =
257-
elementToReplace.prefNewPath ?? elementToReplace.path;
262+
elementToReplace.prefNewPath ??
263+
removeLeadingBackslash(elementToReplace.path);
258264
restOfElements = restOfElements.map((e) => {
259-
let currentPath = e.newPath ?? e.path;
265+
let currentPath =
266+
e.newPath ?? removeLeadingBackslash(e.path);
260267
let currentName = e.newName ?? e.name;
261268
let currentPathWithoutName = getPathWithoutName(
262269
currentPath,
@@ -265,14 +272,19 @@ export const preflightResultReducer: Reducer<
265272

266273
let index = currentPathWithoutName.indexOf(prevOldPath);
267274

275+
let pathNeedsChange = index === 0;
268276
console.log(
277+
"PREFLIGHT_UPDATE_NAME",
269278
prevOldPath,
270279
currentPathWithoutName,
271280
currentName
272281
);
273-
if (index === 0) {
282+
if (pathNeedsChange) {
274283
e.newPath =
275-
newPath + currentPath.substr(prevOldPath?.length);
284+
newPath +
285+
removeLeadingBackslash(currentPath).substr(
286+
prevOldPath?.length
287+
);
276288
e.prefNewPath = e.newPath;
277289
}
278290
return e;
Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
1-
import { EditablePreflightEntityOrFile, PreflightEntity } from "./preflightTypes";
1+
import {
2+
EditablePreflightEntityOrFile,
3+
PreflightEntity
4+
} from "./preflightTypes";
25

3-
export const preflightResultCombine = (files: EditablePreflightEntityOrFile[], response: PreflightEntity[]) => {
6+
export const preflightResultCombine = (
7+
files: EditablePreflightEntityOrFile[],
8+
response: PreflightEntity[]
9+
) => {
10+
console.log("[preflightResultCombine] files", files, "response", response);
11+
return files.map((file: EditablePreflightEntityOrFile) => {
12+
const resInfo = response.find(
13+
(e) =>
14+
e.path === (file.newPath ?? file.path) ||
15+
"/" + e.path === (file.newPath ?? file.path)
16+
);
17+
// could be done better if rest returned paths with leading slashes
18+
if (!resInfo) {
19+
console.error("Did not found a response for the file:", file);
20+
}
421

5-
return files.map((file: EditablePreflightEntityOrFile) => {
6-
console.log(response)
7-
const resInfo = response.find((e) => (e.path === (file.newPath ?? file.path)) || ("/" + e.path === (file.newPath ?? file.path)));
8-
// could be done better if rest returned paths with leading slashes
9-
if (!resInfo){console.error("Did not found a response for the file:",file)}
22+
file.permissionIsSufficient = resInfo?.permissionIsSufficient ?? true;
23+
file.nameAlreadyInUse = resInfo?.nameAlreadyInUse ?? false;
24+
file.nameIsValid = resInfo?.nameIsValid ?? true;
25+
file.isFile = resInfo?.isFile ?? false;
1026

11-
file.permissionIsSufficient =
12-
resInfo?.permissionIsSufficient ?? true;
13-
file.nameAlreadyInUse = resInfo?.nameAlreadyInUse ?? false;
14-
file.nameIsValid = resInfo?.nameIsValid ?? true;
15-
file.isFile = resInfo?.isFile ?? true;
16-
17-
return file;
18-
});
27+
return file;
28+
});
1929
};

0 commit comments

Comments
 (0)