From 99d84dff26572a557ab6b3fcb226a0dff6d6c07a Mon Sep 17 00:00:00 2001 From: Christian Egli Date: Mon, 10 Jun 2024 12:44:51 +0200 Subject: [PATCH] Handle upload of the same image again --- src/clj/daisyproducer2/documents/images.clj | 2 +- src/clj/daisyproducer2/routes/services.clj | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/clj/daisyproducer2/documents/images.clj b/src/clj/daisyproducer2/documents/images.clj index 644245f..cb75812 100644 --- a/src/clj/daisyproducer2/documents/images.clj +++ b/src/clj/daisyproducer2/documents/images.clj @@ -29,7 +29,7 @@ ;; make sure path exists (fs/create-dirs (fs/parent absolute-path)) ;; copy the contents into the archive - (fs/copy tempfile absolute-path) + (fs/copy tempfile absolute-path {:replace-existing true}) ;; and store it in the db ... (-> (db/insert-image {:document-id document-id :content (str path)}) diff --git a/src/clj/daisyproducer2/routes/services.clj b/src/clj/daisyproducer2/routes/services.clj index 10a5181..1d47e37 100644 --- a/src/clj/daisyproducer2/routes/services.clj +++ b/src/clj/daisyproducer2/routes/services.clj @@ -489,9 +489,12 @@ :handler (fn [{{{:keys [id]} :path {{:keys [filename tempfile]} :file} :multipart} :parameters {{uid :uid} :user} :identity}] (try - (let [new-key (images/insert-image id filename tempfile) - new-url (format "/documents/%s/images/%s" id new-key)] - (created new-url {})) + (if-let [new-key (images/insert-image id filename tempfile)] + ;; a new image resource was created + (let [new-url (format "/documents/%s/images/%s" id new-key)] + (created new-url {})) + ;; an existing image was uploaded again + (no-content)) (finally (fs/delete tempfile))))} :delete {:summary "Delete all images of a given document"