aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Barksdale <amatus@amatus.name>2014-11-30 17:02:11 -0600
committerDavid Barksdale <amatus@amatus.name>2014-11-30 17:02:11 -0600
commit45a57724b2799da9c8173031f0e19db083becfc9 (patch)
treefcc2d90ed9cfc69ee18ca4cd30998ab839d845ad
parent6b198c7c2d498aeb6251918662dc39c290d2097b (diff)
Publish metadata and better keyword generation
-rw-r--r--gnunet-build/packages/gnunet/gnunet/files/client-lib.exports6
-rw-r--r--src/cljs/gnunet_web/extractor.cljs51
-rw-r--r--src/cljs/gnunet_web/filesharing.cljs25
-rw-r--r--src/hl/index.cljs.hl36
4 files changed, 96 insertions, 22 deletions
diff --git a/gnunet-build/packages/gnunet/gnunet/files/client-lib.exports b/gnunet-build/packages/gnunet/gnunet/files/client-lib.exports
index c5643cd..1e76813 100644
--- a/gnunet-build/packages/gnunet/gnunet/files/client-lib.exports
+++ b/gnunet-build/packages/gnunet/gnunet/files/client-lib.exports
@@ -1,7 +1,9 @@
[
"_EXTRACTOR_metatype_to_description",
"_EXTRACTOR_metatype_to_string",
-"_GNUNET_CONTAINER_meta_data_get_by_type",
+"_GNUNET_CONTAINER_meta_data_create",
+"_GNUNET_CONTAINER_meta_data_destroy",
+"_GNUNET_CONTAINER_meta_data_insert",
"_GNUNET_CONTAINER_meta_data_iterate",
"_GNUNET_FS_BlockOptions_new",
"_GNUNET_FS_ProgressInfo_get_download_cctx",
@@ -29,6 +31,8 @@
"_GNUNET_FS_uri_chk_get_file_size2",
"_GNUNET_FS_uri_destroy",
"_GNUNET_FS_uri_ksk_create",
+"_GNUNET_FS_uri_ksk_create_from_meta_data",
+"_GNUNET_FS_uri_ksk_to_string_fancy",
"_GNUNET_FS_uri_parse",
"_GNUNET_FS_uri_to_string",
"_GNUNET_xstrdup_",
diff --git a/src/cljs/gnunet_web/extractor.cljs b/src/cljs/gnunet_web/extractor.cljs
index 5ecccc2..1a2f000 100644
--- a/src/cljs/gnunet_web/extractor.cljs
+++ b/src/cljs/gnunet_web/extractor.cljs
@@ -21,6 +21,11 @@
(def format-data 2)
(def format-string 3) ; unknown character encoding
+(def metatype-mimetype 1)
+(def metatype-filename 2)
+(def metatype-title 4)
+(def metatype-original-filename 180)
+
(defn metatype-to-string
[type]
(js/Pointer_stringify
@@ -30,3 +35,49 @@
[type]
(js/Pointer_stringify
(js/_EXTRACTOR_metatype_to_description type)))
+
+(defn metadata-destroy
+ [metadata]
+ (js/_GNUNET_CONTAINER_meta_data_destroy metadata))
+
+(defn metadata-extract
+ [file]
+ (let [metadata (js/_GNUNET_CONTAINER_meta_data_create)]
+ (js/ccallFunc
+ js/_GNUNET_CONTAINER_meta_data_insert
+ "number"
+ (array "number" "string" "number" "number" "string" "string" "number")
+ (array
+ metadata
+ "<gnunet-web>"
+ metatype-mimetype
+ format-utf8
+ "text/plain"
+ (.-type file)
+ (inc (count (.-type file)))))
+ (js/ccallFunc
+ js/_GNUNET_CONTAINER_meta_data_insert
+ "number"
+ (array "number" "string" "number" "number" "string" "string" "number")
+ (array
+ metadata
+ "<gnunet-web>"
+ metatype-original-filename
+ format-utf8
+ "text/plain"
+ (.-name file)
+ (inc (count (.-name file)))))
+ metadata))
+
+(defn guess-filename
+ [metadata]
+ (let [preference [metatype-original-filename
+ metatype-filename
+ metatype-title
+ ;; ...
+ ]]
+ (first
+ (for [type preference
+ x metadata
+ :when (= type (:type x))]
+ (:data x)))))
diff --git a/src/cljs/gnunet_web/filesharing.cljs b/src/cljs/gnunet_web/filesharing.cljs
index a7a2498..c787fea 100644
--- a/src/cljs/gnunet_web/filesharing.cljs
+++ b/src/cljs/gnunet_web/filesharing.cljs
@@ -44,6 +44,15 @@
(array "string" "number")
(array uri 0)))
+(defn keywords-from-metadata
+ [metadata]
+ (let [uri-pointer (js/_GNUNET_FS_uri_ksk_create_from_meta_data metadata)
+ keywords-pointer (js/_GNUNET_FS_uri_ksk_to_string_fancy uri-pointer)
+ keywords (js/Pointer_stringify keywords-pointer)]
+ (js/_free keywords-pointer)
+ (js/_GNUNET_FS_uri_destroy uri-pointer)
+ keywords))
+
(defn metadata-iterator
[metadata cls plugin-name type format mime-type data data-size]
(swap! metadata conj
@@ -189,18 +198,6 @@
(unregister-object callback-key)
(close! ch))
-(defn guess-filename
- [metadata]
- (let [preference ["original filename"
- "title"
- ;; ...
- ]]
- (first
- (for [type preference
- x metadata
- :when (= type (e/metatype-to-string (:type x)))]
- (:data x)))))
-
(defn start-download
[uri anonymity]
(let [uri-pointer (string-to-uri-pointer uri)
@@ -234,7 +231,7 @@
(js/_GNUNET_FS_BlockOptions_new expiration anonymity priority replication))
(defn start-publish
- [file keywords block-options]
+ [file keywords metadata block-options]
(let [file-key (register-object file)
length (real-to-i64 (.-byteLength file))
ch (chan 1)
@@ -249,7 +246,7 @@
publish-reader-callback-pointer ; GNUNET_FS_DataReader reader
file-key ; void *reader_cls
uri-pointer ; struct GNUNET_FS_Uri *keywords
- 0 ; struct GNUNET_CONTAINER_MetaData *meta
+ metadata ; struct GNUNET_CONTAINER_MetaData *meta
0 ; int do_index
bo-pointer); struct GNUNET_FS_BlockOptions *bo
publish (js/_GNUNET_FS_publish_start
diff --git a/src/hl/index.cljs.hl b/src/hl/index.cljs.hl
index 93578e2..8f25b3a 100644
--- a/src/hl/index.cljs.hl
+++ b/src/hl/index.cljs.hl
@@ -17,6 +17,7 @@
(page "index.html"
(:require [amatus.code :as code]
[gnunet-web.core :as core]
+ [gnunet-web.extractor :as extractor]
[gnunet-web.filesharing :as filesharing]
[gnunet-web.hostlist :as hostlist]
[gnunet-web.metadata :as metadata]
@@ -95,7 +96,7 @@
(recur))))))
(defn start-publish
- [keywords file state-cell progress-cell uri-cell]
+ [keywords metadata file state-cell progress-cell uri-cell]
(let [reader (js/FileReader.)]
(set!
(.-onload reader)
@@ -104,6 +105,7 @@
(let [publish (filesharing/start-publish
(.-result (.-target e))
keywords
+ metadata
{:expiration (+ (* 2 356.24 24 60 60 1000 1000) (now))
:anonymity 0
:priority 365
@@ -140,17 +142,37 @@
(defelem ui-publish
[attr kids]
- (let [keywords (input :type "text" :placeholder "Keywords...")
- file (input :type "file")
+ (let [file-chosen (cell false)
publish-state (cell :inactive)
publish-progress (cell 0)
- publish-uri (cell nil)]
+ publish-uri (cell nil)
+ metadata (atom nil)
+ keywords (input :type "text")
+ file (input :type "file"
+ :change
+ (fn [e]
+ (swap! metadata
+ (fn [metadata]
+ (extractor/metadata-destroy metadata)
+ (extractor/metadata-extract
+ (aget (.-files (.-target e)) 0))))
+ (set! (.-value keywords)
+ (filesharing/keywords-from-metadata
+ @metadata))
+ (reset! file-chosen true)))]
(div :class "ui form segment"
- file
- keywords
+ (div :class "ui labeled input"
+ (div :class "ui label" "Choose a file to publish")
+ file)
+ (div :class "ui labeled input"
+ :toggle file-chosen
+ (div :class "ui label" "Keywords")
+ keywords)
(div :class "ui icon button"
:title "Publish"
+ :toggle file-chosen
:click #(start-publish (.-value keywords)
+ @metadata
(aget (.-files file) 0)
publish-state
publish-progress
@@ -186,7 +208,7 @@
(tbody
(loop-tpl :bindings [result search-results]
(let [uri (cell= (:uri result))
- filename (cell= (filesharing/guess-filename
+ filename (cell= (extractor/guess-filename
(:metadata result)))
download-state (cell :inactive)
download-progress (cell 0)