Skip to content

Commit

Permalink
fix: prevent removal of last character on no filename extension (#120)
Browse files Browse the repository at this point in the history
* fix: prevent removal of last character on no filename extension

Fixes sanity-io/client#98

If a file is uploaded with an original filename that does not include an extension (`.png` or similar), the `getFilenameWithoutExtension` method will remove the last character of the filename, because it uses the last index of `.`, which is not found, resulting in `-1`.

* fix: simplify filename inputs, trim before validation

---------

Co-authored-by: Robin Pyon <[email protected]>
  • Loading branch information
rexxars and robinpyon authored Feb 3, 2023
1 parent 7df3800 commit 3c0db14
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 64 deletions.
19 changes: 5 additions & 14 deletions src/components/DialogAssetEdit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import AssetMetadata from '../AssetMetadata'
import Dialog from '../Dialog'
import DocumentList from '../DocumentList'
import FileAssetPreview from '../FileAssetPreview'
import FormFieldInputFilename from '../FormFieldInputFilename'
import FormFieldInputTags from '../FormFieldInputTags'
import FormFieldInputText from '../FormFieldInputText'
import FormFieldInputTextarea from '../FormFieldInputTextarea'
Expand All @@ -35,14 +34,9 @@ type Props = {
type FormData = yup.InferType<typeof formSchema>

const formSchema = yup.object().shape({
originalFilename: yup.string().required('Filename cannot be empty')
originalFilename: yup.string().trim().required('Filename cannot be empty')
})

const getFilenameWithoutExtension = (asset?: Asset): string | undefined => {
const extensionIndex = asset?.originalFilename?.toLowerCase().lastIndexOf(`.${asset.extension}`)
return asset?.originalFilename?.slice(0, extensionIndex)
}

const DialogAssetEdit = (props: Props) => {
const {
children,
Expand Down Expand Up @@ -73,7 +67,7 @@ const DialogAssetEdit = (props: Props) => {
const generateDefaultValues = (asset?: Asset) => ({
altText: asset?.altText || '',
description: asset?.description || '',
originalFilename: asset ? getFilenameWithoutExtension(asset) : undefined,
originalFilename: asset?.originalFilename || '',
opt: {media: {tags: assetTagOptions}},
title: asset?.title || ''
})
Expand Down Expand Up @@ -165,9 +159,7 @@ const DialogAssetEdit = (props: Props) => {
_weak: true
})) || null
}
},
// Append extension to filename
originalFilename: `${sanitizedFormData.originalFilename}.${assetItem?.asset.extension}`
}
}
})
)
Expand Down Expand Up @@ -323,14 +315,13 @@ const DialogAssetEdit = (props: Props) => {
value={assetTagOptions}
/>
{/* Filename */}
<FormFieldInputFilename
<FormFieldInputText
disabled={formUpdating}
error={errors?.originalFilename}
extension={currentAsset?.extension || ''}
label="Filename"
name="originalFilename"
ref={register}
value={getFilenameWithoutExtension(currentAsset)}
value={currentAsset?.originalFilename}
/>
{/* Title */}
<FormFieldInputText
Expand Down
50 changes: 0 additions & 50 deletions src/components/FormFieldInputFilename/index.tsx

This file was deleted.

0 comments on commit 3c0db14

Please sign in to comment.