renamed paths to urls back
This commit is contained in:
+15
-15
@@ -1,11 +1,11 @@
|
||||
(ns NoteHub.api
|
||||
(:import
|
||||
(:import
|
||||
[java.util Calendar])
|
||||
(:use
|
||||
[NoteHub.settings]
|
||||
[clojure.string :rename {replace sreplace}
|
||||
:only [replace blank? lower-case split-lines split]])
|
||||
(:require
|
||||
(:require
|
||||
[ring.util.codec]
|
||||
[NoteHub.storage :as storage]))
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
(println))
|
||||
|
||||
; Concatenates all fields to a string
|
||||
(defn build-key
|
||||
(defn build-key
|
||||
"Returns a storage-key for the given note coordinates"
|
||||
[[year month day] title]
|
||||
(print-str year month day title))
|
||||
@@ -28,7 +28,7 @@
|
||||
(defn get-date
|
||||
"Returns today's date"
|
||||
[]
|
||||
(map #(+ (second %) (.get (Calendar/getInstance) (first %)))
|
||||
(map #(+ (second %) (.get (Calendar/getInstance) (first %)))
|
||||
{Calendar/YEAR 0, Calendar/MONTH 1, Calendar/DAY_OF_MONTH 0}))
|
||||
|
||||
(defn- create-response
|
||||
@@ -36,12 +36,12 @@
|
||||
([success message & params]
|
||||
(assoc (create-response success) :message (apply format message params))))
|
||||
|
||||
(defn- getPath [noteID & description]
|
||||
(defn- get-path [noteID & description]
|
||||
(if description
|
||||
(str "/" (storage/get-short-url noteID))
|
||||
(let [[year month day title] (split noteID #" ")]
|
||||
(apply str (interpose "/"
|
||||
[year month day (ring.util.codec/url-encode title)])))))
|
||||
(apply str (interpose "/"
|
||||
[domain year month day (ring.util.codec/url-encode title)])))))
|
||||
|
||||
(let [md5Instance (java.security.MessageDigest/getInstance "MD5")]
|
||||
(defn get-signature
|
||||
@@ -55,8 +55,8 @@
|
||||
(defn get-note [noteID]
|
||||
(if (storage/note-exists? noteID)
|
||||
{:note (storage/get-note noteID)
|
||||
:longPath (getPath noteID)
|
||||
:shortPath (getPath noteID :short)
|
||||
:longURL (get-path noteID)
|
||||
:shortURL (get-path noteID :short)
|
||||
:statistics (storage/get-note-statistics noteID)
|
||||
:status (create-response true)}
|
||||
(create-response false "noteID '%s' unknown" noteID)))
|
||||
@@ -73,13 +73,13 @@
|
||||
(when (blank? note) "note is empty")])]
|
||||
(if (empty? errors)
|
||||
(let [[year month day] (get-date)
|
||||
untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %))
|
||||
untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %))
|
||||
(-> note split-lines first (sreplace " " "-") lower-case))
|
||||
trim (fn [s] (apply str (drop-while #(= \- %) s)))
|
||||
title-uncut (-> untrimmed-line trim reverse trim reverse)
|
||||
max-length (get-setting :max-title-length #(Integer/parseInt %) 80)
|
||||
proposed-title (apply str (take max-length title-uncut))
|
||||
date [year month day]
|
||||
date [year month day]
|
||||
title (first (drop-while #(storage/note-exists? (build-key date %))
|
||||
(cons proposed-title
|
||||
(map #(str proposed-title "-" (+ 2 %)) (range)))))
|
||||
@@ -88,8 +88,8 @@
|
||||
(storage/add-note noteID note password)
|
||||
(storage/create-short-url noteID)
|
||||
{:noteID noteID
|
||||
:longPath (getPath noteID)
|
||||
:shortPath (getPath noteID :short)
|
||||
:longURL (get-path noteID)
|
||||
:shortURL (get-path noteID :short)
|
||||
:status (create-response true)}))
|
||||
{:status (create-response false (first errors))}))))
|
||||
|
||||
@@ -107,7 +107,7 @@
|
||||
(if (empty? errors)
|
||||
(do
|
||||
(storage/edit-note noteID note)
|
||||
{:longPath (getPath noteID)
|
||||
:shortPath (getPath noteID :short)
|
||||
{:longURL (get-path noteID)
|
||||
:shortURL (get-path noteID :short)
|
||||
:status (create-response true)})
|
||||
{:status (create-response false (first errors))})))
|
||||
|
||||
+10
-10
@@ -34,10 +34,10 @@
|
||||
|
||||
; Converts given markdown to html and wraps with the main layout
|
||||
(defn- wrap [short-url params md-text]
|
||||
(when md-text
|
||||
(when md-text
|
||||
(layout params (params :title)
|
||||
[:article.bottom-space.markdown md-text]
|
||||
(let [links (map #(link-to
|
||||
(let [links (map #(link-to
|
||||
(if (= :short-url %)
|
||||
(url short-url)
|
||||
(str (params :title) "/" (name %)))
|
||||
@@ -62,7 +62,7 @@
|
||||
fields
|
||||
(text-area {:class :max-width} :note content)
|
||||
[:fieldset#input-elems {:class css-class}
|
||||
(text-field {:class "ui-elem" :placeholder (get-message passwd-msg)}
|
||||
(text-field {:class "ui-elem" :placeholder (get-message passwd-msg)}
|
||||
:plain-password)
|
||||
(submit-button {:class "button ui-elem"
|
||||
:id :publish-button} (get-message command))])])))
|
||||
@@ -82,13 +82,13 @@
|
||||
[:br]
|
||||
[:a.landing-button {:href "/new" :style "color: white"} (get-message :new-page)]]
|
||||
[:div#dashed-line]
|
||||
[:article.helvetica.bottom-space.markdown {:style "font-size: 1em"}
|
||||
[:article.helvetica.bottom-space.markdown {:style "font-size: 1em"}
|
||||
(slurp "LANDING.md")]
|
||||
[:div.centered.helvetica.markdown (get-message :footer)]))
|
||||
|
||||
; Displays the note
|
||||
(defpage "/:year/:month/:day/:title" {:keys [year month day title theme header-font text-font] :as params}
|
||||
(wrap
|
||||
(wrap
|
||||
(storage/create-short-url params)
|
||||
(select-keys params [:title :theme :header-font :text-font])
|
||||
(:note (api/get-note (api/build-key [year month day] title)))))
|
||||
@@ -133,7 +133,7 @@
|
||||
; Update Note Page
|
||||
(defpage "/:year/:month/:day/:title/edit" {:keys [year month day title]}
|
||||
(let [noteID (api/build-key [year month day] title)]
|
||||
(input-form "/update-note" :update
|
||||
(input-form "/update-note" :update
|
||||
(html (hidden-field :noteID noteID))
|
||||
(:note (api/get-note noteID)) :enter-passwd)))
|
||||
|
||||
@@ -145,7 +145,7 @@
|
||||
(if (storage/valid-publisher? pid)
|
||||
(let [resp (api/post-note note pid (api/get-signature (str pid psk note)) password)]
|
||||
(if (get-in resp [:status :success])
|
||||
(redirect (:longPath resp))
|
||||
(redirect (:longURL resp))
|
||||
(response 400)))
|
||||
(response 500)))
|
||||
(response 400)))
|
||||
@@ -155,11 +155,11 @@
|
||||
(let [pid api/domain
|
||||
psk (storage/get-psk pid)]
|
||||
(if (storage/valid-publisher? pid)
|
||||
(let [resp (api/update-note noteID note pid
|
||||
(api/get-signature (str pid psk noteID note password))
|
||||
(let [resp (api/update-note noteID note pid
|
||||
(api/get-signature (str pid psk noteID note password))
|
||||
password)]
|
||||
(if (get-in resp [:status :success])
|
||||
(redirect (:longPath resp))
|
||||
(redirect (:longURL resp))
|
||||
(response 403)))
|
||||
(response 500))))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user