feat: nostr.build
This commit is contained in:
30
src/Feed/FileUpload.ts
Normal file
30
src/Feed/FileUpload.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { useSelector } from "react-redux";
|
||||
import { RootState } from "State/Store";
|
||||
import NostrBuildUpload from "./NostrBuildUpload";
|
||||
import VoidUpload from "./VoidUpload";
|
||||
|
||||
export interface UploadResult {
|
||||
url?: string,
|
||||
error?: string
|
||||
}
|
||||
|
||||
export interface Uploader {
|
||||
upload: (f: File | Blob, filename: string) => Promise<UploadResult>
|
||||
}
|
||||
|
||||
export default function useFileUpload(): Uploader {
|
||||
const fileUploader = useSelector((s: RootState) => s.login.preferences.fileUploader);
|
||||
|
||||
switch (fileUploader) {
|
||||
case "nostr.build": {
|
||||
return {
|
||||
upload: NostrBuildUpload
|
||||
} as Uploader;
|
||||
}
|
||||
default: {
|
||||
return {
|
||||
upload: VoidUpload
|
||||
} as Uploader;
|
||||
}
|
||||
}
|
||||
}
|
26
src/Feed/NostrBuildUpload.ts
Normal file
26
src/Feed/NostrBuildUpload.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { UploadResult } from "./FileUpload";
|
||||
|
||||
export default async function NostrBuildUpload(file: File | Blob): Promise<UploadResult> {
|
||||
let fd = new FormData();
|
||||
fd.append("fileToUpload", file);
|
||||
fd.append("submit", "Upload Image");
|
||||
|
||||
let rsp = await fetch("https://nostr.build/api/upload/", {
|
||||
body: fd,
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "multipart/form-data",
|
||||
"accept": "application/json"
|
||||
}
|
||||
});
|
||||
if(rsp.ok) {
|
||||
let data = await rsp.json();
|
||||
console.debug(data);
|
||||
return {
|
||||
url: data.url
|
||||
}
|
||||
}
|
||||
return {
|
||||
error: "Upload failed"
|
||||
}
|
||||
}
|
@ -1,11 +1,12 @@
|
||||
import * as secp from "@noble/secp256k1";
|
||||
import { VoidCatHost } from "Const";
|
||||
import { FileExtensionRegex, VoidCatHost } from "Const";
|
||||
import { UploadResult } from "./FileUpload";
|
||||
|
||||
/**
|
||||
* Upload file to void.cat
|
||||
* https://void.cat/swagger/index.html
|
||||
*/
|
||||
export default async function VoidUpload(file: File | Blob, filename: string) {
|
||||
export default async function VoidUpload(file: File | Blob, filename: string): Promise<UploadResult> {
|
||||
const buf = await file.arrayBuffer();
|
||||
const digest = await crypto.subtle.digest("SHA-256", buf);
|
||||
|
||||
@ -25,9 +26,20 @@ export default async function VoidUpload(file: File | Blob, filename: string) {
|
||||
|
||||
if (req.ok) {
|
||||
let rsp: VoidUploadResponse = await req.json();
|
||||
return rsp;
|
||||
if (rsp.ok) {
|
||||
let ext = filename.match(FileExtensionRegex);
|
||||
return {
|
||||
url: rsp.file?.meta?.url ?? `${VoidCatHost}/d/${rsp.file?.id}${ext ? `.${ext[1]}` : ""}`
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
error: rsp.errorMessage
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return {
|
||||
error: "Upload failed"
|
||||
};
|
||||
}
|
||||
|
||||
export type VoidUploadResponse = {
|
||||
|
Reference in New Issue
Block a user