[feature] add ipfs module to add and cat content on ipfs
Signed-off-by: yenda <eric@status.im>
This commit is contained in:
parent
1de40487e8
commit
9aea669da3
|
@ -266,9 +266,5 @@
|
||||||
(def ^:const scan-qr-code-callback "scan-qr-code-callback")
|
(def ^:const scan-qr-code-callback "scan-qr-code-callback")
|
||||||
|
|
||||||
;;ipfs
|
;;ipfs
|
||||||
(def ^:const ipfs-add-url "https://ipfs.infura.io:5001/api/v0/add")
|
|
||||||
(def ^:const ipfs-add-param-name "extension.event.edn")
|
|
||||||
(def ^:const ipfs-cat-url "https://ipfs.infura.io/ipfs/")
|
|
||||||
|
|
||||||
(def ^:const ipfs-proto-code "e3")
|
(def ^:const ipfs-proto-code "e3")
|
||||||
(def ^:const swarm-proto-code "e4")
|
(def ^:const swarm-proto-code "e4")
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
[status-im.ui.components.react :as react]
|
[status-im.ui.components.react :as react]
|
||||||
[status-im.ui.screens.wallet.settings.views :as settings]
|
[status-im.ui.screens.wallet.settings.views :as settings]
|
||||||
[status-im.i18n :as i18n]
|
[status-im.i18n :as i18n]
|
||||||
|
[status-im.ipfs.core :as ipfs]
|
||||||
[status-im.utils.money :as money]
|
[status-im.utils.money :as money]
|
||||||
[status-im.constants :as constants]
|
[status-im.constants :as constants]
|
||||||
[status-im.ui.components.colors :as colors]
|
[status-im.ui.components.colors :as colors]
|
||||||
|
@ -224,40 +225,13 @@
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
:ipfs/cat
|
:ipfs/cat
|
||||||
(fn [_ [_ _ {:keys [hash on-success on-failure]}]]
|
(fn [cofx [_ _ args]]
|
||||||
{:http-raw-get (merge {:url (str constants/ipfs-cat-url hash)
|
(ipfs/cat cofx args)))
|
||||||
:success-event-creator
|
|
||||||
(fn [{:keys [status body]}]
|
|
||||||
(if (= 200 status)
|
|
||||||
(on-success {:value body})
|
|
||||||
(when on-failure
|
|
||||||
(on-failure {:value status}))))}
|
|
||||||
(when on-failure
|
|
||||||
{:failure-event-creator on-failure})
|
|
||||||
{:timeout-ms 5000})}))
|
|
||||||
|
|
||||||
(defn- parse-ipfs-add-response [res]
|
|
||||||
(let [{:keys [Name Hash Size]} (parse-json res)]
|
|
||||||
{:name Name
|
|
||||||
:hash Hash
|
|
||||||
:size Size}))
|
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
:ipfs/add
|
:ipfs/add
|
||||||
(fn [_ [_ _ {:keys [value on-success on-failure]}]]
|
(fn [cofx [_ _ args]]
|
||||||
(let [formdata (doto (js/FormData.)
|
(ipfs/add cofx args)))
|
||||||
(.append constants/ipfs-add-param-name value))]
|
|
||||||
{:http-raw-post (merge {:url constants/ipfs-add-url
|
|
||||||
:body formdata
|
|
||||||
:success-event-creator
|
|
||||||
(fn [{:keys [status body]}]
|
|
||||||
(if (= 200 status)
|
|
||||||
(on-success {:value (parse-ipfs-add-response body)})
|
|
||||||
(when on-failure
|
|
||||||
(on-failure {:value status}))))}
|
|
||||||
(when on-failure
|
|
||||||
{:failure-event-creator on-failure})
|
|
||||||
{:timeout-ms 5000})})))
|
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
:http/post
|
:http/post
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
(ns status-im.ipfs.core
|
||||||
|
(:refer-clojure :exclude [cat])
|
||||||
|
(:require [status-im.utils.fx :as fx]))
|
||||||
|
|
||||||
|
;; we currently use an ipfs gateway but this detail is not relevant
|
||||||
|
;; outside of this namespace
|
||||||
|
(def ^:const ipfs-add-url "https://ipfs.infura.io:5001/api/v0/add")
|
||||||
|
(def ^:const ipfs-cat-url "https://ipfs.infura.io/ipfs/")
|
||||||
|
|
||||||
|
(fx/defn cat
|
||||||
|
[cofx {:keys [hash on-success on-failure]}]
|
||||||
|
{:http-raw-get (cond-> {:url (str ipfs-cat-url hash)
|
||||||
|
:timeout-ms 5000
|
||||||
|
:success-event-creator
|
||||||
|
(fn [{:keys [status body]}]
|
||||||
|
(if (= 200 status)
|
||||||
|
(on-success {:value body})
|
||||||
|
(when on-failure
|
||||||
|
(on-failure {:value status}))))}
|
||||||
|
on-failure
|
||||||
|
(assoc :failure-event-creator on-failure))})
|
||||||
|
|
||||||
|
(defn- parse-ipfs-add-response
|
||||||
|
[response]
|
||||||
|
(when response
|
||||||
|
(let [{:keys [Name Hash Size]} (js->clj (js/JSON.parse response)
|
||||||
|
:keywordize-keys true)]
|
||||||
|
{:name Name
|
||||||
|
:hash Hash
|
||||||
|
:size Size})))
|
||||||
|
|
||||||
|
(fx/defn add
|
||||||
|
[cofx {:keys [value on-success on-failure]}]
|
||||||
|
(let [formdata (doto (js/FormData.)
|
||||||
|
;; the key is ignored so there is no need to provide one
|
||||||
|
(.append nil value))]
|
||||||
|
{:http-raw-post (cond-> {:url ipfs-add-url
|
||||||
|
:body formdata
|
||||||
|
:timeout-ms 5000
|
||||||
|
:success-event-creator
|
||||||
|
(fn [{:keys [status body]}]
|
||||||
|
(if (= 200 status)
|
||||||
|
(on-success {:value (parse-ipfs-add-response body)})
|
||||||
|
(when on-failure
|
||||||
|
(on-failure {:value status}))))}
|
||||||
|
on-failure
|
||||||
|
(assoc :failure-event-creator on-failure))}))
|
Loading…
Reference in New Issue