write files when uploading

master
Josha von Gizycki 3 years ago
parent 2c33c6cdcc
commit 5f3ed0e363

@ -0,0 +1,50 @@
(ns wanijo.instance.files
(:require [clojure.java.io :as io]
[wanijo.infra.sysconfig :refer [env]]
[wanijo.infra.neo4j :refer [uuid]])
(:import [java.io File]))
(def files-home (str (env "WANIJO_HOME") "/files"))
(comment
(def files-home "/tmp/wanijo"))
(defn persist-files! [instance]
(doall
(for [file (->> (:properties instance)
(filter #(= "file" (-> % :attribute :type)))
(map (fn [prop] {:file (:value prop)
:uuid (:uuid prop)})))
:let [uploaded-file (:file file)
size (:size uploaded-file)
uuid (if (empty? (:uuid file)) (uuid) (:uuid file))
path (str files-home "/" uuid)]
:when (> size 0)]
(do
(.mkdirs (io/file files-home))
(io/copy (io/file (:tempfile uploaded-file)) (io/file path))
{:path path
:uuid uuid
:filename (:filename uploaded-file)
:content-type (:content-type uploaded-file)}))))
(comment
(let [instance {:name "Frank",
:properties
'({:attribute
{:name "Dateidings",
:created_at "20210929T154103.152Z",
:type "file",
:uuid "00847b4c-9961-47cd-a64f-18eaea0e362f",
:required 0},
:value
{:filename "2020.pdf",
:content-type "application/pdf",
:tempfile nil,
:size 127785},
:uuid "f7d56f9c-e74a-4b12-95c1-f33961672072"}),
:uuid "def4dacb-979f-4a0d-b1d6-535ac2a3f94b"}]
(->> (:properties instance)
(filter #(= "file" 1 #_(-> % :attribute :type))) #_(map (fn [prop] {:file (:value prop)
:uuid (:uuid prop)}))))
)

@ -14,7 +14,8 @@
[wanijo.instance
[db :as db]
[forms :as forms-inst]
[domain :as domain]]
[domain :as domain]
[files :as files]]
[wanijo.schema.db :as domain-schema]
[wanijo.schema.middleware :as middleware-schema]
[wanijo.link.db :as domain-link]
@ -77,9 +78,10 @@
form-def (forms-inst/with-attributes attrs)]
(if (form/valid? form-def req)
(let [form-data (form/form-data form-def req)
_ (clojure.pprint/pprint req)
form-instance (forms-inst/form-data->instance form-data attrs)
instance (assoc form-instance :uuid uuid)]
instance (assoc form-instance :uuid uuid)
_ (clojure.pprint/pprint instance)]
(clojure.pprint/pprint (files/persist-files! instance))
(db/edit! instance (domain/to-revision instance))
(resp/redirect (path :instance-show instance)))
(route-show! uuid req))))

Loading…
Cancel
Save