Make Uploaders return NIP-94 tags

This commit is contained in:
Alex Gleason 2024-05-18 18:23:04 -05:00
parent ff39dfc4ec
commit 6090c4a6d9
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
7 changed files with 41 additions and 75 deletions

View File

@ -9,46 +9,24 @@ interface FileMeta {
/** Upload a file, track it in the database, and return the resulting media object. */
async function uploadFile(file: File, meta: FileMeta, signal?: AbortSignal): Promise<UnattachedMedia> {
const { type, size } = file;
const { pubkey, description } = meta;
if (file.size > Conf.maxUploadSize) {
throw new Error('File size is too large.');
}
const { url, sha256, cid, blurhash, width, height } = await uploader.upload(file, { signal });
const data: string[][] = [
['url', url],
['m', type],
['size', size.toString()],
];
if (typeof width === 'number' && typeof height === 'number') {
data.push(['dim', `${width}x${height}`]);
}
if (sha256) {
data.push(['x', sha256]);
}
if (cid) {
data.push(['cid', cid]);
}
if (blurhash) {
data.push(['blurhash', blurhash]);
}
const tags = await uploader.upload(file, { signal });
const url = tags[0][1];
if (description) {
data.push(['alt', description]);
tags.push(['alt', description]);
}
return insertUnattachedMedia({
id: crypto.randomUUID(),
pubkey,
url,
data,
data: tags,
uploaded_at: Date.now(),
});
}

View File

@ -12,8 +12,8 @@ const configUploader: Uploader = {
upload(file, opts) {
return uploader().upload(file, opts);
},
delete(id, opts) {
return uploader().delete(id, opts);
async delete(id, opts) {
return await uploader().delete?.(id, opts);
},
};

View File

@ -32,11 +32,12 @@ const ipfsUploader: Uploader = {
const { Hash: cid } = ipfsAddResponseSchema.parse(await response.json());
return {
id: cid,
cid,
url: new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString(),
};
return [
['url', new URL(`/ipfs/${cid}`, Conf.mediaDomain).toString()],
['m', file.type],
['cid', cid],
['size', file.size.toString()],
];
},
async delete(cid, opts) {
const url = new URL('/api/v0/pin/rm', Conf.ipfs.apiUrl);

View File

@ -22,11 +22,12 @@ const localUploader: Uploader = {
const url = new URL(mediaDomain);
const path = url.pathname === '/' ? filename : join(url.pathname, filename);
return {
id: filename,
sha256,
url: new URL(path, url).toString(),
};
return [
['url', new URL(path, url).toString()],
['m', file.type],
['x', sha256],
['size', file.size.toString()],
];
},
async delete(id) {
await Deno.remove(join(Conf.uploadsDir, id));

View File

@ -17,17 +17,19 @@ export const nostrbuildUploader: Uploader = {
const json = await response.json();
const [data] = nostrbuildSchema.parse(json).data;
return {
id: data.url,
sha256: data.sha256,
url: data.url,
blurhash: data.blurhash,
width: data.dimensions?.width,
height: data.dimensions?.height,
};
},
// deno-lint-ignore require-await
async delete(): Promise<void> {
return;
const tags: [['url', string], ...string[][]] = [
['url', data.url],
['m', data.mime],
['x', data.sha256],
['ox', data.original_sha256],
['size', file.size.toString()],
['blurhash', data.blurhash],
];
if (data.dimensions) {
tags.push(['dim', `${data.dimensions.width}x${data.dimensions.height}`]);
}
return tags;
},
};

View File

@ -25,12 +25,14 @@ const s3Uploader: Uploader = {
const { pathStyle, bucket } = Conf.s3;
const path = (pathStyle && bucket) ? join(bucket, filename) : filename;
const url = new URL(path, Conf.mediaDomain).toString();
return {
id: filename,
sha256,
url: new URL(path, Conf.mediaDomain).toString(),
};
return [
['url', url],
['m', file.type],
['x', sha256],
['size', file.size.toString()],
];
},
async delete(id) {
await client().deleteObject(id);

View File

@ -1,27 +1,9 @@
/** Modular uploader interface, to support uploading to different backends. */
interface Uploader {
/** Upload the file to the backend. */
upload(file: File, opts?: { signal?: AbortSignal }): Promise<UploadResult>;
upload(file: File, opts?: { signal?: AbortSignal }): Promise<[['url', string], ...string[][]]>;
/** Delete the file from the backend. */
delete(cid: string, opts?: { signal?: AbortSignal }): Promise<void>;
}
/** Return value from the uploader after uploading a file. */
interface UploadResult {
/** File ID specific to the uploader, so it can later be referenced or deleted. */
id: string;
/** URL where the file can be accessed. */
url: string;
/** SHA-256 hash of the file. */
sha256?: string;
/** Blurhash of the file. */
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;
delete?(cid: string, opts?: { signal?: AbortSignal }): Promise<void>;
}
export type { Uploader };