Skip to content
This repository has been archived by the owner on Apr 26, 2019. It is now read-only.

[#384] Move PNGs with QR codes out of DB #466

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion env/dev/resources/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@
:user-whitelist #{}

;; used for blacklisting tokens from token registry data
:token-blacklist #{}}
:token-blacklist #{}

;; directory to store qr images, relative to resources/public or absolute path
:qr-dir ""}
5 changes: 5 additions & 0 deletions resources/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,11 @@ FROM issue_comment
WHERE issue_id = :issue_id
AND comment_hash = :hash;

-- :name get-issue-comment-hash :? :1
-- :doc retrieve image hash for given issue's github comment
SELECT comment_hash
FROM issue_comment
WHERE issue_id = :issue_id;

-- :name top-hunters :? :*
-- :doc top 5 list of users that have reveived bounty payouts with sum of
Expand Down
6 changes: 6 additions & 0 deletions src/clj/commiteth/db/comment_images.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
:hash hash
:png_data png-data})))

(defn get-hash
[issue-id]
(:comment-hash
(jdbc/with-db-connection [con-db *db*]
(db/get-issue-comment-hash con-db {:issue_id issue-id}))))

(defn get-image-data
[issue-id hash]
(jdbc/with-db-connection [con-db *db*]
Expand Down
42 changes: 33 additions & 9 deletions src/clj/commiteth/github/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
[commiteth.db.issues :as db-issues]
[commiteth.db.bounties :as db-bounties]
[commiteth.db.comment-images :as comment-images]
[clojure.string :as str])
[clojure.string :as str]
[clojure.java.io :as io])
(:import [java.util UUID]))

(def ^:dynamic url "https://api.github.com/")
Expand Down Expand Up @@ -179,13 +180,14 @@


(defn github-comment-hash
[owner repo issue-number balance]
(digest/sha-256 (str "SALT_Yoh2looghie9jishah7aiphahphoo6udiju" owner repo issue-number balance)))
[owner repo issue-number balance tokens]
(digest/sha-256
(str "SALT_Yoh2looghie9jishah7aiphahphoo6udiju" owner repo issue-number balance tokens)))

(defn- get-qr-url
[owner repo issue-number balance]
(let [hash (github-comment-hash owner repo issue-number balance)]
(str (server-address) (format "/qr/%s/%s/bounty/%s/%s/qr.png" owner repo issue-number hash))))
[owner repo issue-number balance tokens]
(let [hash (github-comment-hash owner repo issue-number balance tokens)]
(str (server-address) (format "/qr-image/%s.png" hash))))

(defn- md-url
([text url]
Expand Down Expand Up @@ -235,7 +237,7 @@

(defn generate-open-comment
[owner repo issue-number contract-address eth-balance tokens]
(let [image-url (md-image "QR Code" (get-qr-url owner repo issue-number eth-balance))
(let [image-url (md-image "QR Code" (get-qr-url owner repo issue-number eth-balance tokens))
site-url (md-url (server-address) (server-address))]
(format (str "Current balance: %s ETH\n"
(token-balances-text tokens)
Expand Down Expand Up @@ -294,8 +296,30 @@
:otp))]
(assoc req :body (json/generate-string (or raw-query proper-query)))))

(defn- create-qr-image
[issue-id hash data]
(let [file-name (str (:qr-dir env) "/" hash ".png")]
(when-not (.isDirectory (io/file file-name))
(io/make-parents file-name))
(with-open [input-stream (io/input-stream data)
output-stream (io/output-stream file-name)]
(io/copy input-stream output-stream))))

(defn- remove-qr-image
[hash]
(let [file-name (str (:qr-dir env) "/" hash ".png")]
(when (.exists (io/file file-name))
(io/delete-file file-name))))

(defn- save-image
[issue-id hash png-data]
(let [old-hash (comment-images/get-hash issue-id)]
(create-qr-image issue-id hash png-data)
(comment-images/save-image! issue-id hash png-data)
(when (and (not (nil? old-hash)) (not= hash old-hash)) (remove-qr-image old-hash))))

(defn update-bounty-comment-image [{:keys [issue-id owner repo issue-number contract-address balance-eth tokens]}]
(let [hash (github-comment-hash owner repo issue-number balance-eth)
(let [hash (github-comment-hash owner repo issue-number balance-eth tokens)
issue-url (str owner "/" repo "/issues/" (str issue-number))
png-data (png-rendering/gen-comment-image
contract-address
Expand All @@ -307,7 +331,7 @@
(log/debug "hash" hash)

(if png-data
(comment-images/save-image! issue-id hash png-data)
(save-image issue-id hash png-data)
(log/error "Failed ot generate PNG"))))

(defn post-deploying-comment
Expand Down
22 changes: 12 additions & 10 deletions src/clj/commiteth/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@
[ring.middleware.keyword-params :refer [wrap-keyword-params]]
[commiteth.env :refer [defaults]]
[mount.core :as mount]
[commiteth.middleware :as middleware]))
[commiteth.middleware :as middleware]
[commiteth.config :refer [env]]))

(mount/defstate init-app
:start ((or (:init defaults) identity))
:stop ((or (:stop defaults) identity)))

(def app-routes
(routes
#'webhook-routes
(middleware/wrap-base
(routes
(-> #'home-routes
(wrap-routes middleware/wrap-csrf)
(wrap-routes middleware/wrap-formats))
#'redirect-routes
#'service-routes
#'qr-routes
#'webhook-routes
(middleware/wrap-base
(routes
(-> #'home-routes
(wrap-routes middleware/wrap-csrf)
(wrap-routes middleware/wrap-formats))
#'redirect-routes
#'service-routes
#'qr-routes
(route/files "/qr-image" {:root (:qr-dir env)})
(route/not-found
(:body
(error-page {:status 404
Expand Down
5 changes: 3 additions & 2 deletions src/clj/commiteth/routes/qrcodes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
[clojure.tools.logging :as log])
(:import [java.io ByteArrayInputStream]))


;; TODO(endenwer) qr is handler as static image now. This route is used for old comments.
;; Remove it when it is not necessary anymore.
(defapi qr-routes
(context "/qr" []
(GET "/:owner/:repo/bounty/:issue{[0-9]{1,9}}/:hash/qr.png" [owner repo issue hash]
Expand All @@ -19,7 +20,7 @@
(do
(log/debug "address:" contract-address)
(log/debug owner repo issue balance-eth)
(log/debug hash (github/github-comment-hash owner repo issue balance-eth))
(log/debug hash)
(if contract-address
(if-let [{:keys [png-data]}
(comment-images/get-image-data
Expand Down