USING: accessors calendar checksums combinators.short-circuit
http.client io io.directories io.encodings.binary io.files
io.files.info io.files.unique io.pathnames kernel math
-math.order math.parser present sequences shuffle splitting ;
-
+math.order math.parser namespaces present sequences shuffle
+splitting ;
IN: http.download
-: file-too-old-or-not-exists? ( file duration -- ? )
+: file-too-old-or-not-exists? ( path duration -- ? )
[ ?file-info [ created>> ] ?call ]
[ ago ] bi*
over [ before? ] [ 2drop t ] if ;
-: delete-when-old ( file duration -- deleted? )
+: delete-when-old ( path duration -- deleted/missing? )
dupd file-too-old-or-not-exists? [ ?delete-file t ] [ drop f ] if ;
-: file-matches-checksum? ( file checksum-type bytes -- ? )
+: file-matches-checksum? ( path checksum-type bytes -- ? )
[ checksum-file ] dip = ;
-: delete-when-checksum-mismatches ( file checksum-type bytes -- deleted? )
+: delete-when-checksum-mismatches ( path checksum-type bytes -- deleted? )
dupdd file-matches-checksum? [ drop f ] [ ?delete-file t ] if ;
: file-size= ( path n -- ? ) [ ?file-info [ size>> ] ?call ] dip = ;
drop t
] if ;
-: delete-when-file-size-mismatches? ( file size -- deleted? )
+: delete-when-file-size-mismatches? ( path size -- deleted? )
dupd file-size= [ drop f ] [ ?delete-file t ] if ;
: download-name ( url -- name )
PRIVATE>
-: download-to ( url file -- path )
- [
- [ download-temporary-name binary ] keep
- '[ _ http-write-request ] with-unique-file-writer
- ] dip [ move-file ] keep ;
-
-: download-once-to ( url file -- path )
- dup file-exists? [ nip ] [ download-to ] if ;
+: download-to-temporary-file ( url -- path )
+ [ download-temporary-name binary ] keep
+ '[ _ http-write-request ] with-unique-file-writer ;
-: download-once ( url -- path )
- dup download-name download-once-to ;
+: download-as ( url path -- path )
+ [ download-to-temporary-file ] dip [ ?move-file ] keep ;
-: download-outdated-to ( url file duration -- path )
- 2dup delete-when-old [ drop download-to ] [ drop nip ] if ;
+: download-into ( url directory -- path )
+ [ [ download-to-temporary-file ] keep ] dip
+ dup make-directories to-directory nip
+ [ move-file ] keep ;
: download ( url -- path )
- dup download-name download-to ;
+ dup download-name download-as ;
+
+: download-once-as ( url path -- path )
+ dup file-exists? [ nip ] [ download-as ] if ;
+
+: download-once-into ( url directory -- path ) to-directory download-once-as ;
+
+: download-once ( url -- path ) current-directory get download-once-into ;
+
+: download-outdated-as ( url path duration -- path' )
+ 2dup delete-when-old [ drop download-as ] [ drop nip ] if ;
+
+: download-outdated-into ( url directory duration -- path )
+ [ to-directory ] dip download-outdated-as ;
+
+: download-outdated ( url duration -- path )
+ [ dup download-name current-directory get to-directory nip ] dip download-outdated-as ;