Merge pull request #181 from v0l/nostrimg

feat: nostrimg.com
This commit is contained in:
2023-02-01 19:20:38 +00:00
committed by GitHub
8 changed files with 59 additions and 11 deletions

View File

@ -1,30 +0,0 @@
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;
}
}
}

View File

@ -1,24 +0,0 @@
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/snort.php", {
body: fd,
method: "POST",
headers: {
"accept": "application/json"
}
});
if(rsp.ok) {
let data = await rsp.json();
return {
url: new URL(data).toString()
}
}
return {
error: "Upload failed"
}
}

View File

@ -1,72 +0,0 @@
import * as secp from "@noble/secp256k1";
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): Promise<UploadResult> {
const buf = await file.arrayBuffer();
const digest = await crypto.subtle.digest("SHA-256", buf);
let req = await fetch(`${VoidCatHost}/upload`, {
mode: "cors",
method: "POST",
body: buf,
headers: {
"Content-Type": "application/octet-stream",
"V-Content-Type": file.type,
"V-Filename": filename,
"V-Full-Digest": secp.utils.bytesToHex(new Uint8Array(digest)),
"V-Description": "Upload from https://snort.social",
"V-Strip-Metadata": "true"
}
});
if (req.ok) {
let rsp: VoidUploadResponse = await req.json();
if (rsp.ok) {
let ext = filename.match(FileExtensionRegex);
if(rsp.file?.metadata?.mimeType === "image/webp") {
ext = ["", "webp"];
}
return {
url: rsp.file?.metadata?.url ?? `${VoidCatHost}/d/${rsp.file?.id}${ext ? `.${ext[1]}` : ""}`
}
} else {
return {
error: rsp.errorMessage
}
}
}
return {
error: "Upload failed"
};
}
export type VoidUploadResponse = {
ok: boolean,
file?: VoidFile,
errorMessage?: string
}
export type VoidFile = {
id: string,
metadata?: VoidFileMeta
}
export type VoidFileMeta = {
version: number,
id: string,
name?: string,
size: number,
uploaded: Date,
description?: string,
mimeType?: string,
digest?: string,
url?: string,
expires?: Date,
storage?: string,
encryptionParams?: string,
}