Do things the boilerplatey way just for consistency
This commit is contained in:
parent
24659d8edb
commit
acef173ac4
|
@ -13,19 +13,23 @@ export interface DenoUploaderOpts {
|
|||
|
||||
/** Local Deno filesystem uploader. */
|
||||
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[][]]> {
|
||||
const { dir, baseUrl } = this.opts;
|
||||
|
||||
const sha256 = encodeHex(await crypto.subtle.digest('SHA-256', file.stream()));
|
||||
const ext = extensionsByType(file.type)?.[0] ?? 'bin';
|
||||
const filename = `${sha256}.${ext}`;
|
||||
|
||||
await Deno.mkdir(dir, { recursive: true });
|
||||
await Deno.writeFile(join(dir, filename), file.stream());
|
||||
await Deno.mkdir(this.dir, { recursive: true });
|
||||
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);
|
||||
|
||||
return [
|
||||
|
@ -37,8 +41,7 @@ export class DenoUploader implements DittoUploader {
|
|||
}
|
||||
|
||||
async delete(filename: string) {
|
||||
const { dir } = this.opts;
|
||||
const path = join(dir, filename);
|
||||
const path = join(this.dir, filename);
|
||||
await Deno.remove(path);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,15 +9,19 @@ export interface NostrBuildUploaderOpts {
|
|||
|
||||
/** Upload files to nostr.build or another compatible server. */
|
||||
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[][]]> {
|
||||
const { endpoint = 'https://nostr.build/api/v2/upload/files', fetch = globalThis.fetch } = this.opts;
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('fileToUpload', file);
|
||||
|
||||
const response = await fetch(endpoint, {
|
||||
const response = await this.fetch(this.endpoint, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
signal: opts?.signal,
|
||||
|
|
Loading…
Reference in New Issue