]> gitweb.factorcode.org Git - factor.git/blob - basis/http/download/download.factor
http.download: fix for new changes
[factor.git] / basis / http / download / download.factor
1 ! Copyright (C) 2024 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors calendar checksums combinators.short-circuit
4 http.client io io.directories io.encodings.binary io.files
5 io.files.info io.files.unique io.pathnames kernel math
6 math.order math.parser present sequences shuffle splitting ;
7
8 IN: http.download
9
10 : file-too-old-or-not-exists? ( file duration -- ? )
11     [ ?file-info [ created>> ] ?call ]
12     [ ago ] bi*
13     over [ before? ] [ 2drop t ] if ;
14
15 : delete-when-old ( file duration -- deleted? )
16     dupd file-too-old-or-not-exists? [ ?delete-file t ] [ drop f ] if ;
17
18 : file-matches-checksum? ( file checksum-type bytes -- ? )
19     [ checksum-file ] dip = ;
20
21 : delete-when-checksum-mismatches ( file checksum-type bytes -- deleted? )
22     dupdd file-matches-checksum? [ drop f ] [ ?delete-file t ] if ;
23
24 : file-size= ( path n -- ? ) [ ?file-info [ size>> ] ?call ] dip = ;
25
26 : file-zero-size? ( path -- ? ) 0 file-size= ;
27
28 : delete-when-zero-size ( path -- deleted-or-not-exists? )
29     dup file-exists? [
30         dup file-zero-size? [ ?delete-file t ] [ drop f ] if
31     ] [
32         drop t
33     ] if ;
34
35 : delete-when-file-size-mismatches? ( file size -- deleted? )
36     dupd file-size= [ drop f ] [ ?delete-file t ] if ;
37
38 : download-name ( url -- name )
39     present file-name "?" split1 drop "/" ?tail drop ;
40
41 <PRIVATE
42
43 : increment-file-extension ( path -- path' )
44     dup file-extension
45     [ ?tail drop ]
46     [
47         ?string>number
48         [ 1 + number>string append ]
49         [ ".1" 3append ] if
50     ] bi ;
51
52 : ?parenthesis-number ( str -- n/str ? )
53     dup { [ "(" head? ] [ ")" tail? ] } 1&&
54     [ rest but-last ?string>number ] [ f ] if ;
55
56 : increment-file-name ( path -- path' )
57     [
58         file-stem " " split1-last
59         ?parenthesis-number
60         [ 1 + number>string "(" ")" surround " " glue ]
61         [ "(1)" append " " glue ] if
62     ] [
63         file-extension
64     ] bi "." glue ;
65
66 : find-next-incremented-name ( path -- path' )
67     dup file-exists? [
68         increment-file-name find-next-incremented-name
69     ] when ;
70
71 : next-download-name ( url -- name )
72     download-name find-next-incremented-name ;
73
74 : http-write-request ( url -- )
75     <get-request> [ write ] with-http-request drop ;
76
77 : download-temporary-name ( url -- prefix suffix )
78     [ "temp." ".temp" ] dip download-name prepend ;
79
80 PRIVATE>
81
82 : download-to ( url file -- path )
83     [
84         [ download-temporary-name binary ] keep
85         '[ _ http-write-request ] with-unique-file-writer
86     ] dip [ move-file ] keep ;
87
88 : download-once-to ( url file -- path )
89     dup file-exists? [ nip ] [ download-to ] if ;
90
91 : download-once ( url -- path )
92     dup download-name download-once-to ;
93
94 : download-outdated-to ( url file duration -- path )
95     2dup delete-when-old [ drop download-to ] [ drop nip ] if ;
96
97 : download ( url -- path )
98     dup download-name download-to ;