Skip to content

Commit

Permalink
Add file extension if upload servers don't return them
Browse files Browse the repository at this point in the history
  • Loading branch information
cesardeazevedo committed Oct 30, 2024
1 parent 05beafa commit 1eca761
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
4 changes: 0 additions & 4 deletions src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ describe('parseNote()', () => {
"attrs": {
"alt": null,
"file": null,
"hash": null,
"sha256": null,
"src": "http://host.com/image",
"tags": null,
Expand Down Expand Up @@ -229,7 +228,6 @@ describe('parseNote()', () => {
"attrs": {
"alt": null,
"file": null,
"hash": null,
"sha256": null,
"src": "https://nostr.com/img.jpg",
"tags": null,
Expand Down Expand Up @@ -483,7 +481,6 @@ https://host.com/2.jpeg
"attrs": {
"alt": null,
"file": null,
"hash": null,
"sha256": null,
"src": "https://host.com/1.jpeg",
"tags": null,
Expand All @@ -506,7 +503,6 @@ https://host.com/2.jpeg
"attrs": {
"alt": null,
"file": null,
"hash": null,
"sha256": null,
"src": "https://host.com/2.jpeg",
"tags": null,
Expand Down
12 changes: 7 additions & 5 deletions src/__tests__/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ const res1 = {
url: `https://localhost:3000/${hash1}`,
type: 'image/png',
size: 21792,
tags: [],
}
const res2 = {
sha256: hash2,
url: `https://localhost:3000/${hash2}`,
type: 'image/png',
size: 16630,
tags: [],
}

const server = setupServer(
Expand Down Expand Up @@ -83,9 +85,9 @@ describe('FileUpload', () => {

const schema2 = editor.getJSON()
expect(schema2.content?.[1].attrs?.sha256).toStrictEqual(hash1)
expect(schema2.content?.[1].attrs?.src).toStrictEqual(`https://localhost:3000/${hash1}`)
expect(schema2.content?.[1].attrs?.src).toStrictEqual(`https://localhost:3000/${hash1}.png`)
expect(schema2.content?.[2].attrs?.sha256).toStrictEqual(hash2)
expect(schema2.content?.[2].attrs?.src).toStrictEqual(`https://localhost:3000/${hash2}`)
expect(schema2.content?.[2].attrs?.src).toStrictEqual(`https://localhost:3000/${hash2}.png`)

expect(spySign).toHaveBeenCalledTimes(2)
expect(spyHash).toHaveBeenCalledTimes(2)
Expand All @@ -97,7 +99,7 @@ describe('FileUpload', () => {
expect(spyComplete).toHaveBeenNthCalledWith(1, editor, files)

expect(editor.getText({ blockSeparator: ' ' })).toStrictEqual(
`GM! https://localhost:3000/${hash1} https://localhost:3000/${hash2}`,
`GM! https://localhost:3000/${hash1}.png https://localhost:3000/${hash2}.png`,
)
})

Expand All @@ -115,10 +117,10 @@ describe('FileUpload', () => {
await expect(editor.storage.fileUpload.uploader.start()).rejects.toStrictEqual(new Error('Error: Invalid file'))

const schema2 = editor.getJSON()
expect(schema2.content?.[2].attrs?.sha256).toBeNull()
expect(schema2.content?.[2].attrs?.uploadError).toStrictEqual('Error: Invalid file')
expect(schema2.content?.[1].attrs?.sha256).toStrictEqual(hash1)
expect(schema2.content?.[1].attrs?.uploadError).toBeNull()
expect(schema2.content?.[2].attrs?.sha256).toBeNull()
expect(schema2.content?.[2].attrs?.uploadError).toStrictEqual('Error: Invalid file')

expect(spyDrop).toHaveBeenCalledTimes(2)
expect(spyUpload).toHaveBeenCalledOnce()
Expand Down
16 changes: 13 additions & 3 deletions src/extensions/FileUploadExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ declare module '@tiptap/core' {
}
}

type FileAttributes = ImageAttributes | VideoAttributes

export interface FileUploadOptions {
allowedMimeTypes: string[]
expiration: number
Expand All @@ -29,7 +31,7 @@ export interface FileUploadOptions {
onStart: (currentEditor: Editor) => void
onUpload: (currentEditor: Editor, file: UploadTask) => void
onUploadError: (currentEditor: Editor, file: UploadTask) => void
onComplete: (currentEditor: Editor, files: UploadTask[]) => void
onComplete: (currentEditor: Editor, files: FileAttributes[]) => void
}

export interface FileUploadStorage {
Expand Down Expand Up @@ -193,10 +195,18 @@ class Uploader {
private onUploadDone(nodeRef: Node, response: UploadTask) {
this.findNodes(false).forEach(([node, pos]) => {
if (node.attrs.src === nodeRef.attrs.src) {
if (response.uploadError) {
this.updateNodeAttributes(pos, { uploading: false, uploadError: response.uploadError })
return
}
const file = nodeRef.attrs.file as File
const url = new URL(response.url)
const hasExtension = url.pathname.split('.').length === 2
this.updateNodeAttributes(pos, {
uploading: false,
tags: response.tags,
src: response.url,
// always append file extension if missing
src: response.url + (hasExtension ? '' : '.' + file.type.split('/')[1]),
sha256: response.sha256,
uploadError: response.uploadError,
})
Expand All @@ -218,7 +228,7 @@ class Uploader {
: await uploadBlossom({ file, serverUrl, hash, sign, expiration })
} catch (error) {
const msg = error?.toString() as string
res = { uploadError: msg }
res = { uploadError: msg } as UploadTask
}

this.onUploadDone(node, res)
Expand Down
2 changes: 0 additions & 2 deletions src/extensions/ImageExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export interface ImageAttributes {
src: string
alt: string
title: string
hash: string
file: File
tags: NostrEvent['tags']
sha256: string
Expand Down Expand Up @@ -55,7 +54,6 @@ export const ImageExtension = Image.extend<ImageOptions>({
return {
src: { default: null },
alt: { default: null },
hash: { default: null },
file: { default: null },
tags: { default: null },
sha256: { default: null },
Expand Down
2 changes: 1 addition & 1 deletion src/uploaders/blossom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ export async function uploadBlossom(options: BlossomOptions): Promise<UploadTask
const json = data as BlossomResponse
return {
...json,
tags: Array.from(Object.entries(json.nip94)),
tags: Array.from(Object.entries(json.nip94 || [])),
}
}
6 changes: 3 additions & 3 deletions src/uploaders/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export type UploadParams = {
}

export interface UploadTask {
url?: string
sha256?: string
tags?: NostrEvent['tags']
url: string
sha256: string
tags: NostrEvent['tags']
uploadError?: string
}

0 comments on commit 1eca761

Please sign in to comment.