-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Fetch target images in auth envs (#1420)
Closes https://issues.redhat.com/browse/MTA-1249?filter=-1 - Image was not being fetched in envs with auth enabled. Image tags (<img>) in HTML do not follow the same request mechanism as AJAX or fetch requests. They do not use the XMLHttpRequest or axios API, so they were not intercepted by axios middleware interceptor we have set up for API requests. --------- Signed-off-by: ibolton336 <[email protected]>
- Loading branch information
1 parent
bc0fe72
commit b5f715b
Showing
6 changed files
with
71 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
client/src/app/components/target-card/hooks/useFetchImageDataUrl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useState, useEffect } from "react"; | ||
import axios from "axios"; | ||
import DefaultImage from "@app/images/Icon-Red_Hat-Virtual_server_stack-A-Black-RGB.svg"; | ||
import { Target } from "@app/api/models"; | ||
import { FILES } from "@app/api/rest"; | ||
|
||
const useFetchImageDataUrl = (target: Target) => { | ||
const [imageDataUrl, setImageDataUrl] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const imagePath = target?.image?.id | ||
? `${FILES}/${target?.image.id}` | ||
: DefaultImage; | ||
|
||
(async () => { | ||
try { | ||
const response = await axios.get(imagePath, { | ||
headers: { | ||
Accept: "application/octet-stream", | ||
}, | ||
responseType: "arraybuffer", | ||
}); | ||
const contentType = response.headers["content-type"]; | ||
|
||
let imageData; | ||
|
||
if (contentType === "image/svg+xml") { | ||
const text = new TextDecoder().decode(response.data); | ||
imageData = `data:${contentType},${encodeURIComponent(text)}`; | ||
} else { | ||
const base64 = btoa( | ||
new Uint8Array(response.data).reduce( | ||
(data, byte) => data + String.fromCharCode(byte), | ||
"" | ||
) | ||
); | ||
imageData = `data:${contentType};base64,${base64}`; | ||
} | ||
|
||
setImageDataUrl(imageData); | ||
} catch (error) { | ||
console.error("There was an issue fetching the image:", error); | ||
} | ||
})(); | ||
}, [target]); | ||
|
||
return imageDataUrl; | ||
}; | ||
|
||
export default useFetchImageDataUrl; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters