The existence check matched on filename OR path. replacePhoto regenerates both — a fresh generated filename and a fresh managed path — so a watched-folder photo that had its file replaced stopped matching either arm. The original is still sitting in the watched folder, so the next sweep imported it again and the gallery ended up holding the delivered edit AND the untouched original: the same duplicate shape external_relpath prevents for reference galleries. source_filename is now a third arm. It is the stable key here — written once at ingest by this same path and preserved across a replace by design. Rows predating migration 193 are covered by its backfill: COALESCE(original_filename, filename), and this path never wrote original_filename, so for watcher rows that resolves to the basename being compared. The query is lifted into an exported findExistingPhoto() so the test drives it rather than a copy — the thing under test IS the query, so a query-builder mock would only assert that knex was called the way the test expects. Predates the Lightroom round-trip and applies to the admin replace path too; it became reachable when #1165 brought watcher galleries into round-trip scope. Six tests against a real SQLite database. The load-bearing one fails without the change, verified by removing the arm and re-running; the other five pin what must not move — filename and path matching, the pre-193 backfill shape, a genuinely new file, and event scoping. Co-authored-by: Paul Nothaft <[email protected]>
This commit is contained in:
co-authored by
Paul Nothaft
parent
064b1bcb14
commit
6ca8baab23
@@ -63,6 +63,27 @@ function startFileWatcher() {
|
||||
logger.info('File watcher started');
|
||||
}
|
||||
|
||||
/**
|
||||
* Has this watched file already been imported into this event?
|
||||
*
|
||||
* Exported so the regression test drives this query rather than a copy of it.
|
||||
* See the caller for why source_filename is one of the arms.
|
||||
*
|
||||
* @param {number} eventId
|
||||
* @param {string} basename the file's basename on disk
|
||||
* @param {string} relativePath the path the import would store
|
||||
*/
|
||||
async function findExistingPhoto(eventId, basename, relativePath) {
|
||||
return db('photos')
|
||||
.where({ event_id: eventId })
|
||||
.where(function() {
|
||||
this.where('filename', basename)
|
||||
.orWhere('source_filename', basename)
|
||||
.orWhere('path', relativePath);
|
||||
})
|
||||
.first();
|
||||
}
|
||||
|
||||
async function processNewPhoto(filePath) {
|
||||
const relativePath = path.relative(WATCH_PATH(), filePath);
|
||||
const pathParts = relativePath.split(path.sep);
|
||||
@@ -122,14 +143,23 @@ async function processNewPhoto(filePath) {
|
||||
}
|
||||
}
|
||||
|
||||
// Check if photo already exists (by filename or path, to handle replacements)
|
||||
const existingPhoto = await db('photos')
|
||||
.where({ event_id: event.id })
|
||||
.where(function() {
|
||||
this.where('filename', path.basename(filePath))
|
||||
.orWhere('path', relativePath);
|
||||
})
|
||||
.first();
|
||||
// Check if photo already exists.
|
||||
//
|
||||
// `source_filename` is in here, not just filename/path, because a REPLACEMENT
|
||||
// changes both of those (#1226). replacePhoto generates a fresh filename and
|
||||
// a fresh managed path, so a watched-folder photo that had its file replaced
|
||||
// — through the Lightroom round-trip (#745) or the admin replace — stopped
|
||||
// matching either arm, and the next sweep imported the untouched original a
|
||||
// second time. The gallery then held the edit AND the original: the same
|
||||
// duplicate shape external_relpath prevents for reference galleries.
|
||||
//
|
||||
// source_filename is the stable key: written once at ingest (below) and
|
||||
// preserved across a replace by design. Rows predating migration 193 are
|
||||
// covered too — its backfill sets source_filename from
|
||||
// COALESCE(original_filename, filename), and this path never wrote
|
||||
// original_filename, so for watcher rows that resolves to the basename this
|
||||
// compares against.
|
||||
const existingPhoto = await findExistingPhoto(event.id, path.basename(filePath), relativePath);
|
||||
|
||||
if (!existingPhoto) {
|
||||
// Add to database
|
||||
@@ -192,4 +222,4 @@ async function removePhoto(filePath) {
|
||||
logger.info(`Removed photo: ${relativePath}`);
|
||||
}
|
||||
|
||||
module.exports = { startFileWatcher };
|
||||
module.exports = { startFileWatcher, findExistingPhoto };
|
||||
|
||||
Reference in New Issue
Block a user