Skip to content

High performance self-hosted photo and video management solution.

Random Endpoint use querybuilder and return exifInfo (#9301)

Jan535c7a8618ef
diff --git a/server/src/queries/asset.repository.sql b/server/src/queries/asset.repository.sql
index de97757f17..27a74807f8 100644
--- a/server/src/queries/asset.repository.sql
+++ b/server/src/queries/asset.repository.sql
@@ -545,6 +545,111 @@ ORDER BY
LIMIT
11
+-- AssetRepository.getRandom
+SELECT
+ "asset"."id" AS "asset_id",
+ "asset"."deviceAssetId" AS "asset_deviceAssetId",
+ "asset"."ownerId" AS "asset_ownerId",
+ "asset"."libraryId" AS "asset_libraryId",
+ "asset"."deviceId" AS "asset_deviceId",
+ "asset"."type" AS "asset_type",
+ "asset"."originalPath" AS "asset_originalPath",
+ "asset"."previewPath" AS "asset_previewPath",
+ "asset"."thumbnailPath" AS "asset_thumbnailPath",
+ "asset"."thumbhash" AS "asset_thumbhash",
+ "asset"."encodedVideoPath" AS "asset_encodedVideoPath",
+ "asset"."createdAt" AS "asset_createdAt",
+ "asset"."updatedAt" AS "asset_updatedAt",
+ "asset"."deletedAt" AS "asset_deletedAt",
+ "asset"."fileCreatedAt" AS "asset_fileCreatedAt",
+ "asset"."localDateTime" AS "asset_localDateTime",
+ "asset"."fileModifiedAt" AS "asset_fileModifiedAt",
+ "asset"."isFavorite" AS "asset_isFavorite",
+ "asset"."isArchived" AS "asset_isArchived",
+ "asset"."isExternal" AS "asset_isExternal",
+ "asset"."isOffline" AS "asset_isOffline",
+ "asset"."checksum" AS "asset_checksum",
+ "asset"."duration" AS "asset_duration",
+ "asset"."isVisible" AS "asset_isVisible",
+ "asset"."livePhotoVideoId" AS "asset_livePhotoVideoId",
+ "asset"."originalFileName" AS "asset_originalFileName",
+ "asset"."sidecarPath" AS "asset_sidecarPath",
+ "asset"."stackId" AS "asset_stackId",
+ "exifInfo"."assetId" AS "exifInfo_assetId",
+ "exifInfo"."description" AS "exifInfo_description",
+ "exifInfo"."exifImageWidth" AS "exifInfo_exifImageWidth",
+ "exifInfo"."exifImageHeight" AS "exifInfo_exifImageHeight",
+ "exifInfo"."fileSizeInByte" AS "exifInfo_fileSizeInByte",
+ "exifInfo"."orientation" AS "exifInfo_orientation",
+ "exifInfo"."dateTimeOriginal" AS "exifInfo_dateTimeOriginal",
+ "exifInfo"."modifyDate" AS "exifInfo_modifyDate",
+ "exifInfo"."timeZone" AS "exifInfo_timeZone",
+ "exifInfo"."latitude" AS "exifInfo_latitude",
+ "exifInfo"."longitude" AS "exifInfo_longitude",
+ "exifInfo"."projectionType" AS "exifInfo_projectionType",
+ "exifInfo"."city" AS "exifInfo_city",
+ "exifInfo"."livePhotoCID" AS "exifInfo_livePhotoCID",
+ "exifInfo"."autoStackId" AS "exifInfo_autoStackId",
+ "exifInfo"."state" AS "exifInfo_state",
+ "exifInfo"."country" AS "exifInfo_country",
+ "exifInfo"."make" AS "exifInfo_make",
+ "exifInfo"."model" AS "exifInfo_model",
+ "exifInfo"."lensModel" AS "exifInfo_lensModel",
+ "exifInfo"."fNumber" AS "exifInfo_fNumber",
+ "exifInfo"."focalLength" AS "exifInfo_focalLength",
+ "exifInfo"."iso" AS "exifInfo_iso",
+ "exifInfo"."exposureTime" AS "exifInfo_exposureTime",
+ "exifInfo"."profileDescription" AS "exifInfo_profileDescription",
+ "exifInfo"."colorspace" AS "exifInfo_colorspace",
+ "exifInfo"."bitsPerSample" AS "exifInfo_bitsPerSample",
+ "exifInfo"."fps" AS "exifInfo_fps",
+ "stack"."id" AS "stack_id",
+ "stack"."primaryAssetId" AS "stack_primaryAssetId",
+ "stackedAssets"."id" AS "stackedAssets_id",
+ "stackedAssets"."deviceAssetId" AS "stackedAssets_deviceAssetId",
+ "stackedAssets"."ownerId" AS "stackedAssets_ownerId",
+ "stackedAssets"."libraryId" AS "stackedAssets_libraryId",
+ "stackedAssets"."deviceId" AS "stackedAssets_deviceId",
+ "stackedAssets"."type" AS "stackedAssets_type",
+ "stackedAssets"."originalPath" AS "stackedAssets_originalPath",
+ "stackedAssets"."previewPath" AS "stackedAssets_previewPath",
+ "stackedAssets"."thumbnailPath" AS "stackedAssets_thumbnailPath",
+ "stackedAssets"."thumbhash" AS "stackedAssets_thumbhash",
+ "stackedAssets"."encodedVideoPath" AS "stackedAssets_encodedVideoPath",
+ "stackedAssets"."createdAt" AS "stackedAssets_createdAt",
+ "stackedAssets"."updatedAt" AS "stackedAssets_updatedAt",
+ "stackedAssets"."deletedAt" AS "stackedAssets_deletedAt",
+ "stackedAssets"."fileCreatedAt" AS "stackedAssets_fileCreatedAt",
+ "stackedAssets"."localDateTime" AS "stackedAssets_localDateTime",
+ "stackedAssets"."fileModifiedAt" AS "stackedAssets_fileModifiedAt",
+ "stackedAssets"."isFavorite" AS "stackedAssets_isFavorite",
+ "stackedAssets"."isArchived" AS "stackedAssets_isArchived",
+ "stackedAssets"."isExternal" AS "stackedAssets_isExternal",
+ "stackedAssets"."isOffline" AS "stackedAssets_isOffline",
+ "stackedAssets"."checksum" AS "stackedAssets_checksum",
+ "stackedAssets"."duration" AS "stackedAssets_duration",
+ "stackedAssets"."isVisible" AS "stackedAssets_isVisible",
+ "stackedAssets"."livePhotoVideoId" AS "stackedAssets_livePhotoVideoId",
+ "stackedAssets"."originalFileName" AS "stackedAssets_originalFileName",
+ "stackedAssets"."sidecarPath" AS "stackedAssets_sidecarPath",
+ "stackedAssets"."stackId" AS "stackedAssets_stackId"
+FROM
+ "assets" "asset"
+ LEFT JOIN "exif" "exifInfo" ON "exifInfo"."assetId" = "asset"."id"
+ LEFT JOIN "asset_stack" "stack" ON "stack"."id" = "asset"."stackId"
+ LEFT JOIN "assets" "stackedAssets" ON "stackedAssets"."stackId" = "stack"."id"
+ AND ("stackedAssets"."deletedAt" IS NULL)
+WHERE
+ (
+ "asset"."isVisible" = true
+ AND "asset"."ownerId" IN ($1)
+ )
+ AND ("asset"."deletedAt" IS NULL)
+ORDER BY
+ RANDOM() ASC
+LIMIT
+ 50
+
-- AssetRepository.getTimeBuckets
SELECT
COUNT("asset"."id")::int AS "count",
diff --git a/server/src/repositories/asset.repository.ts b/server/src/repositories/asset.repository.ts
index dfc5e2bfcb..7c359aa895 100644
--- a/server/src/repositories/asset.repository.ts
+++ b/server/src/repositories/asset.repository.ts
@@ -571,15 +571,14 @@ export class AssetRepository implements IAssetRepository {
return result;
}
+ @GenerateSql({ params: [DummyValue.UUID, DummyValue.NUMBER] })
getRandom(ownerId: string, count: number): Promise<AssetEntity[]> {
- // can't use queryBuilder because of custom OFFSET clause
- return this.repository.query(
- `SELECT *
- FROM assets
- WHERE "ownerId" = $1
- OFFSET FLOOR(RANDOM() * (SELECT GREATEST(COUNT(*) - $2, 0) FROM ASSETS WHERE "ownerId" = $1)) LIMIT $2`,
- [ownerId, count],
- );
+ const builder = this.getBuilder({
+ userIds: [ownerId],
+ exifInfo: true,
+ });
+
+ return builder.orderBy('RANDOM()').limit(count).getMany();
}
@GenerateSql({ params: [{ size: TimeBucketSize.MONTH }] })