Use dimensions from nostr.build

This commit is contained in:
Alex Gleason 2024-05-18 16:53:17 -05:00
parent ce49c500ae
commit 353111051a
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
5 changed files with 27 additions and 4 deletions

View File

@ -6,11 +6,12 @@ export const nostrbuildFileSchema = z.object({
thumbnail: z.string(), thumbnail: z.string(),
blurhash: z.string(), blurhash: z.string(),
sha256: z.string(), sha256: z.string(),
original_sha256: z.string(),
mime: z.string(), mime: z.string(),
dimensions: z.object({ dimensions: z.object({
width: z.number(), width: z.number(),
height: z.number(), height: z.number(),
}), }).optional().catch(undefined),
}); });
export const nostrbuildSchema = z.object({ export const nostrbuildSchema = z.object({

View File

@ -16,7 +16,7 @@ async function uploadFile(file: File, meta: FileMeta, signal?: AbortSignal): Pro
throw new Error('File size is too large.'); 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[][] = [ const data: string[][] = [
['url', url], ['url', url],
@ -24,6 +24,10 @@ async function uploadFile(file: File, meta: FileMeta, signal?: AbortSignal): Pro
['size', size.toString()], ['size', size.toString()],
]; ];
if (typeof width === 'number' && typeof height === 'number') {
data.push(['dim', `${width}x${height}`]);
}
if (sha256) { if (sha256) {
data.push(['x', sha256]); data.push(['x', sha256]);
} }

View File

@ -15,8 +15,6 @@ export const nostrbuildUploader: Uploader = {
}); });
const json = await response.json(); const json = await response.json();
console.log(JSON.stringify(json));
const [data] = nostrbuildSchema.parse(json).data; const [data] = nostrbuildSchema.parse(json).data;
return { return {
@ -24,6 +22,8 @@ export const nostrbuildUploader: Uploader = {
sha256: data.sha256, sha256: data.sha256,
url: data.url, url: data.url,
blurhash: data.blurhash, blurhash: data.blurhash,
width: data.dimensions?.width,
height: data.dimensions?.height,
}; };
}, },
// deno-lint-ignore require-await // deno-lint-ignore require-await

View File

@ -18,6 +18,10 @@ interface UploadResult {
blurhash?: string; blurhash?: string;
/** IPFS CID of the file. */ /** IPFS CID of the file. */
cid?: string; cid?: string;
/** Width of the file, if applicable. */
width?: number;
/** Height of the file, if applicable. */
height?: number;
} }
export type { Uploader }; export type { Uploader };

View File

@ -6,10 +6,23 @@ function renderAttachment(media: { id?: string; data: string[][] }) {
const url = tags.find(([name]) => name === 'url')?.[1]; const url = tags.find(([name]) => name === 'url')?.[1];
const alt = tags.find(([name]) => name === 'alt')?.[1]; const alt = tags.find(([name]) => name === 'alt')?.[1];
const cid = tags.find(([name]) => name === 'cid')?.[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]; const blurhash = tags.find(([name]) => name === 'blurhash')?.[1];
if (!url) return; 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 { return {
id: id ?? url, id: id ?? url,
type: getAttachmentType(m ?? ''), type: getAttachmentType(m ?? ''),
@ -18,6 +31,7 @@ function renderAttachment(media: { id?: string; data: string[][] }) {
remote_url: null, remote_url: null,
description: alt ?? '', description: alt ?? '',
blurhash: blurhash || null, blurhash: blurhash || null,
meta,
cid: cid, cid: cid,
}; };
} }