-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Re #1543] * Also added a generic function in the gpml.domain.types namespace to generate the Malli schema for a given type. Which comes in handy as we always endup writing the same code for enums.
- Loading branch information
1 parent
20bb02c
commit f35385f
Showing
3 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
(ns gpml.domain.file | ||
(:require [clojure.string :as str] | ||
[gpml.domain.miscellaneous :as dom.misc] | ||
[gpml.domain.types :as dom.types] | ||
[gpml.util :as util] | ||
[malli.core :as m]) | ||
(:import [java.io File])) | ||
|
||
(def ^:const object-key-pattern | ||
"Pattern to define the object key of the file in the object storage. | ||
Example: | ||
- event/images/12fd5c76-7f12-4084-a9ca-a834d030977d" | ||
"ENTITY-KEY/FILE-KEY/FILE-ID") | ||
|
||
(def file-schema | ||
(m/schema | ||
[:map | ||
{:closed true} | ||
[:id uuid?] | ||
[:object-key | ||
[:string | ||
{:min 1}]] | ||
[:name | ||
[:string | ||
{:min 1}]] | ||
[:alt-desc | ||
{:optional true} | ||
[:maybe | ||
[:string | ||
{:min 1}]]] | ||
[:type | ||
[:string | ||
{:min 1}]] | ||
[:extension | ||
{:optional true} | ||
[:maybe | ||
[:string]]] | ||
[:visibility | ||
(dom.types/get-type-schema :file-visibility)] | ||
[:content | ||
{:optional true} | ||
[:or | ||
dom.misc/base64-schema | ||
[:fn #(instance? File %)]]] | ||
[:created-at inst?] | ||
[:last-updated-at | ||
{:optional true} | ||
inst?]])) | ||
|
||
(defn create-file-object-key | ||
[entity-key file-key file-id] | ||
(-> object-key-pattern | ||
(str/replace #"ENTITY-KEY" (name entity-key)) | ||
(str/replace #"FILE-KEY" (name file-key)) | ||
(str/replace #"FILE-ID" (str file-id)))) | ||
|
||
(defn base64->file | ||
[payload entity-key file-key visibility] | ||
(let [[_ ^String content-type ^String content] (re-find #"^data:(\S+);base64,(.*)$" payload) | ||
[_ extension] (str/split content-type #"\/") | ||
file-id (util/uuid)] | ||
{:id file-id | ||
:object-key (create-file-object-key entity-key file-key file-id) | ||
:name (format "%s-%s" (name entity-key) (util/uuid)) | ||
:alt-desc nil | ||
:type content-type | ||
:extension extension | ||
:visibility visibility | ||
:content content})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
(ns gpml.domain.miscellaneous | ||
(:require [malli.core :as m])) | ||
|
||
(def base64-schema | ||
(m/schema | ||
[:and | ||
[:string] | ||
[:or | ||
[:= ""] | ||
[:and | ||
[:re #"[0-9a-zA-Z+/]+={0,2}"] | ||
[:fn #(= 0 (rem (count %) 4))]]]])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters