Skip to content

Commit

Permalink
Force file extension on blossom only
Browse files Browse the repository at this point in the history
  • Loading branch information
cesardeazevedo committed Oct 31, 2024
1 parent 1eca761 commit 1a7fdd7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
20 changes: 5 additions & 15 deletions src/extensions/FileUploadExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface FileUploadOptions {

export interface FileUploadStorage {
uploader: Uploader | null
getFiles: () => void
getFiles: () => FileAttributes[]
}

export function bufferToHex(buffer: ArrayBuffer) {
Expand Down Expand Up @@ -84,7 +84,7 @@ export const FileUploadExtension = Extension.create<FileUploadOptions, FileUploa
addStorage() {
return {
uploader: null,
getFiles() {},
getFiles: () => [],
}
},

Expand Down Expand Up @@ -167,7 +167,7 @@ class Uploader {
}

getFiles() {
return this.findNodes().map(([node]) => node.attrs as ImageAttributes | VideoAttributes)
return this.findNodes().map(([node]) => node.attrs as FileAttributes)
}

private findNodes(uploaded?: boolean) {
Expand Down Expand Up @@ -195,20 +195,10 @@ 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, {
...response,
src: response.url,
uploading: false,
tags: response.tags,
// always append file extension if missing
src: response.url + (hasExtension ? '' : '.' + file.type.split('/')[1]),
sha256: response.sha256,
uploadError: response.uploadError,
})
}
})
Expand Down
10 changes: 8 additions & 2 deletions src/uploaders/blossom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ export async function uploadBlossom(options: BlossomOptions): Promise<UploadTask
if (res.status !== 200) {
throw new Error((data as BlossomResponseError).message)
}
const json = data as BlossomResponse
const { nip94, ...json } = data as BlossomResponse
// Always append file extension if missing
const { pathname } = new URL(json.url)
const hasExtension = pathname.split('.').length === 2
const extension = '.' + options.file.type.split('/')[1]
const url = json.url + (hasExtension ? '' : extension)
return {
...json,
tags: Array.from(Object.entries(json.nip94 || [])),
url,
tags: Array.from(Object.entries(nip94 || [])).map((tag) => (tag[0] === 'url' ? [tag[0], url] : tag)),
}
}
33 changes: 13 additions & 20 deletions src/uploaders/nip96.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,18 @@ export async function uploadNIP96(options: NIP96Options): Promise<UploadTask> {
if (!options.sign) {
throw new Error('No signer provided')
}
try {
const server = await readServerConfig(options.serverUrl)
const authorization = await getToken(server.api_url, 'POST', options.sign, true)
const res = await uploadFile(options.file, server.api_url, authorization, {
alt: options.alt || '',
expiration: options.expiration?.toString() || '',
content_type: options.file.type,
})
if (res.status === 'error') {
throw new Error(res.message)
}
const url = res.nip94_event?.tags.find((x) => x[0] === 'url')?.[1] || ''
const sha256 = res.nip94_event?.tags.find((x) => x[0] === 'x')?.[1] || ''
return {
url,
sha256,
tags: res.nip94_event?.tags || [],
}
} catch (error) {
throw new Error(error as string)
const server = await readServerConfig(options.serverUrl)
const authorization = await getToken(server.api_url, 'POST', options.sign, true)
const res = await uploadFile(options.file, server.api_url, authorization, {
alt: options.alt || '',
expiration: options.expiration?.toString() || '',
content_type: options.file.type,
})
const url = res.nip94_event?.tags.find((x) => x[0] === 'url')?.[1] || ''
const sha256 = res.nip94_event?.tags.find((x) => x[0] === 'x')?.[1] || ''
return {
url,
sha256,
tags: res.nip94_event?.tags || [],
}
}

0 comments on commit 1a7fdd7

Please sign in to comment.