Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double slash when uploading a file #2106

Merged
merged 3 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ interface IUploadFileModuleProps {
const UploadFileModule = ({ modalOpen, close }: IUploadFileModuleProps) => {
const classes = useStyles()
const [isDoneDisabled, setIsDoneDisabled] = useState(true)
const { uploadFiles } = useFiles()
const { currentPath, refreshContents, bucket } = useFileBrowser()
const { storageSummary } = useFiles()
const { storageSummary, uploadFiles } = useFiles()

const UploadSchema = useMemo(() => object().shape({
files: array().required(t`Please select a file to upload`)
Expand All @@ -105,10 +104,13 @@ const UploadFileModule = ({ modalOpen, close }: IUploadFileModuleProps) => {

const onSubmit = useCallback(async (values: {files: Array<File & {path: string}>}, helpers) => {
if (!bucket) return

helpers.setSubmitting(true)

try {
close()
const paths = [...new Set(values.files.map(f => f.path.substring(0, f.path.lastIndexOf("/"))))]

paths.forEach(async p => {
const filesToUpload = values.files.filter((f => f.path.substring(0, f.path.lastIndexOf("/")) === p))
await uploadFiles(bucket, filesToUpload, getPathWithFile(currentPath, p))
Expand All @@ -124,17 +126,15 @@ const UploadFileModule = ({ modalOpen, close }: IUploadFileModuleProps) => {
const formik = useFormik({
initialValues: { files: [] },
validationSchema: UploadSchema,
onSubmit: onSubmit
onSubmit
})

return (
<CustomModal
active={modalOpen}
closePosition="none"
maxWidth="sm"
injectedClass={{
inner: classes.modalInner
}}
injectedClass={{ inner: classes.modalInner }}
>
<FormikProvider value={formik}>
<Form
Expand Down
13 changes: 10 additions & 3 deletions packages/files-ui/src/Utils/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ export function getURISafePathFromArray(arrayOfPaths: string[]): string {
// /path/to/somewhere + 1.txt -> /path/to/somewhere/1.txt
// /path/to/somewhere/ + 1.txt -> /path/to/somewhere/1.txt
export function getPathWithFile(path: string, fileName: string) {

// make sure the file name doesn't start with a /
// otherwise remove it
const nameToUse = fileName.startsWith("/")
? fileName.substring(1)
: fileName

return path === "/"
? `/${fileName}`
? `/${nameToUse}`
: path[path.length - 1] === "/"
? `${path}${fileName}`
: `${path}/${fileName}`
? `${path}${nameToUse}`
: `${path}/${nameToUse}`
}

// Removes a parent element
Expand Down