NIP-94 file headers (#488)
* feat: NIP-94 file headers * Merge NIP-81 tags * disable embedding nip94 for now * merge error * disable broken test * bugfixes * bug: validation failure
This commit is contained in:
@ -1,12 +1,19 @@
|
||||
import * as secp from "@noble/secp256k1";
|
||||
import { EventKind } from "@snort/nostr";
|
||||
import { FileExtensionRegex, VoidCatHost } from "Const";
|
||||
import { EventPublisher } from "System/EventPublisher";
|
||||
import { UploadResult } from "Upload";
|
||||
import { magnetURIDecode } from "Util";
|
||||
|
||||
/**
|
||||
* Upload file to void.cat
|
||||
* https://void.cat/swagger/index.html
|
||||
*/
|
||||
export default async function VoidCat(file: File | Blob, filename: string): Promise<UploadResult> {
|
||||
export default async function VoidCat(
|
||||
file: File | Blob,
|
||||
filename: string,
|
||||
publisher?: EventPublisher
|
||||
): Promise<UploadResult> {
|
||||
const buf = await file.arrayBuffer();
|
||||
const digest = await crypto.subtle.digest("SHA-256", buf);
|
||||
|
||||
@ -31,9 +38,35 @@ export default async function VoidCat(file: File | Blob, filename: string): Prom
|
||||
if (rsp.file?.metadata?.mimeType === "image/webp") {
|
||||
ext = ["", "webp"];
|
||||
}
|
||||
return {
|
||||
url: rsp.file?.metadata?.url ?? `${VoidCatHost}/d/${rsp.file?.id}${ext ? `.${ext[1]}` : ""}`,
|
||||
};
|
||||
const resultUrl = rsp.file?.metadata?.url ?? `${VoidCatHost}/d/${rsp.file?.id}${ext ? `.${ext[1]}` : ""}`;
|
||||
|
||||
const ret = {
|
||||
url: resultUrl,
|
||||
} as UploadResult;
|
||||
|
||||
if (publisher) {
|
||||
const tags = [
|
||||
["url", resultUrl],
|
||||
["x", rsp.file?.metadata?.digest ?? ""],
|
||||
["m", rsp.file?.metadata?.mimeType ?? "application/octet-stream"],
|
||||
];
|
||||
if (rsp.file?.metadata?.size) {
|
||||
tags.push(["size", rsp.file.metadata.size.toString()]);
|
||||
}
|
||||
if (rsp.file?.metadata?.magnetLink) {
|
||||
tags.push(["magnet", rsp.file.metadata.magnetLink]);
|
||||
const parsedMagnet = magnetURIDecode(rsp.file.metadata.magnetLink);
|
||||
if (parsedMagnet?.infoHash) {
|
||||
tags.push(["i", parsedMagnet?.infoHash]);
|
||||
}
|
||||
}
|
||||
ret.header = await publisher.generic(eb => {
|
||||
eb.kind(EventKind.FileHeader).content(filename);
|
||||
tags.forEach(t => eb.tag(t));
|
||||
return eb;
|
||||
});
|
||||
}
|
||||
return ret;
|
||||
} else {
|
||||
return {
|
||||
error: rsp.errorMessage,
|
||||
@ -69,4 +102,5 @@ export type VoidFileMeta = {
|
||||
expires?: Date;
|
||||
storage?: string;
|
||||
encryptionParams?: string;
|
||||
magnetLink?: string;
|
||||
};
|
||||
|
Reference in New Issue
Block a user