001 (ns ^{:doc "Route which handles the upload of worlds/rules from the client."
002 :author "Simon Brooke"}
003 mw-ui.routes.load
004 (:require [clojure.walk :refer [keywordize-keys]]
005 [noir.io :as io]
006 [noir.session :as session]
007 [mw-ui.layout :as layout]))
008
009 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
010 ;;;;
011 ;;;; mw-ui: a servlet user/visualisation interface for MicroWorld.
012 ;;;;
013 ;;;; This program is free software; you can redistribute it and/or
014 ;;;; modify it under the terms of the GNU General Public License
015 ;;;; as published by the Free Software Foundation; either version 2
016 ;;;; of the License, or (at your option) any later version.
017 ;;;;
018 ;;;; This program is distributed in the hope that it will be useful,
019 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
020 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021 ;;;; GNU General Public License for more details.
022 ;;;;
023 ;;;; You should have received a copy of the GNU General Public License
024 ;;;; along with this program; if not, write to the Free Software
025 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
026 ;;;; USA.
027 ;;;;
028 ;;;; Copyright (C) 2014 Simon Brooke
029 ;;;;
030 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
031
032
033 (defn- upload [file]
034 (io/upload-file "/tmp/" file)
035 (cond
036 (session/put! :world
037 (with-open [_ (java.io.FileReader. (:tempfile file))] (read)))
038 (str "Successfully loaded your world from " (:filename file))))
039
040
041 (defn load-page
042 "If no args, show the load form; with args, load a world file from the client.
043
044 *NOTE* that this reads a Clojure form from an untrusted client and should almost
045 certainly NOT be enabled on a public-facing site, especially not on the Internet.
046
047 *TODO* doesn't work yet."
048 ([]
049 (load-page nil))
050 ([request]
051 (let [file (:file request)]
052 (try
053 (layout/render "load.html"
054 {:title "Load World"
055 :message (upload file)})
056
057 (catch Exception any
058 (layout/render "load.html"
059 {:title "Load World"
060 :message "Failed to load your world"
061 :error (str (.getName (.getClass any)) ": " (.getMessage any))}))))))
062