Support downloading of attachments.
[software/mumi.git] / mumi / web / util.scm
1 ;;; mumi -- Mediocre, uh, mail interface
2 ;;; Copyright © 2016, 2017 Ricardo Wurmus <rekado@elephly.net>
3 ;;; Copyright © 2014 David Thompson <davet@gnu.org>
4 ;;;
5 ;;; This program is free software: you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Affero General Public License
7 ;;; as published by the Free Software Foundation, either version 3 of
8 ;;; the License, or (at your option) any later version.
9 ;;;
10 ;;; This program is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Affero General Public License for more details.
14 ;;;
15 ;;; You should have received a copy of the GNU Affero General Public
16 ;;; License along with this program. If not, see
17 ;;; <http://www.gnu.org/licenses/>.
18
19 (define-module (mumi web util)
20 #:use-module (ice-9 match)
21 #:use-module (srfi srfi-1)
22 #:use-module (web request)
23 #:use-module (web uri)
24 #:export (parse-query-string
25 request-path-components
26 file-extension
27 directory?))
28
29 (define (parse-query-string query)
30 "Parse and decode the URI query string QUERY and return an alist."
31 (let lp ((lst (map uri-decode (string-split query (char-set #\& #\=)))))
32 (match lst
33 ((key value . rest)
34 (cons (cons key value) (lp rest)))
35 (() '()))))
36
37 (define (request-path-components request)
38 (split-and-decode-uri-path (uri-path (request-uri request))))
39
40 (define (file-extension file-name)
41 (last (string-split file-name #\.)))
42
43 (define (directory? filename)
44 (string=? filename (dirname filename)))