Skip to content

Commit

Permalink
Fix image bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmi committed Sep 25, 2023
1 parent 9e77b59 commit a207a6e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/gameComponents/Canvas.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ const NoCanvas = ({ layers, width, height }) => {
<ImageElm
src={firstImage.url}
alt=""
onError={(e) => (e.target.src = "/default.png")}
draggable={false}
width={width}
height={height}
Expand All @@ -176,6 +177,7 @@ const NoCanvas = ({ layers, width, height }) => {
<ImageWrapper key={url}>
<ImageElm
src={url}
onError={(e) => (e.target.src = "/default.png")}
alt=""
draggable={false}
style={{ marginLeft: offsetX * 2, marginTop: offsetY * 2 }}
Expand Down
7 changes: 6 additions & 1 deletion src/mediaLibrary/ImageField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ const ImageField = ({ value, onChange, onImageLoaded }) => {
</form>

<div className="imgContainer" onClick={(e) => e.preventDefault()}>
{type !== "empty" && content && <Thumbnail src={url} />}
{type !== "empty" && content && (
<Thumbnail
src={url}
onError={(e) => (e.target.src = "/default.png")}
/>
)}

{type === "external" && (
<input
Expand Down
10 changes: 10 additions & 0 deletions src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ export const uploadResourceImage = async (boxId, resourceId, file) => {
credentials: "include",
});

if (result.status === 404) {
throw new Error("Resource not found");
}
if (result.status === 403) {
throw new Error("Forbidden");
}
if (result.status >= 300) {
throw new Error("Server error");
}

return await result.text();
};

Expand Down
14 changes: 11 additions & 3 deletions src/utils/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const getImageWithRetry = async (url, retry = 0) => {
resolve(img);
};
img.onerror = () => {
if (retry < 3) {
if (retry < 2) {
getImageWithRetry(url, retry + 1)
.then(resolve)
.catch(reject);
Expand All @@ -25,12 +25,20 @@ export const getImageWithRetry = async (url, retry = 0) => {
});
};

export const getImage = async (url) => {
export const getImage = (url, defaultImage = "/default.png") => {
if (!url) {
return null;
}
if (!imageCache[url]) {
imageCache[url] = getImageWithRetry(url);
imageCache[url] = new Promise((resolve) => {
console.log("promise", url);
getImageWithRetry(url)
.then(resolve)
.catch(async () => {
console.log(`Missing image <${url}>`);
resolve(await getImage(defaultImage));
});
});
}
return imageCache[url];
};
Expand Down

0 comments on commit a207a6e

Please sign in to comment.