From 5f66729b6f6ce3684bd041cce50544f20e182990 Mon Sep 17 00:00:00 2001 From: hzrd149 Date: Tue, 12 Nov 2024 16:59:01 +0000 Subject: [PATCH] update blossom client sdk --- package.json | 2 +- pnpm-lock.yaml | 17 ++++++++--- src/components/content/components/gallery.tsx | 2 +- src/helpers/media-upload/blossom.ts | 28 +++++++++++++------ src/hooks/use-textarea-upload-file.ts | 4 +-- src/views/wiki/components/markdown-editor.tsx | 10 +++---- 6 files changed, 41 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 5bc6a05ef..df8d3e296 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "applesauce-react": "next", "applesauce-signer": "next", "bech32": "^2.0.0", - "blossom-client-sdk": "^0.7.0", + "blossom-client-sdk": "next", "blossom-drive-sdk": "^0.4.1", "blurhash": "^2.0.5", "chart.js": "^4.4.6", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b8c746ea2..43c6ae0b0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -118,8 +118,8 @@ importers: specifier: ^2.0.0 version: 2.0.0 blossom-client-sdk: - specifier: ^0.7.0 - version: 0.7.0 + specifier: next + version: 0.0.0-next-20241107141841 blossom-drive-sdk: specifier: ^0.4.1 version: 0.4.1(typescript@5.6.3) @@ -341,10 +341,10 @@ importers: specifier: ^0.6.8 version: 0.6.8 '@types/react': - specifier: ^18.3.12 + specifier: ^18.2.22 version: 18.3.12 '@types/react-dom': - specifier: ^18.3.1 + specifier: ^18.2.7 version: 18.3.1 '@types/react-window': specifier: ^1.8.8 @@ -1964,6 +1964,10 @@ packages: bezier-js@6.1.4: resolution: {integrity: sha512-PA0FW9ZpcHbojUCMu28z9Vg/fNkwTj5YhusSAjHHDfHDGLxJ6YUKrAN2vk1fP2MMOxVw4Oko16FMlRGVBGqLKg==} + blossom-client-sdk@0.0.0-next-20241107141841: + resolution: {integrity: sha512-br7JUZrMDsURQowBObGdj6VEWbLslacUpKjONLgpRvN2w11fSt39Xdgd0pYjuPRtqTHOLClGXxyItYTQJQMlkw==} + engines: {node: '>=18'} + blossom-client-sdk@0.7.0: resolution: {integrity: sha512-xG0HiuhFcK6UpmYjJ4vRPm3APMrRf+MQDfZWlNRTxs2gEETfqbhYm5pCl2hPfLjpEcFSDXgr3sLCh6C77ABKgg==} @@ -6330,6 +6334,11 @@ snapshots: bezier-js@6.1.4: {} + blossom-client-sdk@0.0.0-next-20241107141841: + dependencies: + '@cashu/cashu-ts': 2.0.0-rc1 + '@noble/hashes': 1.5.0 + blossom-client-sdk@0.7.0: dependencies: '@noble/hashes': 1.5.0 diff --git a/src/components/content/components/gallery.tsx b/src/components/content/components/gallery.tsx index d925cefac..04dadc49d 100644 --- a/src/components/content/components/gallery.tsx +++ b/src/components/content/components/gallery.tsx @@ -1,6 +1,6 @@ import { forwardRef, MouseEventHandler, MutableRefObject, useCallback, useEffect, useMemo, useRef } from "react"; import { Link } from "@chakra-ui/react"; -import { handleImageFallbacks } from "blossom-client-sdk"; +import { handleImageFallbacks } from "blossom-client-sdk/image"; import { NostrEvent } from "nostr-tools"; import { EmbeddedImageProps, getPubkeyMediaServers, TrustImage, useImageThumbnail } from "../links"; diff --git a/src/helpers/media-upload/blossom.ts b/src/helpers/media-upload/blossom.ts index d74ef6107..c51a65bae 100644 --- a/src/helpers/media-upload/blossom.ts +++ b/src/helpers/media-upload/blossom.ts @@ -1,14 +1,26 @@ -import { BlobDescriptor, BlossomClient, Signer } from "blossom-client-sdk"; +import { BlobDescriptor, createUploadAuth, ServerType, Signer } from "blossom-client-sdk"; +import { multiServerUpload, MultiServerUploadOptions } from "blossom-client-sdk/actions/upload"; -export async function uploadFileToServers(servers: string[], file: File, signer: Signer) { - const results: BlobDescriptor[] = []; +export async function simpleMultiServerUpload( + servers: T[], + file: File, + signer: Signer, + opts?: MultiServerUploadOptions, +): Promise { + const results = await multiServerUpload(servers, file, { + ...opts, + onAuth: (_server, blob) => createUploadAuth(signer, blob), + }); + + let blob: BlobDescriptor | null = null; - const auth = await BlossomClient.getUploadAuth(file, signer); for (const server of servers) { - try { - results.push(await BlossomClient.uploadBlob(server, file, auth)); - } catch (e) {} + if (results.has(server)) { + blob = results.get(server)!; + break; + } } + if (!blob) throw new Error("Failed to upload"); - return results[0]; + return blob; } diff --git a/src/hooks/use-textarea-upload-file.ts b/src/hooks/use-textarea-upload-file.ts index 829636550..b6623c5f5 100644 --- a/src/hooks/use-textarea-upload-file.ts +++ b/src/hooks/use-textarea-upload-file.ts @@ -7,7 +7,7 @@ import { useSigningContext } from "../providers/global/signing-provider"; import { UseFormGetValues, UseFormSetValue } from "react-hook-form"; import useAppSettings from "./use-app-settings"; import useUsersMediaServers from "./use-user-media-servers"; -import { uploadFileToServers } from "../helpers/media-upload/blossom"; +import { simpleMultiServerUpload } from "../helpers/media-upload/blossom"; import useCurrentAccount from "./use-current-account"; import { stripSensitiveMetadataOnFile } from "../helpers/image"; @@ -71,7 +71,7 @@ export default function useTextAreaUploadFile( const imageUrl = response.url; insertURL(imageUrl); } else if (mediaUploadService === "blossom" && mediaServers.length) { - const blob = await uploadFileToServers( + const blob = await simpleMultiServerUpload( mediaServers.map((s) => s.toString()), safeFile, requestSignature, diff --git a/src/views/wiki/components/markdown-editor.tsx b/src/views/wiki/components/markdown-editor.tsx index 3cdf9c15b..aa9078471 100644 --- a/src/views/wiki/components/markdown-editor.tsx +++ b/src/views/wiki/components/markdown-editor.tsx @@ -1,6 +1,7 @@ import { useMemo, useRef, useState } from "react"; import { VisuallyHidden } from "@chakra-ui/react"; import SimpleMDE, { SimpleMDEReactProps } from "react-simplemde-editor"; +import { multiServerUpload } from "blossom-client-sdk/actions/upload"; import ReactDOMServer from "react-dom/server"; import { Global, css } from "@emotion/react"; @@ -13,7 +14,7 @@ import useCurrentAccount from "../../../hooks/use-current-account"; import { CharkaMarkdown } from "./markdown"; import { useSigningContext } from "../../../providers/global/signing-provider"; -import { uploadFileToServers } from "../../../helpers/media-upload/blossom"; +import { simpleMultiServerUpload } from "../../../helpers/media-upload/blossom"; import { stripSensitiveMetadataOnFile } from "../../../helpers/image"; const fixCodeMirrorFont = css` @@ -36,11 +37,8 @@ export default function MarkdownEditor({ options, ...props }: SimpleMDEReactProp if (!servers) return onError("No media servers set"); try { const safeFile = await stripSensitiveMetadataOnFile(file); - const blob = await uploadFileToServers( - servers.map((s) => s.toString()), - safeFile, - requestSignature, - ); + const blob = await simpleMultiServerUpload(servers, safeFile, requestSignature); + if (blob) onSuccess(blob.url); } catch (error) { if (error instanceof Error) onError(error.message);