Skip to content

Commit

Permalink
fix blossom
Browse files Browse the repository at this point in the history
  • Loading branch information
moysa committed Dec 19, 2024
1 parent c565556 commit 3a3eace
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/components/NoteImage/NoteImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,44 @@ const NoteImage: Component<{
return true;
}

const userBlossoms = app?.actions.getUserBlossomUrls(props.authorPk || '');
// list of user's blossom servers from kind 10_063
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) => {
// Image url from a Note
const originalSrc = src() || '';

// extract the file hash
const fileHash = originalSrc.slice(originalSrc.lastIndexOf('/'))

// Send HEAD requests to each blossom server to check if the resource is there
const reqs = userBlossoms.map(url =>
new Promise<string>((resolve, reject) => {
const resourceUrl = `${url}/${fileHash}`;
const response = fetch(resourceUrl, { method: 'HEAD'}).then(response => {
// Check to see if there is an image there
if (response.headers.get('Content-Type')?.startsWith('image')) {
res(url);
resolve(resourceUrl);
} else {
rej('')
reject('')
}
});
}));

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}`);
try {
// Wait for at least one req to succeed
const blossomUrl = await Promise.any(reqs);

// If found, set image src to the blossom url
if (blossomUrl.length > 0) {
setSrc(() => blossomUrl);
image.onerror = "";
image.src = src();
image.src = blossomUrl;
return true;
}
} catch {
setSrc(() => props.altSrc || '');
}

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

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

const ratio = () => {
Expand Down

0 comments on commit 3a3eace

Please sign in to comment.