From 347daa432b831035974d7f61beae7fa92d56020e Mon Sep 17 00:00:00 2001 From: Stepan Lusnikov Date: Tue, 19 Jun 2018 12:24:16 +0800 Subject: [PATCH] Store qr images on filesystem --- env/dev/resources/config.edn | 5 ++- resources/sql/queries.sql | 5 +++ src/clj/commiteth/db/comment_images.clj | 6 ++++ src/clj/commiteth/github/core.clj | 42 +++++++++++++++++++------ src/clj/commiteth/handler.clj | 22 +++++++------ src/clj/commiteth/routes/qrcodes.clj | 5 +-- 6 files changed, 63 insertions(+), 22 deletions(-) diff --git a/env/dev/resources/config.edn b/env/dev/resources/config.edn index e8889f0a..c8e4ec8b 100644 --- a/env/dev/resources/config.edn +++ b/env/dev/resources/config.edn @@ -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 ""} diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql index 8379717d..58f05e05 100644 --- a/resources/sql/queries.sql +++ b/resources/sql/queries.sql @@ -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 diff --git a/src/clj/commiteth/db/comment_images.clj b/src/clj/commiteth/db/comment_images.clj index 8f09363f..b61bdb39 100644 --- a/src/clj/commiteth/db/comment_images.clj +++ b/src/clj/commiteth/db/comment_images.clj @@ -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*] diff --git a/src/clj/commiteth/github/core.clj b/src/clj/commiteth/github/core.clj index dba42153..4be2fe7a 100644 --- a/src/clj/commiteth/github/core.clj +++ b/src/clj/commiteth/github/core.clj @@ -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/") @@ -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] @@ -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) @@ -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 @@ -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 diff --git a/src/clj/commiteth/handler.clj b/src/clj/commiteth/handler.clj index ca218f11..e38afd51 100644 --- a/src/clj/commiteth/handler.clj +++ b/src/clj/commiteth/handler.clj @@ -11,7 +11,8 @@ [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)) @@ -19,15 +20,16 @@ (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 diff --git a/src/clj/commiteth/routes/qrcodes.clj b/src/clj/commiteth/routes/qrcodes.clj index 1831ed0e..82b979aa 100644 --- a/src/clj/commiteth/routes/qrcodes.clj +++ b/src/clj/commiteth/routes/qrcodes.clj @@ -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] @@ -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