Skip to content

Don't re-upload the image on a later text-only save (#542) - #1103

Open
dduugg wants to merge 1 commit into
shift-org:mainfrom
dduugg:issue-542-image-reversion
Open

dduugg wants to merge 1 commit into
shift-org:mainfrom
dduugg:issue-542-image-reversion

Conversation

@dduugg

@dduugg dduugg commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #542.

Upload an image, save, then make a text-only change and save again in the same tab: the image's version is bumped even though the image never changed. Reload the tab between the two saves and it isn't.

Cause

A file input keeps its selection after the form is submitted, and every save posts whatever it holds:

// assets/js/cal/addevent.js, the save handler
let data = new FormData();
$.each($('#image')[0].files, function(i, file) {
    data.append('file', file);
});

So the second save re-sends the identical file. The server versions the image whenever a file arrives:

// app/endpoints/manage_event.js:69
uploader.write( req.file, evt.id ).then(f => {
  const sequence = evt.nextChange();
  evt.image = `${f.name}-${sequence}${f.ext}`;
});

That server-side rule is correct as written — a posted file does mean the image changed. The wrong part is the client posting a file when nothing was picked. That also explains the issue's own note that a reload avoids it: reloading clears the input.

The change

Clear the input once a save succeeds:

$('#image').val('');

Placed in the common tail of the success handler, so it covers both the new-event and existing-event branches.

Two things that made val('') the right way to do it: it fires no event, so checkForChanges() (which listens for input) doesn't mark the form dirty and raise a spurious "Leave site?" prompt, and the #image change handler doesn't fire and swap the preview for a placeholder. The displayed image is already updated from returnVal.image just above.

Verification

Reproduced on main first, driving the real form in the browser, then confirmed fixed. Watching both the posted FormData keys and the stored row:

Before — image saved as 2-3.png, then a title-only save:

save posted keys stored image
image upload file, json 2-3.png
title only file, json 2-4.png ← bumped

The input still held ride.png going into the second save.

After:

save posted keys stored image
image upload file, json 2-5.png
title only json 2-5.png ← held
genuinely new image file, json 2-7.png ← bumps, as it should

The title did update on the text-only save, so the save itself still works; only the needless re-upload is gone. After each save the input reports 0 files, and the on-page image still renders (naturalWidth > 0, pointing at /eventimages/2-7.png).

Worth separating from the above: calevent.changes still increments on every save. That counter is the ical feed's change signal and is supposed to move whenever the event is edited. What this fixes is the image filename's cache-busting sequence, which should only move when the image does.

Backend suite unaffected: 55 passed, 0 failed. (The change is theme JS, which has no test suite.)

Every save posts whatever $('#image')[0].files holds, and a file input
keeps its selection after the form is submitted. So once an image had
been uploaded, the next save in that same session re-sent the identical
file; the server versions the image whenever a file arrives, so the
cache-busting sequence advanced for an edit that never touched it.
Reloading the tab cleared the input, which is why the issue only
reproduced within a session.

Clear the input once a save succeeds. The server side is right as it
stands: a posted file does mean the image changed.

Clearing with val() fires no event, so the form isn't marked dirty and
the change handler doesn't swap the preview for a placeholder.
@dduugg
dduugg force-pushed the issue-542-image-reversion branch from c568e90 to 50f84e8 Compare October 9, 2026 03:39
@ionous

ionous commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

we should hang on to this until after the table changes. that code tweaks the manage endpoint's handling of returnVal.image. i'd want to make sure both uploading no image is good to go, and that the local image indeed doesn't get replaced/ cleared if no image is posted. ( a manual check )

re:

Worth separating from the above: calevent.changes still increments on every save. That counter is the ical feed's change signal and is supposed to move whenever the event is edited.

this will be fixed once the table format changes. it has a separate images tables which includes a counter for image changes separate from other data changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Image is versioned when additonal non-image edits are made to an event during the same session

2 participants