diff --git a/src/utils/image-metadata.ts b/src/utils/image-metadata.ts index 75b2f6ba..4429cc7a 100644 --- a/src/utils/image-metadata.ts +++ b/src/utils/image-metadata.ts @@ -6,23 +6,10 @@ import { Stickynotes } from '@soapbox/stickynotes'; const console = new Stickynotes('ditto:uploaders'); -export function toByteArray(f: File): Promise { - return new Promise((resolve, reject) => { - const reader = new FileReader(); - reader.addEventListener('loadend', (m) => { - if (m?.target?.result instanceof ArrayBuffer) { - resolve(new Uint8Array(m.target.result)); - } else reject('Error loading file: readAsArrayBufferFailed'); - }); - reader.addEventListener('error', (e) => reject(e)); - reader.readAsArrayBuffer(f); - }); -} - export async function getOptionalNip94Metadata(f: File): Promise { const tags: Nip94MetadataOptional = {}; try { - const buffer = await toByteArray(f); + const buffer = await new Response(f.stream()).bytes(); const hash = await crypto.subtle.digest('SHA-256', buffer).then(encodeHex); tags.x = tags.ox = hash; const img = sharp(buffer); diff --git a/src/utils/upload.ts b/src/utils/upload.ts index cadac9bc..9f4c90a4 100644 --- a/src/utils/upload.ts +++ b/src/utils/upload.ts @@ -3,7 +3,7 @@ import { HTTPException } from '@hono/hono/http-exception'; import { AppContext } from '@/app.ts'; import { Conf } from '@/config.ts'; import { DittoUpload, dittoUploads } from '@/DittoUploads.ts'; -import { getOptionalNip94Metadata, toByteArray } from '@/utils/image-metadata.ts'; +import { getOptionalNip94Metadata } from '@/utils/image-metadata.ts'; import type { Nip94MetadataOptional } from '@/interfaces/Nip94Metadata.ts'; import { encodeHex } from '@std/encoding/hex'; @@ -53,7 +53,8 @@ export async function uploadFile( } } if (!tagMap.has('x') || !tagMap.has('ox')) { - const hash = metadata?.x || await crypto.subtle.digest('SHA-256', await toByteArray(file)).then(encodeHex); + const hash = metadata?.x || + await crypto.subtle.digest('SHA-256', await new Response(file.stream()).bytes()).then(encodeHex); tags.push(['x', hash!]); tags.push(['ox', hash!]); }