Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show images on cluck dashboard via proxying images through cluck server #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/frontend/dash/chiefdelphi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ async function refreshDelphi() {
}
async function refreshImage() {
const url = await (await fetch(getResourceURL('/dash/image'))).text();
(document.querySelector('.theimage') as HTMLImageElement).src = url;
(document.querySelector('.theimage') as HTMLImageElement).src =
`/dash/imageproxy?googleurl=${encodeURIComponent(url)}`;
}

export function setPanelType(type: PanelType) {
Expand Down
39 changes: 39 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,45 @@ router.get('/dash/image', async (req, res, next) => {
next(e)
}
})
router.get('/dash/imageproxy', async (req, res, next) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not return the image blob from /dash/image directly? It already has the image url and it also lets you avoid making user-specified fetch requests server-side

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯 Great idea. What if you or the app sw managers gave that task to a new sw person so they can learn how cluck works for when you are gone?

try{
let url = req.query.googleurl as string;
let response = await fetch(url, {
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache",
"pragma": "no-cache",
"priority": "u=0, i",
"sec-ch-ua": "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Google Chrome\";v=\"126\"",
"sec-ch-ua-arch": "\"arm\"",
"sec-ch-ua-bitness": "\"64\"",
"sec-ch-ua-full-version-list": "\"Not/A)Brand\";v=\"8.0.0.0\", \"Chromium\";v=\"126.0.6478.127\", \"Google Chrome\";v=\"126.0.6478.127\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-model": "\"\"",
"sec-ch-ua-platform": "\"macOS\"",
"sec-ch-ua-platform-version": "\"14.2.1\"",
"sec-ch-ua-wow64": "?0",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
},
"referrerPolicy": "strict-origin-when-cross-origin",
"body": null,
"method": "GET"
});
let blob = await response.blob();
// source https://stackoverflow.com/questions/52665103/using-express-how-to-send-blob-object-as-response
blob.arrayBuffer().then((buf) => {
res.send(Buffer.from(buf))
})
} catch (e) {
console.error(e)
next(e)
}
})
router.get('/dash/delphi', async (req, res) => {
delphiPost++; delphiPost %= 20; // switch to next post

Expand Down
2 changes: 1 addition & 1 deletion www/dash/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<a><h1 id="delphiTitle"></h1></a>
<h3 id="delphiBody"></h3>
<div id="bottom_fade"></div>
<img class="theimage" src="https://drive.google.com/uc?id=19ysfpPy6dT3m2SD24Cz6A-OuGN89nAkv">
<img class="theimage" src="/dash/imageproxy?googleurl=https://drive.google.com/uc?id=19ysfpPy6dT3m2SD24Cz6A-OuGN89nAkv">
</div>
</div>
</div>
Expand Down