Skip to content

Commit

Permalink
Slugify upload paths correctly, but keep the original file name aroun…
Browse files Browse the repository at this point in the history
…d in the entry title
  • Loading branch information
benmerckx committed Jan 31, 2024
1 parent eac8b49 commit 0c30a5c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/dashboard/hook/UseUploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
EntryPhase,
EntryRow,
HttpError,
Workspace
Workspace,
slugify
} from 'alinea/core'
import {entryFileName, entryFilepath} from 'alinea/core/EntryFilenames'
import {createId} from 'alinea/core/Id'
Expand Down Expand Up @@ -161,7 +162,9 @@ async function process(
}
case UploadStatus.Uploading: {
const fileName = upload.file.name
const file = join(upload.to.directory, fileName)
const extension = extname(fileName)
const path = slugify(basename(fileName, extension))
const file = join(upload.to.directory, path + extension)
const info = await client.prepareUpload(file)
await fetch(info.upload.url, {
method: info.upload.method ?? 'POST',
Expand Down Expand Up @@ -244,9 +247,10 @@ export function useUploads(onSelect?: (entry: EntryRow) => void) {
}
})
)

const extension = extname(upload.file.name)
const path = slugify(basename(upload.file.name, extension))
const prev = await graph.preferPublished.maybeGet(Entry({parent: parentId}))
const extension = extname(upload.file.name.toLowerCase())
const path = basename(upload.file.name.toLowerCase(), extension)

const entryLocation = {
workspace: upload.to.workspace,
Expand All @@ -270,13 +274,14 @@ export function useUploads(onSelect?: (entry: EntryRow) => void) {
: location

const hash = await createFileHash(new Uint8Array(buffer))
const title = basename(upload.file.name, extension)
const entry = await createEntryRow<Media.File>(config, {
...entryLocation,
parent: parent?.entryId ?? null,
entryId: entryId,
type: 'MediaFile',
url: (parent ? parent.url : '') + '/' + path,
title: basename(path, extension),
title,
seeded: null,
modifiedAt: Date.now(),
searchableText: '',
Expand All @@ -290,7 +295,7 @@ export function useUploads(onSelect?: (entry: EntryRow) => void) {
active: true,
main: true,
data: {
title: basename(path, extension),
title,
location: fileLocation,
extension: extension,
size: buffer.byteLength,
Expand Down

0 comments on commit 0c30a5c

Please sign in to comment.