aboutsummaryrefslogtreecommitdiffstats
path: root/hypo.hy
blob: 07d31cc27fc0f07d71ae85c9af2ea81b964376da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env hy
;; Hypo -- Quickly share stuff
;; Copyright (C) 2013  Tom Willemse

;; Hypo is free software: you can redistribute it and/or modify it
;; under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.

;; Hypo is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
;; Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with Hypo.  If not, see <http://www.gnu.org/licenses/>.

(import web sys os hashlib datetime shutil
        [pygments [highlight]]
        [pygments.lexers [get-lexer-by-name guess-lexer-for-filename]]
        [pygments.formatters [HtmlFormatter]]
        [pygments.util [ClassNotFound]]
        [gittle [Gittle]])

(try (import [config [*]])
     (catch [ImportError]
       (print "Please copy the config.example.hy to config.hy and set"
              "the values to your preference.")
       (sys.exit 1)))

(def render (web.template.render "templates/"))
(def urls (, (+ "/" *prefix*) "index"
             (+ "/" *prefix* "raw/(.*)") "raw"
             (+ "/" *prefix* "dl/(.*)") "download"
             (+ "/" *prefix* "([a-f0-9]{7})$") "html"
             (+ "/" *prefix* "(.*)") "upload"))

(defun hashes [name]
  (let ((hasher (hashlib.sha1)))
    (hasher.update name)
    (hasher.update (str (datetime.datetime.now)))
    (let ((digest (hasher.hexdigest)))
      (, (slice digest 0 7) digest))))

(defun get-type [ext]
  (cond
   [(in ext (, ".jpg" ".jpeg" ".png" ".gif")) "image"]
   [True "text"]))

(defun read-file [filename]
  (let (res)
    (with [[f (file filename "r")]]
          (setv res (f.read)))
    res))

(defun no-such-file []
  (setv web.ctx.status (str "404 Not Found"))
  (render.notfound))

(defun get-lexer [filename content]
  "Try to guess the correct lexer by FILENAME and CONTENT.

If no lexer is found fallback onto the text lexer."
  (try (guess-lexer-for-filename filename content)
       (catch [ClassNotFound]
         (get-lexer-by-name "text"))))

(defun get-raw [self name]
  (let ((dirname (+ "files/" (os.path.dirname name)))
        (repo (and (os.path.exists dirname)
                   (Gittle dirname)))
        (resp (if repo
                (get (.commit-file repo "HEAD" (os.path.basename name))
                     "data"))))
    (or resp (no-such-file))))

(defun get-attachment [self name]
  (let ((dirname (+ "files/" (os.path.dirname name)))
        (repo (and (os.path.exists dirname)
                   (Gittle dirname))))
    (if repo
      (progn
       (web.header "Content-Disposition"
                   (+ "attachment; filename=\"" name "\""))
       (get (.commit-file repo "HEAD" (os.path.basename name)) "data"))
      (no-such-file))))

(defun render-file [hash repo ref filename]
  (if (not (os.path.isdir filename))
    (let ((content (get (.commit-file repo ref filename) "data"))
          (lexer (get-lexer filename content))
          (formatter (HtmlFormatter))
          (args {"file" filename "hash" hash}))
      (.update
       args (if (in (get (os.path.splitext filename) 1)
                    [".png" ".jpg" ".jpeg" ".gif"])
              {"content" (kwapply (render.image)
                                  {"name" filename
                                   "hash" hash})
               "style" ""}
              {"content" (highlight content lexer formatter)
               "style" (formatter.get-style-defs ".highlight")}))
      (kwapply (render.main) args))
    ""))

(defun get-html [self name]
  (let ((dirname (+ "files/" name))
        (repo (and (os.path.exists dirname)
                   (Gittle dirname))))
    (if repo
      (car (list-comp (render-file name repo "HEAD" f)
                      [f (.iterkeys (.commit-tree repo "HEAD"))]
                      (not (or (= f ".")
                               (= f "..")))))
      (no-such-file))))

(defun delete-dir [self name]
  (let ((dirname (+ "files/" name)))
    (if (os.path.exists dirname)
      (do (shutil.rmtree dirname)
          (+ "Succesfully removed " name "\n"))
      (no-such-file))))

(defun upload-file [self name]
  (let ((h (hashes name))
        (dirname (+ "files/" (get h 0))))
    (os.mkdir dirname)
    (with [[f (file (+ dirname "/" name) "w")]]
          (.write f (web.data)))
    (let ((repo (Gittle.init dirname)))
      (.stage repo [(str name)])
      (kwapply (repo.commit)
               {"name" "Hypo"
                "email" "hypo@ryuslash.org"
                "message" "Initial commit"}))
    (setv web.ctx.status (str "201 Created"))
    (+ web.ctx.home "/" *prefix* (get h 0) "\n")))

(defclass raw []
  [[GET get-raw]])

(defclass download []
  [[GET get-attachment]])

(defclass html []
  [[GET get-html]
   [DELETE delete-dir]])

(defclass upload []
  [[PUT upload-file]])

(defclass index []
  [[GET (lambda [self] (render.index))]])

(defun hypo-start [argv]
  (let ((sys.argv argv)
        (app (web.application urls (globals))))
    (.run app)))