Two shooters, one card format
Most weddings we cover have at least two photographers. Each one brings their own camera, and every camera names its files the same way: DSC_0001.JPG, DSC_0002.JPG, counting up from wherever the card left off. Canon writes IMG_0001.JPG, Sony and Nikon write DSC_0001.JPG, and a second body of the same brand writes exactly the same names as the first.
So on any big shoot, two different photos with the same filename are normal. Our upload code treated a matching filename as "the same file again". This post is about what that cost, and the small change that fixed it.
Why re-uploads replace in place
Re-uploading is a real workflow. An editor exports a photo, notices the skin tone is off, fixes it and exports again with the same name. The customer may already have left a comment on the first version, or the photo may already be on a share link.
If we deleted the old row and inserted a new one, the comments would go with it: media_comments and the read-tracking tables cascade on delete. Any share link pointing at that asset id would break too. So a re-upload doesn't delete anything. It finds the existing row and points it at the new file:
existing.setObjectKey(objectKey);
existing.setFileSizeBytes(fileSizeBytes);
existing.setUploadedAt(Instant.now());
existing.setUploadedByAdminEmail(uploaderEmail);
// Previews, streams and proxies came from the old bytes, so they are rebuilt.
...
deleteSupersededObject(previousObjectKey, objectKey);The id, the comments, the share links and the studio's lock and preview choices all stay. Only the bytes change, and the old object is deleted from storage, because nothing points at it any more.
That design is still right. What was wrong was the question it asked first.
The question was "same name?"
Here is how the old code decided which row a new upload replaced:
private MediaAsset findReplaceableByFileName(Long bookingId, String fileName) {
if (fileName == null || fileName.isBlank()) {
return null;
}
return mediaAssetRepository
.findByBookingIdAndFileNameIgnoreCaseOrderByUploadedAtDesc(bookingId, fileName)
.stream()
.findFirst()
.orElse(null);
}Same booking, same filename (ignoring case), and that was all. Now play it out with two shooters on one wedding:
- Photographer A uploads their card.
DSC_0001.JPGbecomes row 51. - Photographer B uploads their card. Their
DSC_0001.JPGmatches row 51. - Row 51 now points at B's photo. A's original object is deleted from storage as "superseded".
Nothing errors. The audit log says MEDIA_REPLACED, which reads like a correction, not a loss. The gallery shows the same number of files on the booking it showed after A's upload, just with B's photos in A's slots. A's photos are gone, and the only copy left is on A's memory card, if it hasn't been formatted for the next shoot yet.
Raw footage had the same lookup. It already had one extra rule: a match only counted within the same audience, so uploading A001.MOV as editor source footage never replaced the graded A001.MOV the client was watching. That rule existed because "cameras reuse filenames" was a known problem. It just hadn't been followed to the next case: two cameras on the same day, uploading into the same set.
The fix: same name, from the same person
A re-upload is something one person does to their own file. The editor who fixes a skin tone is the editor who uploaded it. So the lookup now needs the uploader too:
private MediaAsset findReplaceable(Long bookingId, String uploaderEmail, String fileName) {
if (fileName == null || fileName.isBlank() || uploaderEmail == null || uploaderEmail.isBlank()) {
return null;
}
return mediaAssetRepository
.findByBookingIdAndUploadedByAdminEmailIgnoreCaseAndFileNameIgnoreCaseOrderByUploadedAtDesc(
bookingId, uploaderEmail, fileName)
.stream()
.findFirst()
.orElse(null);
}Raw footage got the same rule on top of its audience check, so a match now means same booking, same audience, same uploader, same filename.
Three things are worth pointing out:
- An unknown uploader never replaces. If we can't tell who is uploading, we can't tell a correction from a new photo, so we keep both. A duplicate row costs one click to delete. A wrongly replaced photo can't be brought back.
- The email comparison ignores case. It's the login email, and
Priya@studio.inandpriya@studio.inare the same person. - The method name is long on purpose. Spring Data builds the query from the method name, so the name spells out the whole rule. It's ugly, but anyone reading the repository sees exactly what counts as "the same file".
The tests pin the rule, not the code
Each upload path got three tests, and they're written as the three sentences of the rule:
@Test
void sameNameFromAnotherAdminIsANewFile() { ... }
@Test
void sameNameFromSameAdminReplacesTheirRow() { ... }
@Test
void unknownUploaderNeverReplaces() { ... }The first one checks the thing that actually matters: a new row is saved, and storage.delete is never called. The old bug never threw an exception, so a test that only checked the response would have passed. The harm was a delete call on someone else's file, so that is what the test watches.
What it still doesn't cover
One person shooting on two bodies of the same brand, and uploading both cards from the same account, still produces two DSC_0001.JPGs from one uploader. The second will replace the first. We know, and we chose to leave it for now:
- It is much rarer than two shooters. Most of our photographers use one main body, and a second body usually belongs to a second person.
- The fix for it is different. It needs something about the file itself, such as the camera serial number from EXIF or a hash of the bytes, not who uploaded it.
- A wrong guess the other way is also costly: if we stopped replacing on re-upload, every corrected export would turn into a duplicate, and the customer's comments would stay on the old version.
That case, and the original bug, can both be stopped before a file ever reaches us.
Preventing it at capture time
The cleanest fix isn't in our code at all. Most cameras let you change the start of the filename they write, so every body can sign its own files.
| Brand | Default name | What you can change | Where (menu names vary by model) |
|---|---|---|---|
| Canon | IMG_0001.JPG | The four letters before the number | File name |
| Sony | DSC00001.JPG | The three letters before the number | File Name |
| Nikon | DSC_0001.JPG | The three letters before the number | File naming |
| Fujifilm | DSCF0001.JPG | The four letters before the number | Edit file name |
If photographer A's body writes PA1_0001.JPG and photographer B's writes PB1_0001.JPG, the two files never share a name, so there is nothing to confuse. That works on every system the photos pass through, not just ours: the card reader, Lightroom, the editor's hard drive and the client's download folder.
A simple convention for the team, set up when a body joins the kit:
- Two letters for the photographer, one digit for the body.
PA1,PA2,PB1. Three characters fits every brand above, and Canon and Fujifilm get a spare fourth. - Set it once, on the body, not per shoot. It is saved in the camera, so it survives card formats and battery swaps.
- Write it on the body's kit label. Whoever picks up the camera can see whose files it makes.
The server fix still has to exist. A freelancer joining for one day won't have set a prefix, a rental body arrives with the factory name, and phones all write IMG_0001.JPG. A camera setting helps only when someone remembers it. The upload rule protects every file anyway. The two together give two layers: the camera prevents most clashes, and the server makes sure the ones that slip through never delete anything.
The other fix in the same branch
The backend image is built FROM eclipse-temurin:17-jre, and that tag follows the newest Ubuntu release. When it moved to a new release, the build started failing: apt-get install ffmpeg got 404s for ffmpeg's dependencies, because the new release's security mirror was still filling up.
Nothing in our code had changed, and the build broke anyway. That's what an unpinned base image does. Two changes:
- The base is pinned to Ubuntu 24.04 LTS (
17-jre-noble), which we checked: ffmpeg installs and OpenCV loads. apt-getgets one retry after 20 seconds, because a mirror in the middle of a sync can 404 a package for a few minutes and then serve it fine.
Takeaways
- "Same name" is not "same file". Filenames come from cameras, and cameras don't know about each other.
- Fix it at the source too. A custom filename prefix on each camera body stops the clash before upload, for every tool the photos pass through.
- Ask who is doing it. A replace is something a person does to their own work, so the person belongs in the lookup.
- When unsure, keep both. A duplicate is cheap to remove. A deleted original isn't coming back.
- Test the side effect that hurts. The bug returned a perfectly good response. It was the
deletecall that did the damage. - Write down the case you didn't fix, and why. Two cards from one person is a known gap, not a surprise.
- Pin your base images. "Latest" changes underneath you on someone else's schedule.