From 353111051a79a37aa3b7be1c44b4c54ffbeb9904 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 18 May 2024 16:53:17 -0500 Subject: [PATCH] Use dimensions from nostr.build --- src/schemas/nostrbuild.ts | 3 ++- src/upload.ts | 6 +++++- src/uploaders/nostrbuild.ts | 4 ++-- src/uploaders/types.ts | 4 ++++ src/views/mastodon/attachments.ts | 14 ++++++++++++++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/schemas/nostrbuild.ts b/src/schemas/nostrbuild.ts index c9fd680..db9f607 100644 --- a/src/schemas/nostrbuild.ts +++ b/src/schemas/nostrbuild.ts @@ -6,11 +6,12 @@ export const nostrbuildFileSchema = z.object({ thumbnail: z.string(), blurhash: z.string(), sha256: z.string(), + original_sha256: z.string(), mime: z.string(), dimensions: z.object({ width: z.number(), height: z.number(), - }), + }).optional().catch(undefined), }); export const nostrbuildSchema = z.object({ diff --git a/src/upload.ts b/src/upload.ts index 5b43f39..1da5a7d 100644 --- a/src/upload.ts +++ b/src/upload.ts @@ -16,7 +16,7 @@ async function uploadFile(file: File, meta: FileMeta, signal?: AbortSignal): Pro throw new Error('File size is too large.'); } - const { url, sha256, cid, blurhash } = await uploader.upload(file, { signal }); + const { url, sha256, cid, blurhash, width, height } = await uploader.upload(file, { signal }); const data: string[][] = [ ['url', url], @@ -24,6 +24,10 @@ async function uploadFile(file: File, meta: FileMeta, signal?: AbortSignal): Pro ['size', size.toString()], ]; + if (typeof width === 'number' && typeof height === 'number') { + data.push(['dim', `${width}x${height}`]); + } + if (sha256) { data.push(['x', sha256]); } diff --git a/src/uploaders/nostrbuild.ts b/src/uploaders/nostrbuild.ts index d9eed24..d9d0865 100644 --- a/src/uploaders/nostrbuild.ts +++ b/src/uploaders/nostrbuild.ts @@ -15,8 +15,6 @@ export const nostrbuildUploader: Uploader = { }); const json = await response.json(); - console.log(JSON.stringify(json)); - const [data] = nostrbuildSchema.parse(json).data; return { @@ -24,6 +22,8 @@ export const nostrbuildUploader: Uploader = { sha256: data.sha256, url: data.url, blurhash: data.blurhash, + width: data.dimensions?.width, + height: data.dimensions?.height, }; }, // deno-lint-ignore require-await diff --git a/src/uploaders/types.ts b/src/uploaders/types.ts index ac5bf05..e423028 100644 --- a/src/uploaders/types.ts +++ b/src/uploaders/types.ts @@ -18,6 +18,10 @@ interface UploadResult { blurhash?: string; /** IPFS CID of the file. */ cid?: string; + /** Width of the file, if applicable. */ + width?: number; + /** Height of the file, if applicable. */ + height?: number; } export type { Uploader }; diff --git a/src/views/mastodon/attachments.ts b/src/views/mastodon/attachments.ts index 273b460..0b1b8eb 100644 --- a/src/views/mastodon/attachments.ts +++ b/src/views/mastodon/attachments.ts @@ -6,10 +6,23 @@ function renderAttachment(media: { id?: string; data: string[][] }) { const url = tags.find(([name]) => name === 'url')?.[1]; const alt = tags.find(([name]) => name === 'alt')?.[1]; const cid = tags.find(([name]) => name === 'cid')?.[1]; + const dim = tags.find(([name]) => name === 'dim')?.[1]; const blurhash = tags.find(([name]) => name === 'blurhash')?.[1]; if (!url) return; + const [width, height] = dim?.split('x').map(Number) ?? [null, null]; + + const meta = (typeof width === 'number' && typeof height === 'number') + ? { + original: { + width, + height, + aspect: width / height, + }, + } + : undefined; + return { id: id ?? url, type: getAttachmentType(m ?? ''), @@ -18,6 +31,7 @@ function renderAttachment(media: { id?: string; data: string[][] }) { remote_url: null, description: alt ?? '', blurhash: blurhash || null, + meta, cid: cid, }; }