mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Replace our NostrBuildUploader with the one from Nostrify
This commit is contained in:
parent
f0b247130f
commit
0541287f0e
2 changed files with 2 additions and 72 deletions
|
|
@ -1,10 +1,9 @@
|
||||||
import { BlossomUploader } from '@nostrify/nostrify/uploaders';
|
import { BlossomUploader, NostrBuildUploader } from '@nostrify/nostrify/uploaders';
|
||||||
|
|
||||||
import { AppMiddleware } from '@/app.ts';
|
import { AppMiddleware } from '@/app.ts';
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { DenoUploader } from '@/uploaders/DenoUploader.ts';
|
import { DenoUploader } from '@/uploaders/DenoUploader.ts';
|
||||||
import { IPFSUploader } from '@/uploaders/IPFSUploader.ts';
|
import { IPFSUploader } from '@/uploaders/IPFSUploader.ts';
|
||||||
import { NostrBuildUploader } from '@/uploaders/NostrBuildUploader.ts';
|
|
||||||
import { S3Uploader } from '@/uploaders/S3Uploader.ts';
|
import { S3Uploader } from '@/uploaders/S3Uploader.ts';
|
||||||
import { fetchWorker } from '@/workers/fetch.ts';
|
import { fetchWorker } from '@/workers/fetch.ts';
|
||||||
|
|
||||||
|
|
@ -23,7 +22,7 @@ export const uploaderMiddleware: AppMiddleware = async (c, next) => {
|
||||||
c.set('uploader', new DenoUploader({ baseUrl: Conf.mediaDomain, dir: Conf.uploadsDir }));
|
c.set('uploader', new DenoUploader({ baseUrl: Conf.mediaDomain, dir: Conf.uploadsDir }));
|
||||||
break;
|
break;
|
||||||
case 'nostrbuild':
|
case 'nostrbuild':
|
||||||
c.set('uploader', new NostrBuildUploader({ endpoint: Conf.nostrbuildEndpoint, fetch: fetchWorker }));
|
c.set('uploader', new NostrBuildUploader({ endpoint: Conf.nostrbuildEndpoint, signer, fetch: fetchWorker }));
|
||||||
break;
|
break;
|
||||||
case 'blossom':
|
case 'blossom':
|
||||||
if (signer) {
|
if (signer) {
|
||||||
|
|
|
||||||
|
|
@ -1,69 +0,0 @@
|
||||||
import { z } from 'zod';
|
|
||||||
|
|
||||||
import { DittoUploader } from '@/interfaces/DittoUploader.ts';
|
|
||||||
|
|
||||||
export interface NostrBuildUploaderOpts {
|
|
||||||
endpoint?: string;
|
|
||||||
fetch?: typeof fetch;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Upload files to nostr.build or another compatible server. */
|
|
||||||
export class NostrBuildUploader implements DittoUploader {
|
|
||||||
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 formData = new FormData();
|
|
||||||
formData.append('fileToUpload', file);
|
|
||||||
|
|
||||||
const response = await this.fetch(this.endpoint, {
|
|
||||||
method: 'POST',
|
|
||||||
body: formData,
|
|
||||||
signal: opts?.signal,
|
|
||||||
});
|
|
||||||
|
|
||||||
const json = await response.json();
|
|
||||||
const [data] = NostrBuildUploader.schema().parse(json).data;
|
|
||||||
|
|
||||||
const tags: [['url', string], ...string[][]] = [
|
|
||||||
['url', data.url],
|
|
||||||
['m', data.mime],
|
|
||||||
['x', data.sha256],
|
|
||||||
['ox', data.original_sha256],
|
|
||||||
['size', data.size.toString()],
|
|
||||||
];
|
|
||||||
|
|
||||||
if (data.dimensions) {
|
|
||||||
tags.push(['dim', `${data.dimensions.width}x${data.dimensions.height}`]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.blurhash) {
|
|
||||||
tags.push(['blurhash', data.blurhash]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return tags;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** nostr.build API response schema. */
|
|
||||||
private static schema() {
|
|
||||||
return z.object({
|
|
||||||
data: z.object({
|
|
||||||
url: z.string().url(),
|
|
||||||
blurhash: z.string().optional().catch(undefined),
|
|
||||||
sha256: z.string(),
|
|
||||||
original_sha256: z.string(),
|
|
||||||
mime: z.string(),
|
|
||||||
size: z.number(),
|
|
||||||
dimensions: z.object({
|
|
||||||
width: z.number(),
|
|
||||||
height: z.number(),
|
|
||||||
}).optional().catch(undefined),
|
|
||||||
}).array().min(1),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Reference in a new issue