forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

avatar.ts27 lines, 1.1 KiBHistoryRawDownload
1import { createSignal } from "solid-js";
2
3// The whole set the avatar endpoint accepts — deliberately narrower than
4// limits.file.image_content_types, which also carries SVG for other uploads
5// and would be refused here.
6export const AVATAR_CONTENT_TYPES = ["image/png", "image/jpeg", "image/webp", "image/gif"];
7
8export const AVATAR_ACCEPT = AVATAR_CONTENT_TYPES.join(",");
9
10// GET /users/{id}/avatar answers at one fixed URL, so an upload or a delete
11// leaves the address unchanged and the browser's in-memory image cache can go
12// on serving the old bytes. Every mounted avatar reads this counter into its
13// `?v=` query, so bumping it once re-requests all of them — which is also how
14// the sidebar chip picks up a photo changed on the profile page.
15const [avatarRevision, setAvatarRevision] = createSignal(0);
16
17export { avatarRevision };
18
19export function bumpAvatarRevision(): void {
20 setAvatarRevision((n) => n + 1);
21}
22
23export function formatBytes(bytes: number): string {
24 if (bytes >= 1024 * 1024) return `${Math.round(bytes / (1024 * 1024))} MB`;
25 if (bytes >= 1024) return `${Math.round(bytes / 1024)} KB`;
26 return `${bytes} B`;
27}