Skip to content

Commit

Permalink
Blossom for images
Browse files Browse the repository at this point in the history
  • Loading branch information
moysa committed Dec 18, 2024
1 parent 82fcf7b commit c02d49e
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 569 deletions.
1 change: 1 addition & 0 deletions src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ const Avatar: Component<{
onError={imgError}
mediaThumb={imageThumb()}
ignoreRatio={true}
authorPk={props.user?.pubkey}
/>
</Show>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/components/DirectMessages/DirectMessageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const DirectMessageConversation: Component<{
>
<DirectMessageParsedContent
content={msg.content}
sender={msg.sender}
/>
</div>
);
Expand Down Expand Up @@ -105,6 +106,7 @@ const DirectMessageConversation: Component<{
>
<DirectMessageParsedContent
content={section}
sender={msg.sender}
/>
</div>
}>
Expand Down
3 changes: 3 additions & 0 deletions src/components/DirectMessages/DirectMessageParsedContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const groupGridLimit = 7;
const DirectMessageParsedContent: Component<{
id?: string,
content: string,
sender: string,
ignoreMedia?: boolean,
noLinks?: string,
noPreviews?: boolean,
Expand Down Expand Up @@ -412,6 +413,7 @@ const DirectMessageParsedContent: Component<{
mediaThumb={imageThumb}
width={514}
imageGroup={`${imageGroup}`}
authorPk={props.sender}
/>
}

Expand Down Expand Up @@ -439,6 +441,7 @@ const DirectMessageParsedContent: Component<{
imageGroup={`${imageGroup}`}
plainBorder={true}
forceHeight={500}
authorPk={props.sender}
/>
}}
</For>
Expand Down
1 change: 1 addition & 0 deletions src/components/Note/NoteGallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ const NoteGallery: Component<{
</A>
</div>
}
authorPk={props.note.pubkey}
/>
</div>
</Match>
Expand Down
31 changes: 30 additions & 1 deletion src/components/NoteImage/NoteImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component, createEffect, createSignal, JSX, JSXElement, onMount, Show
import styles from "./NoteImage.module.scss";
import { generatePrivateKey } from "../../lib/nTools";
import { MediaVariant } from "../../types/primal";
import { useAppContext } from "../../contexts/AppContext";

const NoteImage: Component<{
class?: string,
Expand All @@ -19,7 +20,9 @@ const NoteImage: Component<{
caption?: JSXElement | string,
ignoreRatio?: boolean,
forceHeight?: number;
authorPk?: string,
}> = (props) => {
const app = useAppContext();
const imgId = generatePrivateKey();

let imgVirtual: HTMLImageElement | undefined;
Expand All @@ -31,7 +34,7 @@ const NoteImage: Component<{

const isCached = () => !props.isDev || props.media;

const onError = (event: any) => {
const onError = async (event: any) => {
const image = event.target;

if (image.src === props.altSrc || !props.altSrc || image.src.endsWith(props.altSrc)) {
Expand All @@ -40,6 +43,32 @@ const NoteImage: Component<{
return true;
}

const userBlossoms = app?.actions.getUserBlossomUrls(props.authorPk || '');

if (userBlossoms) {
const reqs = userBlossoms.map(url => new Promise<string>((res, rej) => {
fetch(url, { method: 'HEAD'}).then((response) => {
if (response.headers.get('Content-Type')?.startsWith('image')) {
res(url);
} else {
rej('')
}
});
}));

const bServer = await Promise.any(reqs);

if (typeof bServer === 'string' && bServer.length > 0) {
const oSrc = src() || '';
const bSrc = oSrc.slice(oSrc.lastIndexOf('/'), oSrc.lastIndexOf('.'));
setSrc(() => `${bServer}/${bSrc}`);

image.onerror = "";
image.src = src();
return true;
}
}

setSrc(() => props.altSrc || '');

image.onerror = "";
Expand Down
Loading

0 comments on commit c02d49e

Please sign in to comment.