Skip to content

Commit

Permalink
Add path refreshing, last modified date detection for tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
verma committed May 12, 2014
1 parent 8bae172 commit a4d7974
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
10 changes: 5 additions & 5 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
[org.clojure/core.async "0.1.267.0-0d7780-alpha"]
[org.clojure/core.match "0.2.1"]
[org.clojure/tools.cli "0.3.1"]
[compojure "1.1.6"]
[clj-ssh "0.5.7"]
[compojure "1.1.8"]
[clj-ssh "0.5.9"]
[ring/ring-devel "1.2.2"]
[ring/ring-core "1.2.2"]
[http-kit "2.1.18"]
[org.clojure/clojurescript "0.0-2202"]
[org.clojure/data.json "0.2.4"]
[de.ubercode.clostache/clostache "1.3.1"]
[de.ubercode.clostache/clostache "1.4.0"]
[org.clojure/tools.logging "0.2.6"]
[javax.jmdns/jmdns "3.4.1"]
[me.raynes/conch "0.5.1"]
[om "0.5.3"]
[jayq "2.5.0"]]
[om "0.6.2"]
[jayq "2.5.1"]]
:plugins [[lein-cljsbuild "1.0.3"]
[lein-ancient "0.5.5"]]

Expand Down
3 changes: 2 additions & 1 deletion src-cljs/dakait/components.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns dakait.components
(:use [dakait.util :only [format-file-size format-date]]
(:use [dakait.util :only [format-file-size format-date duration-since]]
[clojure.string :only [join split]]
[jayq.core :only [$ html append css text ajax on bind hide show attr add-class remove-class]]
[dakait.net :only [http-post]]
Expand Down Expand Up @@ -70,6 +70,7 @@
(dom/div #js {:className "col-sm-2 tag-button-container"}
(dom/button #js {:className "btn btn-default btn-lg tag-item-action"
:type "button"
:disabled (:recent l)
:onClick (tag-handler (:name l)) } "Tag")))
(when-let [pc (:percent-complete (:download l))]
(dom/div #js {:className "thin-progress"}
Expand Down
34 changes: 32 additions & 2 deletions src-cljs/dakait/start.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns dakait.index
(:use [dakait.components :only [current-path listing sort-order
download-activity-monitor tags-modal content-pusher-modal]]
[dakait.util :only [duration-since]]
[dakait.net :only [get-json http-delete http-post]]
[dakait.downloads :only [start-listening-for-downloads]]
[clojure.string :only [join split]]
Expand Down Expand Up @@ -126,6 +127,31 @@
attach-tag
attach-dl)))))

(def path-refresh-timeout (atom nil))
(def path-refresh-was-canceled (atom false))

(defn path-refresh
"Continously query the given path at regular intervals, should be cancellable"
[path f]
(let [to (js/setTimeout (fn []
(log "Refreshing path...")
(get-file-listing path
(fn [list]
(when-not @path-refresh-was-canceled
(f list)
(path-refresh path f)))))
2000)]
(reset! path-refresh-was-canceled false)
(reset! path-refresh-timeout to)))

(defn cancel-path-refresh
"Cancel an active path refresh loop"
[]
(when @path-refresh-timeout
(js/clearTimeout @path-refresh-timeout)
(reset! path-refresh-timeout nil)
(reset! path-refresh-was-canceled true)
(log "Path refresh was cancelled!")))

(defn full-page
"Full page om component"
Expand Down Expand Up @@ -158,11 +184,15 @@
(go (loop []
(let [path (<! path-chan)]
(om/set-state! owner :is-loading path)
(cancel-path-refresh)
(get-file-listing path
(fn [list]
(om/set-state! owner :is-loading false)
(om/update! app :current-path path)
(om/update! app :listing list)))
(om/update! app :listing list)
(path-refresh path
(fn [l]
(om/update! app :listing l)))))
(recur))))
;; Start loop for listening to sort requests
(go (loop []
Expand Down Expand Up @@ -258,7 +288,7 @@
;;
(om/build listing {:listing (-> (:listing app)
(merge-listing (:tags app) (:downloads app) (:current-path app))
(sort-list))
sort-list)
:current-path (:current-path app)}
{:opts {:path-chan (:path-chan state)
:modal-chan (:modal-chan state)}})
Expand Down
6 changes: 6 additions & 0 deletions src-cljs/dakait/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@
(< diffInHours 48) "A day ago"
(< diffInHours 168) (str (quot diffInHours 24) " days ago")
:else (.toDateString (js/Date. dt)))))

(defn duration-since
"Given a time stamp (time in seconds since epoch), returns how much time in seconds has passed since"
[n]
(let [now (quot (.getTime (js/Date.)) 1000)]
(- now n)))
3 changes: 3 additions & 0 deletions src/dakait/files.clj
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,20 @@

(defn all-remote-files [path]
(let [query-path (join-path (config :base-path) path)
now (quot (.getTime (java.util.Date.)) 1000)
entries (list-remote-files query-path)
not-hidden? (fn [e] (not= (.charAt (.getFilename e) 0) \.))
file-type (fn [e] (if (.isDir (.getAttrs e)) "dir" "file"))
file-size (fn [e] (.getSize (.getAttrs e)))
last-modified (fn [e] (-> e (.getAttrs) (.getMTime)))
recent? (fn [e] (< (- now (last-modified e)) 10))
]
(info "query path: " query-path)
(->> entries
(filter not-hidden?)
(map (fn [e] { :name (.getFilename e)
:type (file-type e)
:modified (last-modified e)
:recent (recent? e)
:size (file-size e)})))))

0 comments on commit a4d7974

Please sign in to comment.