Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.
TypeScript albumsexif-extractiongeocodinglive-photomotion-photophoto-galleryphoto-sharingwebgl-viewer
Merge remote-tracking branch 'upstream/main' into fix/indexed-photo-access-lookup
Ahmet Ömer468e5677776d
diff --cc server/utils/album-access.tsindex a97b5095,5560eed0..b2853113--- a/server/utils/album-access.ts+++ b/server/utils/album-access.ts@@@ -1,6 -1,6 +1,6 @@@import { createHmac, randomBytes, scrypt, timingSafeEqual } from 'node:crypto'import { promisify } from 'node:util'- import { eq } from 'drizzle-orm'-import { eq, inArray, or } from 'drizzle-orm'++import { eq, inArray } from 'drizzle-orm'import type { H3Event } from 'h3'import { tables, useDB } from './db'@@@ -145,18 -164,49 +164,16 @@@ export async function assertPhotoAccess}const db = useDB()- const values = [cleanKey, `/storage/${cleanKey}`, `/image/${cleanKey}`]- const photos = db- .select()- .from(tables.photos)- .where(- or(- ...values.flatMap((value) => [- eq(tables.photos.id, value),- eq(tables.photos.storageKey, value),- eq(tables.photos.thumbnailKey, value),- eq(tables.photos.originalUrl, value),- eq(tables.photos.thumbnailUrl, value),- eq(tables.photos.livePhotoVideoKey, value),- eq(tables.photos.livePhotoVideoUrl, value),- ]),- ),- )+ // photo_access_keys is filled when a photo is written, including tidied+ // aliases of storage keys and /storage or /image URLs. This is one index+ // lookup; it does not read photo payloads.+ const matchedPhotos = db+ .select({ id: tables.photoAccessKeys.photoId })+ .from(tables.photoAccessKeys)+ .where(eq(tables.photoAccessKeys.accessKey, cleanKey)).all()- const normalized = db.select().from(tables.photos).all()- const matches = normalized.filter((photo) =>- [- photo.id,- photo.storageKey,- photo.thumbnailKey,- photo.originalUrl,- photo.thumbnailUrl,- photo.livePhotoVideoKey,- photo.livePhotoVideoUrl,- ]- .filter((value): value is string => Boolean(value))- .some(- (value) =>- value.replace(/\\/g, '/').replace(/\/+/g, '/').replace(/^\/+/, '') ===- cleanKey,- ),- )- const matchedPhotos = [- ...new Map(- [...photos, ...matches].map((photo) => [photo.id, photo]),- ).values(),- ]if (matchedPhotos.length === 0) {- if (!admin || requireKnown) {- throw createError({ statusCode: 404, statusMessage: 'Photo not found' })- }+ if (!admin || requireKnown) throw photoNotFound()return false}let restricted = false@@@ -186,3 -227,73 +194,73 @@@}return restricted}++ const photoIdQueryChunkSize = 500++ // Indexed photo-id check with the same album rule as assertPhotoAccess.+ // Unknown ids and photos in hidden or locked albums are both omitted, so a-// guessed id cannot be confirmed. This does not call assertPhotoAccess,-// which loads every photo row.++// guessed id cannot be confirmed. Reactions are keyed by photo id, so this++// looks those ids up directly instead of matching storage keys.+ export async function filterAccessiblePhotoIds(+ event: H3Event,+ photoIds: string[],+ ): Promise<Set<string>> {+ const uniqueIds = [...new Set(photoIds.filter((id) => id.length > 0))]+ if (uniqueIds.length === 0) return new Set()++ const { admin, canAccess } = await albumAccess(event)+ const db = useDB()+ const accessible = new Set<string>()++ for (+ let offset = 0;+ offset < uniqueIds.length;+ offset += photoIdQueryChunkSize+ ) {+ const chunk = uniqueIds.slice(offset, offset + photoIdQueryChunkSize)+ const existing = db+ .select({ id: tables.photos.id })+ .from(tables.photos)+ .where(inArray(tables.photos.id, chunk))+ .all()+ if (existing.length === 0) continue++ const ids = existing.map((photo) => photo.id)+ if (admin) {+ for (const id of ids) accessible.add(id)+ continue+ }++ const rows = db+ .select({+ photoId: tables.albumPhotos.photoId,+ album: tables.albums,+ })+ .from(tables.albumPhotos)+ .innerJoin(+ tables.albums,+ eq(tables.albumPhotos.albumId, tables.albums.id),+ )+ .where(inArray(tables.albumPhotos.photoId, ids))+ .all()+ const membershipsByPhoto = new Map<string, { album: AlbumAccessTarget }[]>()+ for (const row of rows) {+ const memberships = membershipsByPhoto.get(row.photoId)+ const membership = { album: row.album }+ if (memberships) memberships.push(membership)+ else membershipsByPhoto.set(row.photoId, [membership])+ }++ for (const id of ids) {+ const memberships = membershipsByPhoto.get(id) ?? []+ if (!photoAccessDenied(admin, memberships, canAccess)) accessible.add(id)+ }+ }++ return accessible+ }++ export async function assertPhotoIdAccess(event: H3Event, photoId: string) {+ const accessible = await filterAccessiblePhotoIds(event, [photoId])+ if (!accessible.has(photoId)) throw photoNotFound()+ }