Skip to content

Commit

Permalink
profile - use CAS for profile images
Browse files Browse the repository at this point in the history
  • Loading branch information
plexus committed Aug 28, 2024
1 parent 4863986 commit 8336cbd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 60 deletions.
29 changes: 14 additions & 15 deletions src/co/gaiwan/compass/db/data.clj
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
(ns co.gaiwan.compass.db.data
"Static data that gets imported at boot"
(:require [clojure.java.io :as io]
[clojure.pprint :as pprint]))
(:require
[clojure.java.io :as io]
[clojure.string :as str]
[co.gaiwan.compass.model.assets :as assets]))

(require 'java-time-literals.core)

(defn locations []
[{:location/name "Het Depot"
:db/ident :location.type/depot}
{:location/name "Het Depot - main stage"
:db/ident :location.type/depot-main-stage}
{:location/name "Het Depot - Bar"
:db/ident :location.type/depot-bar}
{:location/name "Hal 5"
:db/ident :location.type/hal5}
{:location/name "Hal 5 - zone A"
{:location/name "Hal 5 - Workshop Zone"
:db/ident :location.type/hal5-zone-a}
{:location/name "Hal 5 - zone B"
{:location/name "Hal 5 - Presentation Zone"
:db/ident :location.type/hal5-zone-b}
{:location/name "Hal 5 - HoC Café"
{:location/name "Hal 5 - Open Zone"
:db/ident :location.type/hal5-hoc-cafe}
{:location/name "Hal 5 - Foodcourt"
:db/ident :location.type/hal5-foodcourt}
{:location/name "Hal 5 - park"
{:location/name "Hal 5 - Park"
:db/ident :location.type/hal5-park}
{:location/name "Hal 5 - outside seating"
{:location/name "Hal 5 - Outside seating"
:db/ident :location.type/hal5-outside-seating}
{:location/name "Hal 5 - long table"
{:location/name "Hal 5 - Long table"
:db/ident :location.type/hal5-long-table}])

(defn session-types []
Expand All @@ -50,5 +46,8 @@
:db/ident :session.type/activity}])

(defn schedule []
(read-string (slurp (io/resource "compass/schedule.edn"))))
(map
(fn [s]
(update s :session/image #(assets/download-image %)))
(read-string (slurp (io/resource "compass/schedule.edn")))))

72 changes: 27 additions & 45 deletions src/co/gaiwan/compass/routes/profiles.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"We need a page/route for user's profile"
(:require
[clojure.java.io :as io]
[clojure.string :as str]
[co.gaiwan.compass.config :as config]
[co.gaiwan.compass.db :as db]
[co.gaiwan.compass.db.queries :as q]
[co.gaiwan.compass.model.attendees :as attendees]
[co.gaiwan.compass.html.profiles :as h]
[co.gaiwan.compass.http.response :as response]
[co.gaiwan.compass.model.assets :as assets]
[co.gaiwan.compass.model.attendees :as attendees]
[ring.util.response :as ring-response]))

(defn GET-profile [req]
Expand Down Expand Up @@ -83,33 +85,28 @@
private-name-switch
bio_public name_public
bio_private name_private
rows-count] :as params}]
rows-count
image] :as params}]
(tap> params)
(let [user-id (parse-long user-id)
out {:db/id user-id
:public-profile/bio bio_public
:private-profile/bio bio_private
:public-profile/name name_public}
;; handle the links data
links (vec (mapcat #(index->link-data params %)
(range (parse-long rows-count))))
txes (conj links out)
;; handle the hidden?
txes (cond
(= "on" hidden?)
(conj txes [:db/add user-id :public-profile/hidden? true])
(nil? hidden?)
(conj txes [:db/retract user-id :public-profile/hidden? true]))
;; handle the user private name
txes (cond
(and name_private
(= "on" private-name-switch))
(conj txes [:db/add user-id :private-profile/name name_private])
(nil? private-name-switch)
(conj txes [:db/retract user-id :private-profile/name])
:else
txes)]
txes))
(let [user-id (parse-long user-id)]
(cond-> (into [{:db/id user-id
:public-profile/bio bio_public
:public-profile/name name_public
:public-profile/hidden? (= "on" hidden?)
:private-profile/bio bio_private}]
(mapcat #(index->link-data params %))
(range (parse-long rows-count)))
image
(conj [:db/add user-id :public-profile/avatar-url
(assets/add-to-content-addressed-storage (:content-type image) (:tempfile image))])

(and (= "on" private-name-switch)
(not (str/blank? name_private)))
(conj [:db/add user-id :private-profile/name name_private])

(or (not= "on" private-name-switch)
(str/blank? name_private))
(conj [:db/retract user-id :private-profile/name]))))

(defn POST-save-profile
"Save profile to DB
Expand All @@ -119,24 +116,9 @@
:tityle \"CEO of Gaiwan\"
:image {:content-type :filename :size :tempfile}}"
[{:keys [params identity] :as req}]
(let [{:keys [filename tempfile] :as image} (:image params)
file-id (str (:db/id identity))
filepath (str (config/value :uploads/dir) "/" file-id "_" filename)
;; creating the transaction
txes (params->profile-data params)
txes (if image
(conj txes
{:db/id (parse-long (:user-id params))
:public-profile/avatar-url (str "/" filepath)})
txes)
_ (tap> {:txes txes})
{:keys [tempids]} @(db/transact txes)]
;; (tap> req)
;; Copy the image file content to the uploads directory
(when image
(io/copy tempfile (io/file filepath)))
(response/redirect "/profile"
{:flash "Successfully Saved!"})))
@(db/transact (params->profile-data params))
(response/redirect "/profile"
{:flash "Successfully Saved!"}))

(defn file-handler [req]
(let [file (io/file (config/value :uploads/dir) (get-in req [:path-params :filename]))]
Expand Down

0 comments on commit 8336cbd

Please sign in to comment.