Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 182x 181x 182x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { eq } from 'drizzle-orm';
import type { Database } from './db/client.ts';
import { profile } from './db/schema.ts';
import { prepareAvatarUpload, type AvatarUpload } from '@be-music/service-avatar';
export {
AVATAR_MAX_BYTES,
detectAvatarType,
generatedAvatarSVG,
type AvatarType,
type AvatarUpload,
} from '@be-music/service-avatar';
/** The URL of a player's icon; the version (the upload's hash, or `g` for the generated one) busts caches on change. */
export function avatarURL(handle: string, avatarKey: string | null): string {
if (avatarKey?.startsWith('https://')) return avatarKey;
const version = avatarKey === null ? 'g' : (/\/([0-9a-f]{16})[0-9a-f]*\.\w+$/.exec(avatarKey)?.[1] ?? 'u');
return `/players/${encodeURIComponent(handle)}/avatar?v=${version}`;
}
/** Stores an uploaded icon in R2 and points the profile at it; the previous upload is deleted. */
export async function putAvatar(
db: Database,
blobs: R2Bucket,
userId: string,
bytes: Uint8Array,
): Promise<AvatarUpload> {
const upload = await prepareAvatarUpload(userId, bytes);
if (!upload.ok) return upload;
const { avatarKey, type } = upload;
const current = await db.query.profile.findFirst({ where: eq(profile.userId, userId), columns: { avatarKey: true } });
await blobs.put(avatarKey, bytes, { httpMetadata: { contentType: type } });
await db.update(profile).set({ avatarKey }).where(eq(profile.userId, userId));
Iif (current?.avatarKey != null && current.avatarKey !== avatarKey) await blobs.delete(current.avatarKey);
return { ok: true, avatarKey };
}
/** Goes back to the generated icon. */
export async function removeAvatar(db: Database, blobs: R2Bucket, userId: string): Promise<void> {
const current = await db.query.profile.findFirst({ where: eq(profile.userId, userId), columns: { avatarKey: true } });
await db.update(profile).set({ avatarKey: null }).where(eq(profile.userId, userId));
Eif (current?.avatarKey != null) await blobs.delete(current.avatarKey);
}
|