-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.lisp
180 lines (163 loc) · 9.44 KB
/
api.lisp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
(in-package #:keygen)
(define-api keygen/project/list () (:access (perm keygen))
;; FIXME: filter by access
(api-output (list-projects)))
(define-api keygen/project/find (name) (:access (perm keygen))
(api-output (find-package name)))
(define-api keygen/project/cover (project) ()
(setf (header "Cache-Control") "public")
(serve-file (project-cover project)))
(define-api keygen/package/list (project) (:access (perm keygen))
(let ((project (ensure-project (db:ensure-id project))))
(api-output (list-packages project))))
(define-api keygen/package/files (package) (:access (perm keygen))
(let ((package (ensure-package (db:ensure-id package))))
(api-output (list-files package))))
(define-api keygen/package/keys (package) (:access (perm keygen))
(let ((package (ensure-package (db:ensure-id package))))
(api-output (list-keys package))))
(define-api keygen/file/list (project) (:access (perm keygen))
(let ((project (ensure-project (db:ensure-id project))))
(api-output (list-files project))))
(define-api keygen/file/upload (file payload &optional version chunk) (:access (perm keygen))
(let* ((file (ensure-file (db:ensure-id file)))
(tmp (make-pathname :type "tmp" :defaults (file-pathname file)))
(chunk (or* chunk)))
(cond ((null chunk)
(uiop:copy-file (first payload) (file-pathname file))
(when version
(edit-file file :version version))
(edit-file file :last-modified (get-universal-time)))
((string= "0" chunk)
(uiop:copy-file (first payload) tmp))
((string= "end" chunk)
(uiop:rename-file-overwriting-target tmp (file-pathname file))
(edit-file file :last-modified (get-universal-time)))
(T
(with-open-file (in (first payload) :direction :input :element-type '(unsigned-byte 8))
(with-open-file (out tmp :direction :output :element-type '(unsigned-byte 8) :if-exists :append)
(uiop:copy-stream-to-stream in out :element-type '(unsigned-byte 8))))))
(output file "File uploaded" "keygen/project/~a" (dm:field file "project"))))
(define-api keygen/file/download (file) (:access (perm keygen))
(let ((file (ensure-file (db:ensure-id file))))
(setf (header "Cache-Control") "private")
(setf (header "Last-Modified") (format-last-modified (dm:field file "last-modified")))
(setf (header "Content-Disposition") (format NIL "inline; filename=\"~a\"" (dm:field file "filename")))
(serve-file (file-pathname file) "application/octet-stream")))
(define-api keygen/key/list (package) (:access (perm keygen))
(let ((package (ensure-package (db:ensure-id package))))
(api-output (list-keys package))))
(define-api keygen/key/generate (package count &optional segment expires unclaimable) (:access (perm keygen))
(let* ((package (ensure-package (db:ensure-id package)))
(project (ensure-project package))
(codes (generate-keys package (parse-integer count)
:segment segment
:expires (when (or* expires) (parse-integer expires))
:unclaimable (string= "true" unclaimable))))
(setf (header "Cache-Control") "no-store")
(setf (header "Content-Disposition") (format NIL "inline; filename=\"~a-~a-~a.csv\""
(dm:field project "title")
(or segment (dm:field package "title"))
(format-filesystem-date (local-time:now))))
(setf (content-type *response*) "text/csv;encoding=utf-8")
(format NIL "~{~a~^~%~}" codes)))
(define-api keygen/key/export (package &optional segment format) (:access (perm keygen))
(let* ((package (ensure-package (db:ensure-id package)))
(project (ensure-project package))
(keys (list-keys package :segment (or* segment))))
(setf (header "Cache-Control") "no-store")
(setf (header "Content-Disposition") (format NIL "inline; filename=\"~a-~a-~a.~a\""
(dm:field project "title")
(or segment (dm:field package "title"))
(format-filesystem-date (local-time:now))
format))
(cond ((string-equal "txt" format)
(setf (content-type *response*) "text/plain;encoding=utf-8")
(with-output-to-string (out)
(dolist (key keys)
(format out "~a~%" (dm:field key "code")))))
((string-equal "csv" format)
(setf (content-type *response*) "text/csv;encoding=utf-8")
(with-output-to-string (out)
(format out "Code, URL, Package, Segment, Owner, Date Created, Date Expires, Date First Access, Date Last Access, Access Count~%")
(flet ((date (date)
(when (and date (< 0 date))
(format-machine-date date out))
(format out ", ")))
(dolist (key keys)
(format out "~a, ~a, ~a, ~@[~a~], ~@[~a~], " (dm:field key "code") (key-url key) (dm:field package "title") (dm:field key "segment") (dm:field key "owner-email"))
(date (dm:field key "expires"))
(date (dm:field key "first-access"))
(date (dm:field key "last-access"))
(format out "~d~%" (dm:field key "access-count"))))))
((string-equal "json" format)
(setf (content-type *response*) "application/json;encoding=utf-8")
(funcall (api-format "json") keys)))))
(defun claim-email (key)
(let* ((url (key-url key))
(package (ensure-package key))
(project (ensure-project package))
(subject (format NIL "Your code for ~a ~a" (dm:field project "title") (dm:field package "title")))
(content (r-clip:process (@template "email.ctml")
:subject subject
:package package
:project project
:key key
:url url))
(text (with-output-to-string (out)
(lquery:$ content "body" (text) (each (lambda (text) (write-line text out))))))
(plump:*tag-dispatchers* plump:*html-tags*))
(values subject text (plump:serialize content NIL))))
(define-api keygen/key/claim (code email) ()
(db:with-transaction ()
(let ((key (find-key code))
(authcode (email-auth-code email)))
(unless (key-valid-p key)
(error 'radiance:request-not-found))
(when (string= "-" (dm:field key "owner-email"))
(error 'radiance:request-denied))
(setf (dm:field key "owner-email") email)
(multiple-value-bind (subject text html) (claim-email key)
(mail:send email subject text :html html))
(dm:save key)
(let* ((message (format NIL "Code registered to ~a. Please check your email!" email))
(target (uri-to-url "keygen/access"
:representation :external
:query `(("code" . ,code)
("authcode" . ,authcode)
("message" . ,message)))))
(if (string= "true" (post/get "browser"))
(redirect target)
(api-output key :message message :target target))))))
(define-api keygen/key/resolve (code file &optional authcode) ()
(let ((key (find-key code)))
(unless (key-valid-p key authcode)
(error 'radiance:request-not-found))
(let ((file (ensure-file (db:ensure-id file))))
(incf (dm:field file "download-count"))
(dm:save file)
(setf (header "Cache-Control") "no-cache, no-transform")
(setf (header "Last-Modified") (format-last-modified (dm:field file "last-modified")))
(setf (header "Content-Disposition") (format NIL "inline; filename=\"~a\"" (dm:field file "filename")))
(serve-file (file-pathname file) "application/octet-stream"))))
(define-api keygen/key/files (code &optional authcode) ()
(let ((key (ignore-errors (find-key code))))
(unless (key-valid-p key authcode)
(api-error "Invalid key ~a" code))
(setf (header "Cache-Control") "no-cache, no-transform")
(api-output (loop for file in (list-files key)
for tab = (make-hash-table :test 'equal)
do (setf (gethash "filename" tab) (dm:field file "filename"))
(setf (gethash "types" tab) (map 'vector #'type-name (dm:field file "types")))
(setf (gethash "version" tab) (dm:field file "version"))
(setf (gethash "last-modified" tab) (universal-to-unix-time (dm:field file "last-modified")))
(setf (gethash "url" tab) (uri-to-url "keygen/api/keygen/key/resolve"
:representation :external
:query `(("code" . ,code)
("authcode" . ,authcode)
("file" . ,(princ-to-string (dm:field file "file"))))))
collect tab))))
(define-api keygen/key/revoke (code) (:access (perm keygen))
(let ((key (find-key code)))
(invalidate-key key)
(api-output NIL)))