Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

feat(database): add photo access keys table and triggers for managing photo identifiers

Ahmet Ömere09570806fb1
diff --git a/server/database/migrations/0014_photo_access_keys.sql b/server/database/migrations/0014_photo_access_keys.sql
new file mode 100644
index 00000000..e6e0d4da
--- /dev/null
+++ b/server/database/migrations/0014_photo_access_keys.sql
@@ -0,0 +1,210 @@
+CREATE TABLE `photo_access_keys` (
+ `access_key` text NOT NULL,
+ `photo_id` text NOT NULL,
+ PRIMARY KEY(`access_key`, `photo_id`),
+ FOREIGN KEY (`photo_id`) REFERENCES `photos`(`id`) ON UPDATE no action ON DELETE cascade
+);
+--> statement-breakpoint
+CREATE INDEX `idx_photo_access_keys_photo_id` ON `photo_access_keys` (`photo_id`);
+--> statement-breakpoint
+-- Keep photo_access_keys aligned with photos. Storage keys stay as the object
+-- provider stored them; only this lookup table holds the tidied aliases.
+-- Tidying matches the local storage provider's sanitizeKey, and also strips a single
+-- /storage/ or /image/ prefix so a file request can hit the public URL.
+-- Absolute http(s) URLs are stored unchanged (`https://` must not collapse).
+CREATE TRIGGER `photos_access_keys_insert`
+AFTER INSERT ON `photos`
+BEGIN
+ INSERT OR IGNORE INTO `photo_access_keys` (`access_key`, `photo_id`)
+ WITH RECURSIVE
+ `raw`(`value`, `kind`) AS (
+ SELECT NEW.`id`, 'id'
+ UNION ALL SELECT NEW.`storage_key`, 'path'
+ UNION ALL SELECT NEW.`thumbnail_key`, 'path'
+ UNION ALL SELECT NEW.`original_url`, 'path'
+ UNION ALL SELECT NEW.`thumbnail_url`, 'path'
+ UNION ALL SELECT NEW.`live_photo_video_key`, 'path'
+ UNION ALL SELECT NEW.`live_photo_video_url`, 'path'
+ ),
+ `present`(`value`, `kind`) AS (
+ SELECT `value`, `kind` FROM `raw`
+ WHERE `value` IS NOT NULL AND `value` != ''
+ ),
+ `slashed`(`value`, `kind`, `absolute`, `step`) AS (
+ SELECT
+ CASE
+ WHEN `kind` = 'id' THEN `value`
+ WHEN lower(`value`) LIKE 'http://%' OR lower(`value`) LIKE 'https://%' THEN `value`
+ ELSE replace(`value`, char(92), '/')
+ END,
+ `kind`,
+ CASE
+ WHEN `kind` = 'id' THEN 1
+ WHEN lower(`value`) LIKE 'http://%' OR lower(`value`) LIKE 'https://%' THEN 1
+ ELSE 0
+ END,
+ 0
+ FROM `present`
+ UNION ALL
+ SELECT replace(`value`, '//', '/'), `kind`, `absolute`, `step` + 1
+ FROM `slashed`
+ WHERE `absolute` = 0 AND instr(`value`, '//') > 0 AND `step` < 64
+ ),
+ `stable`(`value`, `kind`, `absolute`) AS (
+ SELECT `value`, `kind`, `absolute` FROM `slashed`
+ WHERE `absolute` = 1 OR instr(`value`, '//') = 0 OR `step` = 64
+ ),
+ `derived`(`access_key`) AS (
+ SELECT `value` FROM `present`
+ UNION ALL
+ SELECT ltrim(`value`, '/') FROM `stable`
+ WHERE `absolute` = 0 AND ltrim(`value`, '/') != ''
+ UNION ALL
+ SELECT ltrim(substr(`value`, 10), '/') FROM `stable`
+ WHERE `absolute` = 0
+ AND `kind` = 'path'
+ AND `value` LIKE '/storage/%'
+ AND ltrim(substr(`value`, 10), '/') != ''
+ UNION ALL
+ SELECT ltrim(substr(`value`, 8), '/') FROM `stable`
+ WHERE `absolute` = 0
+ AND `kind` = 'path'
+ AND `value` LIKE '/image/%'
+ AND ltrim(substr(`value`, 8), '/') != ''
+ )
+ SELECT DISTINCT `access_key`, NEW.`id` FROM `derived`
+ WHERE `access_key` IS NOT NULL AND `access_key` != '';
+END;
+--> statement-breakpoint
+CREATE TRIGGER `photos_access_keys_update`
+AFTER UPDATE ON `photos`
+BEGIN
+ DELETE FROM `photo_access_keys` WHERE `photo_id` = OLD.`id`;
+ INSERT OR IGNORE INTO `photo_access_keys` (`access_key`, `photo_id`)
+ WITH RECURSIVE
+ `raw`(`value`, `kind`) AS (
+ SELECT NEW.`id`, 'id'
+ UNION ALL SELECT NEW.`storage_key`, 'path'
+ UNION ALL SELECT NEW.`thumbnail_key`, 'path'
+ UNION ALL SELECT NEW.`original_url`, 'path'
+ UNION ALL SELECT NEW.`thumbnail_url`, 'path'
+ UNION ALL SELECT NEW.`live_photo_video_key`, 'path'
+ UNION ALL SELECT NEW.`live_photo_video_url`, 'path'
+ ),
+ `present`(`value`, `kind`) AS (
+ SELECT `value`, `kind` FROM `raw`
+ WHERE `value` IS NOT NULL AND `value` != ''
+ ),
+ `slashed`(`value`, `kind`, `absolute`, `step`) AS (
+ SELECT
+ CASE
+ WHEN `kind` = 'id' THEN `value`
+ WHEN lower(`value`) LIKE 'http://%' OR lower(`value`) LIKE 'https://%' THEN `value`
+ ELSE replace(`value`, char(92), '/')
+ END,
+ `kind`,
+ CASE
+ WHEN `kind` = 'id' THEN 1
+ WHEN lower(`value`) LIKE 'http://%' OR lower(`value`) LIKE 'https://%' THEN 1
+ ELSE 0
+ END,
+ 0
+ FROM `present`
+ UNION ALL
+ SELECT replace(`value`, '//', '/'), `kind`, `absolute`, `step` + 1
+ FROM `slashed`
+ WHERE `absolute` = 0 AND instr(`value`, '//') > 0 AND `step` < 64
+ ),
+ `stable`(`value`, `kind`, `absolute`) AS (
+ SELECT `value`, `kind`, `absolute` FROM `slashed`
+ WHERE `absolute` = 1 OR instr(`value`, '//') = 0 OR `step` = 64
+ ),
+ `derived`(`access_key`) AS (
+ SELECT `value` FROM `present`
+ UNION ALL
+ SELECT ltrim(`value`, '/') FROM `stable`
+ WHERE `absolute` = 0 AND ltrim(`value`, '/') != ''
+ UNION ALL
+ SELECT ltrim(substr(`value`, 10), '/') FROM `stable`
+ WHERE `absolute` = 0
+ AND `kind` = 'path'
+ AND `value` LIKE '/storage/%'
+ AND ltrim(substr(`value`, 10), '/') != ''
+ UNION ALL
+ SELECT ltrim(substr(`value`, 8), '/') FROM `stable`
+ WHERE `absolute` = 0
+ AND `kind` = 'path'
+ AND `value` LIKE '/image/%'
+ AND ltrim(substr(`value`, 8), '/') != ''
+ )
+ SELECT DISTINCT `access_key`, NEW.`id` FROM `derived`
+ WHERE `access_key` IS NOT NULL AND `access_key` != '';
+END;
+--> statement-breakpoint
+CREATE TRIGGER `photos_access_keys_delete`
+AFTER DELETE ON `photos`
+BEGIN
+ DELETE FROM `photo_access_keys` WHERE `photo_id` = OLD.`id`;
+END;
+--> statement-breakpoint
+-- One-off backfill. Read path columns once; do not rewrite photo rows.
+INSERT OR IGNORE INTO `photo_access_keys` (`access_key`, `photo_id`)
+WITH RECURSIVE
+`raw`(`photo_id`, `value`, `kind`) AS (
+ SELECT `id`, `id`, 'id' FROM `photos`
+ UNION ALL SELECT `id`, `storage_key`, 'path' FROM `photos`
+ UNION ALL SELECT `id`, `thumbnail_key`, 'path' FROM `photos`
+ UNION ALL SELECT `id`, `original_url`, 'path' FROM `photos`
+ UNION ALL SELECT `id`, `thumbnail_url`, 'path' FROM `photos`
+ UNION ALL SELECT `id`, `live_photo_video_key`, 'path' FROM `photos`
+ UNION ALL SELECT `id`, `live_photo_video_url`, 'path' FROM `photos`
+),
+`present`(`photo_id`, `value`, `kind`) AS (
+ SELECT `photo_id`, `value`, `kind` FROM `raw`
+ WHERE `value` IS NOT NULL AND `value` != ''
+),
+`slashed`(`photo_id`, `value`, `kind`, `absolute`, `step`) AS (
+ SELECT
+ `photo_id`,
+ CASE
+ WHEN `kind` = 'id' THEN `value`
+ WHEN lower(`value`) LIKE 'http://%' OR lower(`value`) LIKE 'https://%' THEN `value`
+ ELSE replace(`value`, char(92), '/')
+ END,
+ `kind`,
+ CASE
+ WHEN `kind` = 'id' THEN 1
+ WHEN lower(`value`) LIKE 'http://%' OR lower(`value`) LIKE 'https://%' THEN 1
+ ELSE 0
+ END,
+ 0
+ FROM `present`
+ UNION ALL
+ SELECT `photo_id`, replace(`value`, '//', '/'), `kind`, `absolute`, `step` + 1
+ FROM `slashed`
+ WHERE `absolute` = 0 AND instr(`value`, '//') > 0 AND `step` < 64
+),
+`stable`(`photo_id`, `value`, `kind`, `absolute`) AS (
+ SELECT `photo_id`, `value`, `kind`, `absolute` FROM `slashed`
+ WHERE `absolute` = 1 OR instr(`value`, '//') = 0 OR `step` = 64
+),
+`derived`(`photo_id`, `access_key`) AS (
+ SELECT `photo_id`, `value` FROM `present`
+ UNION ALL
+ SELECT `photo_id`, ltrim(`value`, '/') FROM `stable`
+ WHERE `absolute` = 0 AND ltrim(`value`, '/') != ''
+ UNION ALL
+ SELECT `photo_id`, ltrim(substr(`value`, 10), '/') FROM `stable`
+ WHERE `absolute` = 0
+ AND `kind` = 'path'
+ AND `value` LIKE '/storage/%'
+ AND ltrim(substr(`value`, 10), '/') != ''
+ UNION ALL
+ SELECT `photo_id`, ltrim(substr(`value`, 8), '/') FROM `stable`
+ WHERE `absolute` = 0
+ AND `kind` = 'path'
+ AND `value` LIKE '/image/%'
+ AND ltrim(substr(`value`, 8), '/') != ''
+)
+SELECT DISTINCT `access_key`, `photo_id` FROM `derived`
+WHERE `access_key` IS NOT NULL AND `access_key` != '';
\ No newline at end of file
diff --git a/server/database/migrations/meta/0014_snapshot.json b/server/database/migrations/meta/0014_snapshot.json
new file mode 100644
index 00000000..92b73f82
--- /dev/null
+++ b/server/database/migrations/meta/0014_snapshot.json
@@ -0,0 +1,857 @@
+{
+ "version": "6",
+ "dialect": "sqlite",
+ "id": "aafdad2d-343f-49bc-964b-7c914f3877f3",
+ "prevId": "58c6ce07-d486-4373-ba86-cfe4add7a94e",
+ "tables": {
+ "album_photos": {
+ "name": "album_photos",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "album_id": {
+ "name": "album_id",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "photo_id": {
+ "name": "photo_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "position": {
+ "name": "position",
+ "type": "real",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 1000000
+ },
+ "added_at": {
+ "name": "added_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {
+ "album_photos_album_id_albums_id_fk": {
+ "name": "album_photos_album_id_albums_id_fk",
+ "tableFrom": "album_photos",
+ "tableTo": "albums",
+ "columnsFrom": [
+ "album_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "no action"
+ },
+ "album_photos_photo_id_photos_id_fk": {
+ "name": "album_photos_photo_id_photos_id_fk",
+ "tableFrom": "album_photos",
+ "tableTo": "photos",
+ "columnsFrom": [
+ "photo_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "no action"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "albums": {
+ "name": "albums",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "title": {
+ "name": "title",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "description": {
+ "name": "description",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "cover_photo_id": {
+ "name": "cover_photo_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "is_hidden": {
+ "name": "is_hidden",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "password_hash": {
+ "name": "password_hash",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "position": {
+ "name": "position",
+ "type": "real",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {
+ "albums_cover_photo_id_photos_id_fk": {
+ "name": "albums_cover_photo_id_photos_id_fk",
+ "tableFrom": "albums",
+ "tableTo": "photos",
+ "columnsFrom": [
+ "cover_photo_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "set null",
+ "onUpdate": "no action"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "photo_access_keys": {
+ "name": "photo_access_keys",
+ "columns": {
+ "access_key": {
+ "name": "access_key",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "photo_id": {
+ "name": "photo_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ }
+ },
+ "indexes": {
+ "idx_photo_access_keys_photo_id": {
+ "name": "idx_photo_access_keys_photo_id",
+ "columns": [
+ "photo_id"
+ ],
+ "isUnique": false
+ }
+ },
+ "foreignKeys": {
+ "photo_access_keys_photo_id_photos_id_fk": {
+ "name": "photo_access_keys_photo_id_photos_id_fk",
+ "tableFrom": "photo_access_keys",
+ "tableTo": "photos",
+ "columnsFrom": [
+ "photo_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "no action"
+ }
+ },
+ "compositePrimaryKeys": {
+ "photo_access_keys_access_key_photo_id_pk": {
+ "columns": [
+ "access_key",
+ "photo_id"
+ ],
+ "name": "photo_access_keys_access_key_photo_id_pk"
+ }
+ },
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "photo_reactions": {
+ "name": "photo_reactions",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "photo_id": {
+ "name": "photo_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "reaction_type": {
+ "name": "reaction_type",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "fingerprint": {
+ "name": "fingerprint",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "ip_address": {
+ "name": "ip_address",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "user_agent": {
+ "name": "user_agent",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {
+ "photo_reactions_photo_id_photos_id_fk": {
+ "name": "photo_reactions_photo_id_photos_id_fk",
+ "tableFrom": "photo_reactions",
+ "tableTo": "photos",
+ "columnsFrom": [
+ "photo_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "no action"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "photos": {
+ "name": "photos",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "title": {
+ "name": "title",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "description": {
+ "name": "description",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "width": {
+ "name": "width",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "height": {
+ "name": "height",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "aspect_ratio": {
+ "name": "aspect_ratio",
+ "type": "real",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "date_taken": {
+ "name": "date_taken",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "storage_key": {
+ "name": "storage_key",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "thumbnail_key": {
+ "name": "thumbnail_key",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "file_size": {
+ "name": "file_size",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "last_modified": {
+ "name": "last_modified",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "original_url": {
+ "name": "original_url",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "thumbnail_url": {
+ "name": "thumbnail_url",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "thumbnail_hash": {
+ "name": "thumbnail_hash",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "tags": {
+ "name": "tags",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "exif": {
+ "name": "exif",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "latitude": {
+ "name": "latitude",
+ "type": "real",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "longitude": {
+ "name": "longitude",
+ "type": "real",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "country": {
+ "name": "country",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "city": {
+ "name": "city",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "location_name": {
+ "name": "location_name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "is_live_photo": {
+ "name": "is_live_photo",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "live_photo_video_url": {
+ "name": "live_photo_video_url",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "live_photo_video_key": {
+ "name": "live_photo_video_key",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ }
+ },
+ "indexes": {
+ "photos_id_unique": {
+ "name": "photos_id_unique",
+ "columns": [
+ "id"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "pipeline_queue": {
+ "name": "pipeline_queue",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "payload": {
+ "name": "payload",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'{\"type\":\"photo\",\"storageKey\":\"\"}'"
+ },
+ "priority": {
+ "name": "priority",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "attempts": {
+ "name": "attempts",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "max_attempts": {
+ "name": "max_attempts",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 3
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'pending'"
+ },
+ "status_stage": {
+ "name": "status_stage",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "error_message": {
+ "name": "error_message",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "completed_at": {
+ "name": "completed_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "settings": {
+ "name": "settings",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "namespace": {
+ "name": "namespace",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'common'"
+ },
+ "key": {
+ "name": "key",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "type": {
+ "name": "type",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "value": {
+ "name": "value",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "default_value": {
+ "name": "default_value",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "label": {
+ "name": "label",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "description": {
+ "name": "description",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "is_public": {
+ "name": "is_public",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_readonly": {
+ "name": "is_readonly",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_secret": {
+ "name": "is_secret",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "enum": {
+ "name": "enum",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_by": {
+ "name": "updated_by",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ }
+ },
+ "indexes": {
+ "idx_namespace_key": {
+ "name": "idx_namespace_key",
+ "columns": [
+ "namespace",
+ "key"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {
+ "settings_updated_by_users_id_fk": {
+ "name": "settings_updated_by_users_id_fk",
+ "tableFrom": "settings",
+ "tableTo": "users",
+ "columnsFrom": [
+ "updated_by"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "set null",
+ "onUpdate": "no action"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "settings_storage_providers": {
+ "name": "settings_storage_providers",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "provider": {
+ "name": "provider",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "config": {
+ "name": "config",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "users": {
+ "name": "users",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "email": {
+ "name": "email",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "password": {
+ "name": "password",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "avatar": {
+ "name": "avatar",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "is_admin": {
+ "name": "is_admin",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ }
+ },
+ "indexes": {
+ "users_name_unique": {
+ "name": "users_name_unique",
+ "columns": [
+ "name"
+ ],
+ "isUnique": true
+ },
+ "users_email_unique": {
+ "name": "users_email_unique",
+ "columns": [
+ "email"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ }
+ },
+ "views": {},
+ "enums": {},
+ "_meta": {
+ "schemas": {},
+ "tables": {},
+ "columns": {}
+ },
+ "internal": {
+ "indexes": {}
+ }
+}
\ No newline at end of file
diff --git a/server/database/migrations/meta/_journal.json b/server/database/migrations/meta/_journal.json
index c99f7e10..e17a7b1e 100644
--- a/server/database/migrations/meta/_journal.json
+++ b/server/database/migrations/meta/_journal.json
@@ -99,6 +99,13 @@
"when": 1790848090071,
"tag": "0013_empty_vermin",
"breakpoints": true
+ },
+ {
+ "idx": 14,
+ "version": "6",
+ "when": 1791384033370,
+ "tag": "0014_photo_access_keys",
+ "breakpoints": true
}
]
}
\ No newline at end of file
diff --git a/server/database/schema.ts b/server/database/schema.ts
index 4d5cbca1..9edf6bfe 100644
--- a/server/database/schema.ts
+++ b/server/database/schema.ts
@@ -1,9 +1,11 @@
import { sql } from 'drizzle-orm'
import {
- sqliteTable,
- text,
+ index,
integer,
+ primaryKey,
real,
+ sqliteTable,
+ text,
uniqueIndex,
} from 'drizzle-orm/sqlite-core'
import type { NeededExif } from '~~/shared/types/photo'
@@ -69,6 +71,24 @@ export const photos = sqliteTable('photos', {
livePhotoVideoKey: text('live_photo_video_key'),
})
+// One row per string an image request may use to identify a photo: the photo
+// id, each storage key, each public URL, and the tidied form of those paths.
+// Photos insert/update/delete triggers (migration 0014) keep the rows in sync.
+// Album checks look up this indexed key instead of reading every photos row.
+export const photoAccessKeys = sqliteTable(
+ 'photo_access_keys',
+ {
+ accessKey: text('access_key').notNull(),
+ photoId: text('photo_id')
+ .notNull()
+ .references(() => photos.id, { onDelete: 'cascade' }),
+ },
+ (t) => [
+ primaryKey({ columns: [t.accessKey, t.photoId] }),
+ index('idx_photo_access_keys_photo_id').on(t.photoId),
+ ],
+)
+
export const pipelineQueue = sqliteTable('pipeline_queue', {
id: integer('id').primaryKey({ autoIncrement: true }),
payload: text('payload', { mode: 'json' })
diff --git a/server/utils/album-access.ts b/server/utils/album-access.ts
index a11fe885..03942339 100644
--- a/server/utils/album-access.ts
+++ b/server/utils/album-access.ts
@@ -6,7 +6,7 @@ import {
timingSafeEqual,
} from 'node:crypto'
import { promisify } from 'node:util'
-import { eq, or } from 'drizzle-orm'
+import { eq } from 'drizzle-orm'
import type { H3Event } from 'h3'
import { tables, useDB } from './db'
@@ -150,47 +150,14 @@ 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' })