Skip to content

Commit

Permalink
Tactical commit: I'm fairly sure this is close to good.
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-brooke committed Feb 10, 2020
1 parent ad5e41c commit 40f4f13
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 69 deletions.
4 changes: 2 additions & 2 deletions resources/templates/list-uploads.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
<th>{{entry.base-name}}</th>
<td>{{entry.modified}}</td>
<td>
{% if entry.is-image %} ![{{entry.name|capitalize}}](uploads/{{entry.base-name}}) {% else %} [{{entry.name|capitalize}}](uploads/{{entry.base-name}}) {% endif %}
{% if entry.is-image %} ![{{entry.name|capitalize}}]({{entry.resource}}) {% else %} [{{entry.name|capitalize}}](uploads/{{entry.resource}}) {% endif %}
</td>
<td>
{% if entry.is-image %} <img src="uploads/{{entry.base-name}}" alt="{{entry.name|capitalize}}"/> {% else %} <a href="uploads/{{entry.base-name}}">link</a> {% endif %}
{% if entry.is-image %} <img src="{{entry.resource}}" alt="{{entry.name|capitalize}}"/> {% else %} <a href="{{entry.resource}}">link</a> {% endif %}
</td>

</tr>
Expand Down
29 changes: 16 additions & 13 deletions resources/templates/upload.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
{% extends "templates/base.html" %}
{% block content %}
<div id="content" class="auth">
{% if uploaded %}
{% if is-image %}
<p>
<img id="uploaded-image" alt="Uploaded image" src="uploads/{{uploaded}}"/>
{% if has-uploaded %}
{% for upload in uploaded %}
{{upload.filename}}
{% if upload.is-image %)
<p>
<img id="uploaded-image" alt="Uploaded image" src="{{upload.resource}}"/>

{% i18n file-upload-link-text %}:
{% i18n file-upload-link-text %}:

<code>![Uploaded image](uploads/{{uploaded}})</code>
</p>
{% else %}
<p>
{% i18n file-upload-link-text %}:
<code>![{{upload.filename}}]({{upload.resource}})</code>
</p>
{% else %}
<p>
{% i18n file-upload-link-text %}:

<code>[Uploaded file](uploads/{{uploaded}})</code>
</p>
{% endif %}
<code>[{{upload.filename}}]({{upload.resource}})</code>
</p>
{% endif %}
{% endfor %}
{% else %}
<form action="{{servlet-context}}/upload" enctype="multipart/form-data" method="POST">
{% csrf-field %}
Expand Down
52 changes: 27 additions & 25 deletions src/smeagol/routes/wiki.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
(:require [cemerick.url :refer (url url-encode url-decode)]
[clj-jgit.porcelain :as git]
[clojure.java.io :as cjio]
[clojure.pprint :refer [pprint]]
[clojure.string :as cs]
[clojure.walk :refer :all]
[compojure.core :refer :all]
Expand All @@ -22,7 +23,7 @@
[smeagol.sanity :refer [show-sanity-check-error]]
[smeagol.util :as util]
[smeagol.uploads :as ul]
[taoensso.timbre :as timbre]
[taoensso.timbre :as log]
[com.stuartsierra.component :as component]
[smeagol.include.resolve-local-file :as resolve]
[smeagol.include :as include]))
Expand Down Expand Up @@ -54,7 +55,7 @@
"Process `source-text` and save it to the specified `file-path`, committing it
to Git and finally redirecting to wiki-page."
[params suffix request]
(timbre/trace (format "process-source: '%s'" request))
(log/trace (format "process-source: '%s'" request))
(let [source-text (:src params)
page (:page params)
file-name (str page suffix)
Expand All @@ -64,7 +65,7 @@
user (session/get :user)
email (auth/get-email user)
summary (format "%s: %s" user (or (:summary params) "no summary"))]
(timbre/info (format "Saving %s's changes ('%s') to %s in file '%s'" user summary page file-path))
(log/info (format "Saving %s's changes ('%s') to %s in file '%s'" user summary page file-path))
(spit file-path source-text)
(git/git-add git-repo file-name)
(git/git-commit git-repo summary {:name user :email email})
Expand Down Expand Up @@ -94,9 +95,9 @@
user (session/get :user)]
(if-not
exists?
(timbre/info
(log/info
(format "File '%s' not found; creating a new file" file-path))
(timbre/info (format "Opening '%s' for editing" file-path)))
(log/info (format "Opening '%s' for editing" file-path)))
(cond src-text (process-source params suffix request)
true
(layout/render template
Expand Down Expand Up @@ -125,7 +126,7 @@
(defn wiki-page
"Render the markdown page specified in this `request`, if any. If none found, redirect to edit-page"
[request]
(timbre/trace (format "wiki-page: '%s'" request))
(log/trace (format "wiki-page: '%s'" request))
(or
(show-sanity-check-error)
(let [params (keywordize-keys (:params request))
Expand All @@ -135,7 +136,7 @@
exists? (.exists (clojure.java.io/as-file file-path))]
(cond exists?
(do
(timbre/info (format "Showing page '%s' from file '%s'" page file-path))
(log/info (format "Showing page '%s' from file '%s'" page file-path))
(layout/render "wiki.html"
(merge (util/standard-params request)
{:title page
Expand All @@ -156,7 +157,7 @@
page (url-decode (or (:page params) (util/get-message :default-page-title request)))
file-name (str page ".md")
repo-path util/content-dir]
(timbre/info (format "Showing history of page '%s'" page))
(log/info (format "Showing history of page '%s'" page))
(layout/render "history.html"
(merge (util/standard-params request)
{:title (util/get-message :history-title-prefix request)
Expand Down Expand Up @@ -187,10 +188,11 @@
(let
[params (keywordize-keys (:params request))
data-path (str util/content-dir "/uploads/")
cl (count (io/resource-path))
files
(map
#(zipmap
[:base-name :is-image :modified :name]
[:base-name :is-image :modified :name :resource]
[(fs/base-name %)
(if
(and (fs/extension %)
Expand All @@ -199,11 +201,13 @@
(if
(fs/mod-time %)
(format-instant (fs/mod-time %)))
(fs/name %)])
(fs/name %)
(subs (str (fs/absolute %)) cl)])
(remove
#(or (cs/starts-with? (fs/name %) ".")
(fs/directory? %))
(file-seq (clojure.java.io/file data-path))))]
(log/info (with-out-str (pprint files)))
(layout/render
"list-uploads.html"
(merge (util/standard-params request)
Expand Down Expand Up @@ -236,20 +240,18 @@
uploaded (if upload (ul/store-upload params data-path))
user (session/get :user)
summary (format "%s: %s" user (or (:summary params) "no summary"))]
(if
uploaded
(do
(git/git-add git-repo (str data-path (fs/name uploaded)))
(git/git-commit git-repo summary {:name user :email (auth/get-email user)})))
;; (if
;; uploaded
;; (do
;; (map
;; #(git/git-add git-repo (str :resource %))
;; uploaded)
;; (git/git-commit git-repo summary {:name user :email (auth/get-email user)})))
(layout/render "upload.html"
(merge (util/standard-params request)
{:title (util/get-message :file-upload-title request)
:uploaded (if uploaded (fs/base-name uploaded))
:is-image (if
uploaded
(image-extns
(cs/lower-case
(fs/extension uploaded))))}))))
:has-uploaded (not (empty? uploaded))
:uploaded uploaded}))))

(defn version-page
"Render a specific historical version of a page"
Expand All @@ -259,7 +261,7 @@
version (:version params)
file-name (str page ".md")
content (hist/fetch-version util/content-dir file-name version)]
(timbre/info (format "Showing version '%s' of page '%s'" version page))
(log/info (format "Showing version '%s' of page '%s'" version page))
(layout/render "wiki.html"
(merge (util/standard-params request)
{:title (str (util/get-message :vers-col-hdr request) " " version " " (util/get-message :of request) " " page)
Expand All @@ -274,7 +276,7 @@
page (url-decode (or (:page params) (util/get-message :default-page-title request)))
version (:version params)
file-name (str page ".md")]
(timbre/info (format "Showing diff between version '%s' of page '%s' and current" version page))
(log/info (format "Showing diff between version '%s' of page '%s' and current" version page))
(layout/render "wiki.html"
(merge (util/standard-params request)
{:title
Expand Down Expand Up @@ -303,11 +305,11 @@
action (:action form-params)
user (session/get :user)
redirect-to (:redirect-to params)]
(if redirect-to (timbre/info (str "After auth, redirect to: " redirect-to)))
(if redirect-to (log/info (str "After auth, redirect to: " redirect-to)))
(cond
(= action (util/get-message :logout-label request))
(do
(timbre/info (str "User " user " logging out"))
(log/info (str "User " user " logging out"))
(session/remove! :user)
(response/redirect redirect-to))
(and username password (auth/authenticate username password))
Expand Down
62 changes: 37 additions & 25 deletions src/smeagol/uploads.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[image-resizer.core :refer [resize]]
[image-resizer.util :refer :all]
[me.raynes.fs :as fs]
[noir.io :as nio]
[smeagol.configuration :refer [config]]
[taoensso.timbre :as log])
(:import [java.io File]
Expand Down Expand Up @@ -53,42 +54,48 @@
"Writes img, a RenderedImage, to dest, something that can be turned into
a file with clojure.java.io/file.
Takes the following keys as options:
:format - :gif, :jpg, :png or anything supported by ImageIO
:quality - for JPEG images, a number between 0 and 100"
:format - :gif, :jpg, :png or anything supported by ImageIO
:quality - for JPEG images, a number between 0 and 100"
[^RenderedImage img dest & {:keys [format quality] :or {format :jpg}}]
(if (or (not quality) (not (contains? #{:jpg :jpeg} format)))
(ImageIO/write img (name format) (io/file dest))
(let [fmt (rest (fs/extension (cs/lower-case dest)))
iw (doto ^ImageWriter (first
(iterator-seq
(ImageIO/getImageWritersByFormatName
"jpeg")))
(.setOutput (FileImageOutputStream. (io/file dest))))
iw-param (doto ^ImageWriteParam (.getDefaultWriteParam iw)
(.setCompressionMode ImageWriteParam/MODE_EXPLICIT)
(.setCompressionQuality (float (/ quality 100))))
iio-img (IIOImage. img nil nil)]
(.write iw nil iio-img iw-param))))
(log/info "Writing to " dest)
(let [fmt (subs (fs/extension (cs/lower-case dest)) 1)
iw (doto ^ImageWriter (first
(iterator-seq
(ImageIO/getImageWritersByFormatName
fmt)))
(.setOutput (FileImageOutputStream. (io/file dest))))
iw-param (doto ^ImageWriteParam (.getDefaultWriteParam iw)
(.setCompressionMode ImageWriteParam/MODE_EXPLICIT)
(.setCompressionQuality (float (/ (or quality 75) 100))))
iio-img (IIOImage. img nil nil)]
(.write iw nil iio-img iw-param)))

(def image?
(memoize
(fn [filename]
(image-file-extns (fs/extension (cs/lower-case (str filename)))))))

(defn auto-thumbnail
"For each of the thumbnail sizes in the configuration, create a thumbnail
for the file with this `filename` on this `path`, provided that it is a
scalable image and is larger than the size."
([^String path ^String filename]
(if
(image-file-extns (fs/extension (cs/lower-case filename)))
(let [original (buffered-image (.File (str path filename)))] ;; fs/file?
(image? filename)
(let [original (buffered-image (File. (str path filename)))] ;; fs/file?
(map
#(auto-thumbnail path filename % original)
(keys (config :thumbnails))))
(log/info filename " cannot be thumbnailed.")))
([^String path ^String filename size ^RenderedImage image]
(let [s (-> config :thumbnails size)
d (dimensions image)]
d (dimensions image)
p (io/file path (name size) filename)]
(if (and (integer? s) (some #(> % s) d))
(do
(write-image (resize image s s) (io/file path (name size) filename))
(log/info "Created a " size " thumbnail of " filename))
(write-image (resize image s s) p)
(log/info "Created a " size " thumbnail of " filename)
{:size size :filename filename :location (str p) :is-image true})
(log/info filename "is smaller than " s "x" s " and was not scaled to " size)))))

(defn store-upload
Expand All @@ -108,11 +115,16 @@
(str "store-upload mv file: " tmp-file " to: " path filename))
(if tmp-file
(try
(do
(.renameTo tmp-file
(File. (str path filename))) ;; TODO: fs/file
(auto-thumbnail path filename)
(File. (str path filename)))
(let [p (io/file path filename)]
(.renameTo tmp-file p)
(remove
nil?
(cons
{:size :original
:filename filename
:location (str p)
:is-image (and (image? filename) true)}
(remove nil? (or (auto-thumbnail path filename) '())))))
(catch Exception x
(log/error (str "Failed to move " tmp-file " to " path filename "; " (type x) ": " (.getMessage x)))
(throw x)))
Expand Down
10 changes: 6 additions & 4 deletions src/smeagol/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
smeagol.util
(:require [clojure.java.io :as cjio]
[environ.core :refer [env]]
[me.raynes.fs :as fs]
[noir.io :as io]
[noir.session :as session]
[scot.weft.i18n.core :as i18n]
Expand Down Expand Up @@ -39,10 +40,11 @@
(:start-page config))

(def content-dir
(or
(:content-dir config)
(cjio/file (io/resource-path) "content")))

(str
(fs/absolute
(or
(:content-dir config)
(cjio/file (io/resource-path) "content")))))

(defn standard-params
"Return a map of standard parameters to pass to the template renderer."
Expand Down

0 comments on commit 40f4f13

Please sign in to comment.