Add deleting images #13

This commit is contained in:
Aaron Fischer 2017-03-29 22:48:29 +02:00
parent c0270f5f4b
commit 4936e90bc8
4 changed files with 26 additions and 2 deletions

View file

@ -11,7 +11,7 @@ WHERE id = :id
SELECT * FROM images WHERE id = :id SELECT * FROM images WHERE id = :id
-- :name delete-image! :! :n -- :name delete-image! :! :n
DELETE FROM image DELETE FROM images
WHERE id = :id WHERE id = :id
-- :name get-all-images :? :* -- :name get-all-images :? :*
@ -43,6 +43,10 @@ VALUES (:tagname)
INSERT OR IGNORE INTO image_tags (image_id, tag_id) INSERT OR IGNORE INTO image_tags (image_id, tag_id)
VALUES (:imageid, :tagid) VALUES (:imageid, :tagid)
-- :name delete-image-tags! :!
DELETE FROM image_tags
WHERE image_id = :id
-- :name get-tag :? :1 -- :name get-tag :? :1
SELECT * FROM tags SELECT * FROM tags
WHERE tagname = :tagname WHERE tagname = :tagname
@ -74,3 +78,7 @@ ORDER BY created_at DESC
SELECT * FROM comments SELECT * FROM comments
ORDER BY created_at DESC ORDER BY created_at DESC
LIMIT :num-comments LIMIT :num-comments
-- :name delete-image-comments! :!
DELETE FROM comments
WHERE image_id = :id

View file

@ -45,6 +45,7 @@
</a> </a>
</div> </div>
<hr> <hr>
{% ifequal identity ":creator" %}
<div class="btn-group pull-right clearfix" role="group"> <div class="btn-group pull-right clearfix" role="group">
<a class="btn btn-primary btn-sm" href="/edit/{{ image.id }}"> <a class="btn btn-primary btn-sm" href="/edit/{{ image.id }}">
<span class="fa fa-pencil"></span> <span class="fa fa-pencil"></span>
@ -55,6 +56,7 @@
Löschen Löschen
</a> </a>
</div> </div>
{% endifequal %}
<p>{{ image.created_at|parse-date|date:"dd-MM-yyyy HH:mm" }} Uhr</p> <p>{{ image.created_at|parse-date|date:"dd-MM-yyyy HH:mm" }} Uhr</p>
{% if image.description|length > 0 %} {% if image.description|length > 0 %}

View file

@ -28,6 +28,9 @@
(let [file (io/file image-file-path)] (let [file (io/file image-file-path)]
(str (.lastModified file) "-" (digest/md5 file)))) (str (.lastModified file) "-" (digest/md5 file))))
(defn delete-image! [image-hash]
(run! #(fs/delete (data-path "gallery" % (str image-hash ".png")))
["normal" "raw" "thumbnails"]))
(defn scale-thumbnail [width] (defn scale-thumbnail [width]
#(let [image-file % #(let [image-file %

View file

@ -25,7 +25,6 @@
(defn add-tag [image-id tag] (defn add-tag [image-id tag]
(db/create-tag! {:tagname tag}) (db/create-tag! {:tagname tag})
(let [{tag-id :id} (db/get-tag {:tagname tag})] (let [{tag-id :id} (db/get-tag {:tagname tag})]
(db/add-tag-to-image! {:imageid image-id :tagid tag-id}))) (db/add-tag-to-image! {:imageid image-id :tagid tag-id})))
@ -35,6 +34,13 @@
all-tags (str/split tags, #"(\s*,\s*|\s+)")] all-tags (str/split tags, #"(\s*,\s*|\s+)")]
(run! #(add-tag image-id %) all-tags))) (run! #(add-tag image-id %) all-tags)))
(defn delete-image! [id request]
(let [image (db/get-image {:id id})]
(db/delete-image! {:id id})
(db/delete-image-tags! {:id id})
(db/delete-image-comments! {:id id})
(images/delete-image! (:hash image))))
(defroutes admin-routes (defroutes admin-routes
(GET "/upload" [:as request] (GET "/upload" [:as request]
(-> (layout/render (-> (layout/render
@ -47,6 +53,11 @@
(-> (redirect "/upload") (-> (redirect "/upload")
(assoc :flash {:message "Upload erfolgreich." :type "success"}))) (assoc :flash {:message "Upload erfolgreich." :type "success"})))
(GET "/delete/:id" [id :as request]
(delete-image! id request)
(-> (redirect "/page/1")
(assoc :flash {:message "Bild wurde gelöscht" :type "success"})))
(GET "/statistics" [] (GET "/statistics" []
(layout/render "statistics.html")) (layout/render "statistics.html"))
(GET "/comments" [] (GET "/comments" []