diff options
author | Tim <contact@bytim.eu> | 2025-06-07 13:09:24 +0200 |
---|---|---|
committer | Tim <contact@bytim.eu> | 2025-06-07 13:09:24 +0200 |
commit | 294f71fb3c5ca3f0fee62e70c9c6a3427a9afc12 (patch) | |
tree | a9e0ca6460ce9361b967b42c59572bc053633775 /src/chef/pages/admin/api.clj | |
parent | 82da261f168454cd606f4f150313844b9d4b9eea (diff) | |
download | chef-294f71fb3c5ca3f0fee62e70c9c6a3427a9afc12.tar.xz chef-294f71fb3c5ca3f0fee62e70c9c6a3427a9afc12.zip |
Add features to upload and delete thumbnails
Diffstat (limited to 'src/chef/pages/admin/api.clj')
-rw-r--r-- | src/chef/pages/admin/api.clj | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/src/chef/pages/admin/api.clj b/src/chef/pages/admin/api.clj index 9696eb5..1119607 100644 --- a/src/chef/pages/admin/api.clj +++ b/src/chef/pages/admin/api.clj @@ -1,9 +1,12 @@ (ns chef.pages.admin.api (:require [chef.utils :as cutils] [chef.database :as cdb] + [clojure.string :as cstr] [next.jdbc :as jdbc] [honey.sql :as sql] - [ring.util.response :as ruresp])) + [ring.util.response :as ruresp] + [clojure.java.io :as cjio]) + (:import java.io.File)) (defn create-category [req] (cutils/auth-only req @@ -81,7 +84,22 @@ ingredients (get-in req [:params "ingredients"])] (if (and (some? id) (cutils/valid-ingredients? ingredients)) - (do (jdbc/execute! @cdb/db + (do (when-let [thumbnail (get-in req [:params "thumbnail"])] + (when-let [existing-thumbnail-file (->> {:select [:*] + :from [:recipes] + :where [:= :id id]} + sql/format + (jdbc/execute! @cdb/db) + first + cutils/get-thumbnail-file)] + (.delete ^File existing-thumbnail-file)) + (cjio/copy (:tempfile thumbnail) + (File. (str "./thumbnails/" id "." + (-> thumbnail + :filename + (cstr/split #"\.") + last))))) + (jdbc/execute! @cdb/db (sql/format {:update :recipes :set {:title (get-in req [:params "title"]) :category (get-in req [:params "category"]) @@ -91,3 +109,18 @@ :where [:= :id id]})) (ruresp/response "Saved.")) (ruresp/bad-request "Bad request."))))) + +(defn delete-thumbnail [req] + (cutils/auth-only req + (if-let [id (try (Integer/parseInt (get-in req [:path-params :id])) + (catch Exception _ nil))] + (when-let [thumbnail-file (->> {:select [:*] + :from [:recipes] + :where [:= :id id]} + sql/format + (jdbc/execute! @cdb/db) + first + cutils/get-thumbnail-file)] + (.delete ^File thumbnail-file) + (ruresp/response "Done.")) + (ruresp/bad-request "Bad request.")))) |