-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.lisp
103 lines (87 loc) · 3.81 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
(in-package #:displayer)
(defun output (data &optional (message "Ok."))
(if (and (string= (post/get "browser") "true") (referer))
(redirect (merge-url (referer) :parameters `(("message" . ,(princ-to-string message)))))
(api-output data :message message)))
(define-api displayer/video (name) ()
(let ((file (probe-file (video-file name))))
(if file
(api-output (video-data file))
(error 'api-argument-invalid :argument 'name))))
(define-api displayer/video/file (name) ()
(let ((file (probe-file (video-file name))))
(cond (file
(setf (header "Content-Disposition") (format NIL "attachment; filename=~s" (file-namestring file)))
(serve-file file))
(T
(error 'api-argument-invalid :argument 'name)))))
(define-api displayer/video/thumbnail (name) ()
(let ((file (probe-file (video-thumbnail name))))
(if file
(serve-file file)
(error 'api-argument-invalid :argument 'name))))
(define-api displayer/video/list () ()
(api-output (mapcar #'video-data (list-videos))))
(define-api displayer/video/upload (file &optional name) ()
(let ((name (or* name (pathname-name (second file))))
(tmp (uiop:tmpize-pathname (make-pathname :name "video" :type "mp4" :defaults (uiop:temporary-directory)))))
(rename-file (first file) tmp)
(let ((task (make-instance 'add-video :input tmp :name name)))
(output (id task) "Upload started"))))
(define-api displayer/video/download (url name) ()
(let ((task (make-instance 'add-video :input url :name name)))
(output (id task) "Download started")))
(define-api displayer/video/toggle (name) ()
(let* ((enabled-p (video-enabled-p name)))
(cond (enabled-p
(disable-video name)
(ignore-errors (remove-from-playlist name)))
(T
(enable-video name)
(ignore-errors (add-to-playlist name)))))
(output NIL (if (video-enabled-p name) "Video enabled" "Video disabled")))
(define-api displayer/video/delete (name) ()
(let ((task (make-instance 'delete-video :name name)))
(output (id task) "Delete queued")))
(define-api displayer/video/play (name) ()
(let ((task (make-instance 'play-video :name name)))
(output (id task) "Video playback queued")))
(define-api displayer/playback () ()
(api-output (mktab :status (if (video-running-p) "running" "stopped"))))
(define-api displayer/playback/restart () ()
(let ((task (make-instance 'restart-video)))
(output (id task) "Restart queued")))
(define-api displayer/task (id) ()
(let ((task (find-task id)))
(if task
(api-output (mktab :id (id task)
:created-at (created-at task)
:status (string-downcase (status task))
:message (message task)))
(error 'api-argument-invalid :argument 'id))))
(define-api displayer/task/list () ()
(api-output (loop for task in (list-tasks)
collect (mktab :id (id task)
:created-at (created-at task)
:status (string-downcase (status task))
:message (message task)))))
(define-api displayer/task/clear (&optional id) ()
(cond (id
(let ((task (find-task id)))
(if task
(clear task)
(error 'api-argument-invalid :argument 'id))
(output T "Task cleared")))
(T
(clear T)
(output T "All tasks cleared"))))
(define-api displayer/task/restart (&optional id) ()
(cond (id
(let ((task (find-task id)))
(if task
(setf (status task) :pending)
(error 'api-argument-invalid :argument 'id))
(output T "Task restarted")))
(T
(restart-task-runner)
(output T "Task runner restarted"))))