]> gitweb.factorcode.org Git - factor.git/blob - extra/webapps/imagebin/imagebin.factor
core, basis, extra: Remove DOS line endings from files.
[factor.git] / extra / webapps / imagebin / imagebin.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors furnace.actions furnace.redirection
4 html.forms http http.server http.server.dispatchers
5 io.directories io.encodings.utf8 io.files io.pathnames
6 kernel math.parser multiline namespaces sequences urls
7 math ;
8 IN: webapps.imagebin
9
10 TUPLE: imagebin < dispatcher path n ;
11
12 : <uploaded-image-action> ( -- action )
13     <page-action>
14         { imagebin "uploaded-image" } >>template ;
15
16 : next-image-path ( -- path )
17     imagebin get
18     [ path>> ] [ [ 1 + ] change-n n>> number>string ] bi append-path ;
19
20 M: imagebin call-responder*
21     [ imagebin set ] [ call-next-method ] bi ;
22
23 : move-image ( mime-file -- )
24     [ next-image-path dup exists? ] [ drop ] while
25     [ [ temporary-path>> ] dip move-file ]
26     [ [ filename>> ] dip ".txt" append utf8 set-file-contents ] 2bi ;
27
28 : <upload-image-action> ( -- action )
29     <page-action>
30         { imagebin "upload-image" } >>template
31         [
32             "file1" param [ move-image ] when*
33             "file2" param [ move-image ] when*
34             "file3" param [ move-image ] when*
35             "uploaded-image" <redirect>
36         ] >>submit ;
37
38 : <imagebin> ( image-directory -- responder )
39     imagebin new-dispatcher
40         swap [ make-directories ] [ >>path ] bi
41         0 >>n
42         <upload-image-action> "" add-responder
43         <upload-image-action> "upload-image" add-responder
44         <uploaded-image-action> "uploaded-image" add-responder ;
45
46 "resource:images" <imagebin> main-responder set-global