mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
feat(synology): photos attached before the fix get their own capture time and place once
Until #3019 a Synology photo's capture time and GPS came from the item after it, which on many libraries is the next photo, and no one can edit a photo's capture time to put it right. On the first start after this, every Synology photo is asked again through its owner's library, held to the backfill's few lookups per NAS, and what Synology confirms for that very photo replaces what was stored; a photo it cannot confirm keeps its values. While a library could not answer, the pass runs again on the next start, at most three times.
This commit is contained in:
@@ -142,6 +142,46 @@ export class TrekPhotosRepository extends TrekRepository<TrekPhotos> {
|
||||
return changed > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts in the capture time and place a provider has just confirmed for this
|
||||
* very photo, over whatever the row held (#3019: Synology rows that carry the
|
||||
* next photo's). Unlike patchCaptureMetadata it replaces: the coordinates
|
||||
* become the confirmed pair or none at all, while a missing time leaves the
|
||||
* stored one, since every library photo has one and none is no correction.
|
||||
* Answers whether the row changed.
|
||||
*/
|
||||
async replaceCaptureMetadata(
|
||||
id: number,
|
||||
taken_at: string | null,
|
||||
lat: number | null,
|
||||
lng: number | null,
|
||||
): Promise<boolean> {
|
||||
const row = await this.findOne({ id });
|
||||
if (!row) return false;
|
||||
const next = { taken_at: taken_at ?? row.taken_at ?? null, lat, lng };
|
||||
if (next.taken_at === (row.taken_at ?? null) && next.lat === (row.lat ?? null) && next.lng === (row.lng ?? null)) {
|
||||
return false;
|
||||
}
|
||||
await this.nativeUpdate({ id }, next);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* One provider's photos after `afterId`, oldest first, `limit` at a time, with
|
||||
* the user whose library each lives in: the pages a provider-wide repair walks.
|
||||
*/
|
||||
async listByProviderAfter(
|
||||
provider: string,
|
||||
afterId: number,
|
||||
limit: number,
|
||||
): Promise<Array<{ id: number; owner_id: number }>> {
|
||||
const rows = await this.find(
|
||||
{ provider, id: { $gt: afterId }, owner: { $ne: null }, asset_id: { $ne: null } },
|
||||
{ orderBy: { id: 'asc' }, limit },
|
||||
);
|
||||
return rows.flatMap((row) => (row.owner ? [{ id: row.id, owner_id: row.owner.id }] : []));
|
||||
}
|
||||
|
||||
/** PH11 — `DELETE FROM trek_photos WHERE id = ? AND provider != 'local'`; local photos are never auto-reclaimed here. */
|
||||
async deleteNonLocal(id: number): Promise<number> {
|
||||
return await this.nativeDelete({ id, provider: { $ne: 'local' } });
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { AppSettings } from '../../db/entities/AppSettings.entity';
|
||||
import { JourneyContributors } from '../../db/entities/JourneyContributors.entity';
|
||||
import { JourneyPhotos } from '../../db/entities/JourneyPhotos.entity';
|
||||
import { Journeys } from '../../db/entities/Journeys.entity';
|
||||
@@ -28,6 +29,7 @@ import { PhotoProviderRegistry } from './photo-provider.registry';
|
||||
import { PhotoResolverService } from './photo-resolver.service';
|
||||
import { ImmichPhotoProvider } from './providers/immich.provider';
|
||||
import { SynologyPhotoProvider } from './providers/synology.provider';
|
||||
import { SynologyCaptureRepairJob } from './synology-capture-repair.job';
|
||||
import { SynologyMemoriesController } from './synology.controller';
|
||||
import { SynologyService } from './synology.service';
|
||||
import { ThumbnailService } from './thumbnail.service';
|
||||
@@ -84,6 +86,7 @@ import { Module } from '@nestjs/common';
|
||||
StorageModule,
|
||||
UserConnectionRepositoriesModule,
|
||||
MikroOrmModule.forFeature([
|
||||
AppSettings,
|
||||
TripPhotos,
|
||||
TrekPhotos,
|
||||
TripAlbumLinks,
|
||||
@@ -109,6 +112,7 @@ import { Module } from '@nestjs/common';
|
||||
TrekPhotoCacheService,
|
||||
TrekPhotoCacheJob,
|
||||
JourneyThumbsJob,
|
||||
SynologyCaptureRepairJob,
|
||||
ImmichPhotoProvider,
|
||||
SynologyPhotoProvider,
|
||||
PhotoProviderRegistry,
|
||||
|
||||
@@ -215,6 +215,30 @@ export class PhotoCaptureBackfillService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks the provider again about a photo whose stored capture time and place
|
||||
* may belong to another one, and puts in what it confirms (#3019). Inside the
|
||||
* owner's lookup slots like an import, so a repair over a whole library never
|
||||
* has more than BACKFILL_CONCURRENCY calls open against one NAS. `failed` is
|
||||
* any photo the provider did not confirm: unreachable, gone, or no item whose
|
||||
* thumbnail matches. Its row is left as it was.
|
||||
*/
|
||||
async reconfirm(id: number, ownerId: number): Promise<'changed' | 'same' | 'failed'> {
|
||||
try {
|
||||
const info = await this.withLookupSlot(ownerId, () => this.resolver.getPhotoInfo(ownerId, id));
|
||||
if (!info.success) return 'failed';
|
||||
const changed = await this.photos.replaceCaptureMetadata(id, {
|
||||
takenAt: info.data.takenAt ?? null,
|
||||
lat: info.data.lat ?? null,
|
||||
lng: info.data.lng ?? null,
|
||||
});
|
||||
return changed ? 'changed' : 'same';
|
||||
} catch (err) {
|
||||
console.error(`[Photos] capture reconfirm failed for ${id}:`, err instanceof Error ? err.message : err);
|
||||
return 'failed';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* EXIF of an uploaded file.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import { AppSettings } from '../../db/entities/AppSettings.entity';
|
||||
import { TrekPhotos } from '../../db/entities/TrekPhotos.entity';
|
||||
import type { AppSettingsRepository } from '../../db/repositories/AppSettings.repository';
|
||||
import type { TrekPhotosRepository } from '../../db/repositories/TrekPhotos.repository';
|
||||
import { logError, logInfo } from '../audit/audit-log.logger';
|
||||
import { CronRegistrarService } from '../scheduling/cron-registrar.service';
|
||||
import { PhotoCaptureBackfillService } from './photo-capture-backfill.service';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { Injectable, type OnApplicationBootstrap } from '@nestjs/common';
|
||||
|
||||
/** The app_settings row that says every Synology photo has been asked again. */
|
||||
export const SYNOLOGY_CAPTURE_REPAIR_DONE_KEY = 'synology_capture_repair_3019';
|
||||
/** How many starts have tried so far, while some photo could not be confirmed. */
|
||||
export const SYNOLOGY_CAPTURE_REPAIR_TRIES_KEY = 'synology_capture_repair_3019_tries';
|
||||
|
||||
const PROVIDER = 'synologyphotos';
|
||||
const PAGE = 200;
|
||||
/**
|
||||
* Starts that try again while photos stay unconfirmed. A NAS that was off for
|
||||
* one restart gets its photos put right on the next; one taken away for good
|
||||
* is not asked on every start forever.
|
||||
*/
|
||||
const MAX_TRIES = 3;
|
||||
|
||||
/**
|
||||
* One pass over every Synology photo on the first start after #3019.
|
||||
*
|
||||
* Until then the capture time and GPS of a Synology photo were read from the
|
||||
* item one after it, which on many libraries is the next photo: journeys came
|
||||
* out in the wrong order with pins in the wrong places, and nobody can edit a
|
||||
* photo's capture time to put it right. The lookup now names the photo itself
|
||||
* (SynologyService.getSynologyAssetInfo), and this asks again for every row,
|
||||
* through each owner's own library, and puts in what Synology confirms.
|
||||
* A photo it cannot confirm keeps what it has; on a library that was right all
|
||||
* along the answers are the same and nothing changes.
|
||||
*
|
||||
* Not a cron: once the stored rows are right, the fixed lookup keeps them so.
|
||||
* In the background through `CronRegistrarService.runOnBoot`, like the other
|
||||
* boot repairs: the server does not wait for it.
|
||||
*/
|
||||
@Injectable()
|
||||
export class SynologyCaptureRepairJob implements OnApplicationBootstrap {
|
||||
constructor(
|
||||
private readonly backfill: PhotoCaptureBackfillService,
|
||||
private readonly registrar: CronRegistrarService,
|
||||
@InjectRepository(AppSettings) private readonly appSettings: AppSettingsRepository,
|
||||
@InjectRepository(TrekPhotos) private readonly trekPhotos: TrekPhotosRepository,
|
||||
) {}
|
||||
|
||||
onApplicationBootstrap(): void {
|
||||
if (!this.registrar.isEnabled()) return;
|
||||
void this.registrar.runOnBoot('synology-capture-repair-boot', () => this.runOnce());
|
||||
}
|
||||
|
||||
/**
|
||||
* The pages after `after`, one after the other: each page's lookups run at
|
||||
* once, held to a few per library by the backfill's slots.
|
||||
*/
|
||||
private async walk(after: number): Promise<{ changed: number; failed: number }> {
|
||||
const page = await this.trekPhotos.listByProviderAfter(PROVIDER, after, PAGE);
|
||||
if (page.length === 0) return { changed: 0, failed: 0 };
|
||||
const outcomes = await Promise.all(page.map((photo) => this.backfill.reconfirm(photo.id, photo.owner_id)));
|
||||
const rest = await this.walk(page.at(-1)?.id ?? after);
|
||||
return {
|
||||
changed: rest.changed + outcomes.filter((o) => o === 'changed').length,
|
||||
failed: rest.failed + outcomes.filter((o) => o === 'failed').length,
|
||||
};
|
||||
}
|
||||
|
||||
async runOnce(): Promise<void> {
|
||||
try {
|
||||
if ((await this.appSettings.countKeysPresent([SYNOLOGY_CAPTURE_REPAIR_DONE_KEY])) > 0) return;
|
||||
const tries = Number(await this.appSettings.getValue(SYNOLOGY_CAPTURE_REPAIR_TRIES_KEY)) || 0;
|
||||
|
||||
const { changed, failed } = await this.walk(0);
|
||||
|
||||
if (failed > 0 && tries + 1 < MAX_TRIES) {
|
||||
await this.appSettings.upsertOrReplace(SYNOLOGY_CAPTURE_REPAIR_TRIES_KEY, String(tries + 1));
|
||||
} else {
|
||||
await this.appSettings.upsertOrReplace(SYNOLOGY_CAPTURE_REPAIR_DONE_KEY, new Date().toISOString());
|
||||
await this.appSettings.deleteValue(SYNOLOGY_CAPTURE_REPAIR_TRIES_KEY);
|
||||
}
|
||||
if (changed + failed > 0) {
|
||||
logInfo(
|
||||
`Synology capture repair: ${changed} photo(s) given their own capture time and place, ${failed} not confirmed by their library`,
|
||||
);
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
logError(`Synology capture repair: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,6 +143,21 @@ export class TrekPhotoRegistrationService {
|
||||
return await this.trekPhotos.patchCaptureMetadata(photoId, takenAt, hasPair ? lat : null, hasPair ? lng : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The capture time and place a provider has confirmed for this very photo,
|
||||
* put in over what the row holds (#3019). Coordinates stay a pair or none:
|
||||
* a confirmed photo without GPS loses the pair a wrong lookup gave it.
|
||||
* Answers whether the row changed.
|
||||
*/
|
||||
async replaceCaptureMetadata(
|
||||
photoId: number,
|
||||
meta: { takenAt?: string | null; lat?: number | null; lng?: number | null },
|
||||
): Promise<boolean> {
|
||||
const { takenAt = null, lat = null, lng = null } = meta;
|
||||
const hasPair = Number.isFinite(lat) && Number.isFinite(lng);
|
||||
return await this.trekPhotos.replaceCaptureMetadata(photoId, takenAt, hasPair ? lat : null, hasPair ? lng : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* PH10/PH11 — drop the row once no trip and no journey references it.
|
||||
* A local photo is kept unless the caller deletes it on purpose (`local`,
|
||||
|
||||
Reference in New Issue
Block a user