From cf1188ad473aa19c0b805058f2f627633e86d97a Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sun, 20 Nov 2016 23:25:48 +0100 Subject: [PATCH] Add partial impl for icons collections importer (wip). --- backend/resources/sql/cli.sql | 26 +++++++++++- backend/src/uxbox/cli/collimp.clj | 66 +++++++++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/backend/resources/sql/cli.sql b/backend/resources/sql/cli.sql index 360428296..65e3e30c5 100644 --- a/backend/resources/sql/cli.sql +++ b/backend/resources/sql/cli.sql @@ -15,14 +15,38 @@ delete from images where id = :id and "user" = '00000000-0000-0000-0000-000000000000'::uuid; --- :name create-image-collection +-- :name create-images-collection insert into images_collections (id, "user", name) values (:id, '00000000-0000-0000-0000-000000000000'::uuid, :name) on conflict (id) do update set name = :name returning *; +-- :name create-icons-collection +insert into icons_collections (id, "user", name) +values (:id, '00000000-0000-0000-0000-000000000000'::uuid, :name) + on conflict (id) + do update set name = :name +returning *; + -- :name get-image select * from images as i where i.id = :id and i."user" = '00000000-0000-0000-0000-000000000000'::uuid; + +-- :name get-icon +select * from icons as i + where i.id = :id + and i."user" = '00000000-0000-0000-0000-000000000000'::uuid; + +-- :name create-icon :> (sc/fetch-one conn sqlv) + (data/normalize-attrs)))) + +;; (defn- create-icon +;; [conn collid iconid localpath] +;; {:pre [(fs/path? localpath) +;; (uuid? collid) +;; (uuid? imageid)]} +;; (let [filename (fs/base-name localpath) +;; storage media/images-storage +;; [width height] (time (retrieve-image-size localpath)) +;; extension (second (fs/split-ext filename)) +;; path @(st/save storage filename localpath) +;; params {:name filename +;; :path (str path) +;; :mimetype (case extension +;; ".jpg" "image/jpeg" +;; ".png" "image/png") +;; :width width +;; :height height +;; :collection collid +;; :id imageid} +;; sqlv (sql/create-image params)] +;; (sc/execute conn sqlv))) + +(defn- import-icon + [conn id fpath] + {:pre [(uuid? id) (fs/path? fpath)]} + (println "Importing icon:" (str fpath)) + (let [filename (fs/base-name fpath) + iconid (uuid/namespaced +imates-uuid-ns+ (str id filename))] + #_(create-icon conn id iconid fpath))) + +(defn- process-icons-entry + [conn basedir {:keys [path regex] :as entry}] + {:pre [(us/valid? ::import-entry entry)]} + (let [id (create-icons-collection conn entry) + path (fs/resolve basedir path)] + (doseq [fpath (fs/list-files path)] + (when (re-matches regex (str fpath)) + (import-icon conn id fpath))))) + +;; --- Images Collections Importer + +(defn- create-images-collection "Create or replace image collection by its name." [conn {:keys [name] :as entry}] (println "Creating image collection:" name) (let [id (uuid/namespaced +imates-uuid-ns+ name) - sqlv (sql/create-image-collection {:id id :name name})] + sqlv (sql/create-images-collection {:id id :name name})] (sc/execute conn sqlv) id)) @@ -110,7 +168,7 @@ (defn- process-images-entry [conn basedir {:keys [path regex] :as entry}] {:pre [(us/valid? ::import-entry entry)]} - (let [id (create-image-collection conn entry) + (let [id (create-images-collection conn entry) path (fs/resolve basedir path)] (doseq [fpath (fs/list-files path)] (when (re-matches regex (str fpath))