Do things the boilerplatey way just for consistency

This commit is contained in:
Alex Gleason 2024-05-18 22:15:33 -05:00
parent 24659d8edb
commit acef173ac4
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 19 additions and 12 deletions

View File

@ -13,19 +13,23 @@ export interface DenoUploaderOpts {
/** Local Deno filesystem uploader. */ /** Local Deno filesystem uploader. */
export class DenoUploader implements DittoUploader { export class DenoUploader implements DittoUploader {
constructor(private opts: DenoUploaderOpts) {} baseUrl: string;
dir: string;
constructor(opts: DenoUploaderOpts) {
this.baseUrl = opts.baseUrl;
this.dir = opts.dir;
}
async upload(file: File): Promise<[['url', string], ...string[][]]> { async upload(file: File): Promise<[['url', string], ...string[][]]> {
const { dir, baseUrl } = this.opts;
const sha256 = encodeHex(await crypto.subtle.digest('SHA-256', file.stream())); const sha256 = encodeHex(await crypto.subtle.digest('SHA-256', file.stream()));
const ext = extensionsByType(file.type)?.[0] ?? 'bin'; const ext = extensionsByType(file.type)?.[0] ?? 'bin';
const filename = `${sha256}.${ext}`; const filename = `${sha256}.${ext}`;
await Deno.mkdir(dir, { recursive: true }); await Deno.mkdir(this.dir, { recursive: true });
await Deno.writeFile(join(dir, filename), file.stream()); await Deno.writeFile(join(this.dir, filename), file.stream());
const url = new URL(baseUrl); const url = new URL(this.baseUrl);
const path = url.pathname === '/' ? filename : join(url.pathname, filename); const path = url.pathname === '/' ? filename : join(url.pathname, filename);
return [ return [
@ -37,8 +41,7 @@ export class DenoUploader implements DittoUploader {
} }
async delete(filename: string) { async delete(filename: string) {
const { dir } = this.opts; const path = join(this.dir, filename);
const path = join(dir, filename);
await Deno.remove(path); await Deno.remove(path);
} }
} }

View File

@ -9,15 +9,19 @@ export interface NostrBuildUploaderOpts {
/** Upload files to nostr.build or another compatible server. */ /** Upload files to nostr.build or another compatible server. */
export class NostrBuildUploader implements DittoUploader { export class NostrBuildUploader implements DittoUploader {
constructor(private opts: NostrBuildUploaderOpts) {} private endpoint: string;
private fetch: typeof fetch;
constructor(opts: NostrBuildUploaderOpts) {
this.endpoint = opts.endpoint ?? 'https://nostr.build/api/v2/upload/files';
this.fetch = opts.fetch ?? globalThis.fetch;
}
async upload(file: File, opts?: { signal?: AbortSignal }): Promise<[['url', string], ...string[][]]> { async upload(file: File, opts?: { signal?: AbortSignal }): Promise<[['url', string], ...string[][]]> {
const { endpoint = 'https://nostr.build/api/v2/upload/files', fetch = globalThis.fetch } = this.opts;
const formData = new FormData(); const formData = new FormData();
formData.append('fileToUpload', file); formData.append('fileToUpload', file);
const response = await fetch(endpoint, { const response = await this.fetch(this.endpoint, {
method: 'POST', method: 'POST',
body: formData, body: formData,
signal: opts?.signal, signal: opts?.signal,