feat: custom nip96 server

This commit is contained in:
2024-05-13 14:19:28 +01:00
parent ecd3876287
commit b5ca5327db
7 changed files with 87 additions and 6 deletions

View File

@ -46,7 +46,12 @@ export interface UserPreferences {
/**
* File uploading service to upload attachments to
*/
fileUploader: "void.cat" | "nostr.build" | "nostrimg.com" | "void.cat-NIP96" | "nostrcheck.me";
fileUploader: "void.cat" | "nostr.build" | "nostrimg.com" | "void.cat-NIP96" | "nostrcheck.me" | "nip96";
/**
* Custom file server to upload files to
*/
nip96Server?: string;
/**
* Use imgproxy to optimize images

View File

@ -8,7 +8,9 @@ export class Nip96Uploader implements Uploader {
constructor(
readonly url: string,
readonly publisher: EventPublisher,
) {}
) {
this.url = new URL(this.url).toString();
}
get progress() {
return [];
@ -62,16 +64,27 @@ export class Nip96Uploader implements Uploader {
metadata: {
width: dim?.at(0) ? Number(dim[0]) : undefined,
height: dim?.at(1) ? Number(dim[1]) : undefined,
blurhash: data.nip94_event.tags.find(a => a[0] === "blurhash")?.at(1),
hash: data.nip94_event.tags.find(a => a[0] === "x")?.at(1),
},
};
}
return {
error: data.message,
};
} else {
const text = await rsp.text();
try {
const obj = JSON.parse(text) as Nip96Result;
return {
error: obj.message,
};
} catch {
return {
error: `Upload failed: ${text}`,
};
}
}
return {
error: "Upload failed",
};
}
}

View File

@ -0,0 +1 @@
export class BlossomClient {}

View File

@ -65,7 +65,10 @@ export interface UploadProgress {
export type UploadStage = "starting" | "hashing" | "uploading" | "done" | undefined;
export default function useFileUpload(): Uploader {
const fileUploader = usePreferences(s => s.fileUploader);
const { fileUploader, nip96Server } = usePreferences(s => ({
fileUploader: s.fileUploader,
nip96Server: s.nip96Server,
}));
const { publisher } = useEventPublisher();
const [progress, setProgress] = useState<Array<UploadProgress>>([]);
const [stage, setStage] = useState<UploadStage>();
@ -77,6 +80,9 @@ export default function useFileUpload(): Uploader {
progress: [],
} as Uploader;
}
case "nip96": {
return new Nip96Uploader(unwrap(nip96Server), unwrap(publisher));
}
case "void.cat-NIP96": {
return new Nip96Uploader("https://void.cat/nostr", unwrap(publisher));
}