Skip to content

Commit

Permalink
update blossom client sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
hzrd149 committed Nov 12, 2024
1 parent 1e8b51b commit 5f66729
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 13 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/components/content/components/gallery.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
28 changes: 20 additions & 8 deletions src/helpers/media-upload/blossom.ts
Original file line number Diff line number Diff line change
@@ -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<T extends ServerType = ServerType>(
servers: T[],
file: File,
signer: Signer,
opts?: MultiServerUploadOptions<T, File>,
): Promise<BlobDescriptor> {
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;
}
4 changes: 2 additions & 2 deletions src/hooks/use-textarea-upload-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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,
Expand Down
10 changes: 4 additions & 6 deletions src/views/wiki/components/markdown-editor.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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`
Expand All @@ -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);
Expand Down

0 comments on commit 5f66729

Please sign in to comment.