mirror of
https://github.com/djdevin/recflare.git
synced 2026-09-08 06:31:27 -07:00
inventions...almost working
This commit is contained in:
@@ -36,6 +36,20 @@ function subfolderForFileType(fileType: string): string | undefined {
|
||||
return UPLOAD_SUBFOLDER[Number.parseInt(fileType, 10)]
|
||||
}
|
||||
|
||||
/**
|
||||
* The file extension an upload of a given type keeps. Invention data blobs are
|
||||
* named `<name>.inv` — the client expects the extension on the `BlobName` it later
|
||||
* gets back from the api worker, so it has to be part of the stored key too, or the
|
||||
* blob wouldn't be there to download. Other types are stored under a bare name.
|
||||
*/
|
||||
const UPLOAD_EXTENSION: Record<number, string> = {
|
||||
5: '.inv',
|
||||
}
|
||||
|
||||
function extensionForFileType(fileType: string): string {
|
||||
return UPLOAD_EXTENSION[Number.parseInt(fileType, 10)] ?? ''
|
||||
}
|
||||
|
||||
/** Read a text form field by any of its accepted names, matched case-insensitively. */
|
||||
function textField(body: Record<string, unknown>, ...names: string[]): string | undefined {
|
||||
for (const [key, value] of Object.entries(body)) {
|
||||
@@ -80,16 +94,18 @@ const app = new Hono<App>()
|
||||
const file = Object.values(body).find((v): v is File => v instanceof File)
|
||||
|
||||
if (file) {
|
||||
const subfolder = subfolderForFileType(textField(body, 'filetype') ?? '0')
|
||||
const fileType = textField(body, 'filetype') ?? '0'
|
||||
const subfolder = subfolderForFileType(fileType)
|
||||
if (subfolder === undefined) {
|
||||
// makeUploadName == "" → no destination for an unknown/missing type.
|
||||
return c.json({ error: 'missing or unknown FileType' }, 400)
|
||||
}
|
||||
// Folder each upload under its date (e.g. `room/2026-02-03/<uuid>`) so the
|
||||
// bucket stays browsable. The date is part of the returned name, so the key
|
||||
// the `cdn` worker reads back (`<subfolder>/<name>`) still round-trips.
|
||||
// the `cdn` worker reads back (`<subfolder>/<name>`) still round-trips — as
|
||||
// does the extension, which is why it goes on the key, not just the name.
|
||||
const datePrefix = new Date().toISOString().slice(0, 10)
|
||||
const filename = `${datePrefix}/${crypto.randomUUID()}`
|
||||
const filename = `${datePrefix}/${crypto.randomUUID()}${extensionForFileType(fileType)}`
|
||||
await c.env.CDN_ASSETS.put(`${subfolder}/${filename}`, await file.arrayBuffer(), {
|
||||
httpMetadata: { contentType: file.type || 'application/octet-stream' },
|
||||
})
|
||||
|
||||
@@ -103,6 +103,25 @@ it('POST /upload folders each FileType under its own subfolder', async () => {
|
||||
}
|
||||
})
|
||||
|
||||
it('POST /upload names an Invention (FileType 5) upload with the .inv extension', async () => {
|
||||
// The client expects the `.inv` extension on the BlobName it later reads back from
|
||||
// the api worker, so the extension has to be on the stored key too — otherwise the
|
||||
// cdn worker would have nothing to serve at that name.
|
||||
const bytes = new Uint8Array([0x49, 0x4e, 0x56])
|
||||
const res = await SELF.fetch(`${ORIGIN}/upload`, {
|
||||
method: 'POST',
|
||||
headers: await bearer(),
|
||||
body: uploadForm('5', bytes),
|
||||
})
|
||||
expect(res.status).toBe(200)
|
||||
const { filename } = (await res.json()) as { filename: string }
|
||||
expect(filename).toMatch(/^\d{4}-\d{2}-\d{2}\/[0-9a-f-]{36}\.inv$/)
|
||||
|
||||
const stored = await env.CDN_ASSETS.get(`invention/${filename}`)
|
||||
expect(stored).not.toBeNull()
|
||||
expect(new Uint8Array(await stored!.arrayBuffer())).toEqual(bytes)
|
||||
})
|
||||
|
||||
it('POST /upload 400s for a binary with an unknown/missing FileType', async () => {
|
||||
// Unknown type (999) and the Unknown enum value (0) have no destination → 400.
|
||||
for (const fileType of ['999', '0']) {
|
||||
|
||||
Reference in New Issue
Block a user