X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=blobdiff_plain;f=basis%2Fhttp%2Fdownload%2Fdownload.factor;h=3f8b2be8ceb4976986a39fd9b349f088007e413b;hp=1d087a07bd471aa3d45b4afa82d318a7493adc64;hb=refs%2Fheads%2Fmaster;hpb=0c928a1a9dfbcb39fa495286d97443269301e7e5 diff --git a/basis/http/download/download.factor b/basis/http/download/download.factor index 1d087a07bd..ee464b9afc 100644 --- a/basis/http/download/download.factor +++ b/basis/http/download/download.factor @@ -3,22 +3,22 @@ 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 = ; @@ -32,7 +32,7 @@ IN: http.download 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 ) @@ -84,20 +84,33 @@ IN: http.download 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 ;