From 5f3ed0e363cfc7361349bc66a497dc705d427784 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Thu, 30 Sep 2021 00:54:06 +0200 Subject: [PATCH] write files when uploading --- src/wanijo/instance/files.clj | 50 ++++++++++++++++++++++++++++++++++ src/wanijo/instance/routes.clj | 8 ++++-- 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/wanijo/instance/files.clj diff --git a/src/wanijo/instance/files.clj b/src/wanijo/instance/files.clj new file mode 100644 index 0000000..8a6b180 --- /dev/null +++ b/src/wanijo/instance/files.clj @@ -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)})))) + ) diff --git a/src/wanijo/instance/routes.clj b/src/wanijo/instance/routes.clj index 77d7ff5..3827135 100644 --- a/src/wanijo/instance/routes.clj +++ b/src/wanijo/instance/routes.clj @@ -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))))