Skip to content

Commit

Permalink
FEAT: thru-cache module updated to version 0.2.0 (exists-thru?, `…
Browse files Browse the repository at this point in the history
…list-thru`) + bugfix
  • Loading branch information
Oldes committed Jul 15, 2024
1 parent d9e52c6 commit f72f9b6
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions src/modules/thru-cache.reb
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@ Rebol [
name: thru-cache
type: module
options: [delay]
version: 0.1.0
exports: [path-thru read-thru load-thru do-thru clear-thru]
version: 0.2.0
exports: [path-thru read-thru load-thru do-thru clear-thru list-thru exists-thru?]
author: @Oldes
file: %thru-cache.reb
home: https://src.rebol.tech/modules/thru-cache.reb
]

;; Initialize the directory for storing local files
so: system/options
unless select so 'thru-cache [
put so 'thru-cache join to-real-file any [
get-env "TEMP"
so/data
] %thru-cache/
sys/log/info 'REBOL ["Using thru-cache:" mold so/thru-cache]
]

path-thru: func [
{Returns the local disk cache path of a remote file}
url [url!] "Remote file address"
return: [file!]
/local so hash file path
/local hash file path
][
so: system/options
unless select so 'thru-cache [
put so 'thru-cache join to-real-file any [
get-env "TEMP"
so/data
] %thru-cache/
sys/log/info 'REBOL ["Using thru-cache:" mold so/thru-cache]
]
unless find so 'thru-cache [return none]
hash: checksum form url 'MD5
file: head (remove back tail remove remove (form hash))
path: dirize append copy so/thru-cache copy/part file 2
Expand Down Expand Up @@ -83,19 +86,19 @@ do-thru: func [

clear-thru: func [
"Removes local disk cache files"
/only filter [string!] "Delete only files where the filter is found"
/only filter [string!] "Delete only the files that match the filter"
/test "Only print files to be deleted"
/local temp dir log
][
unless exists? dir: select system/options 'thru-cache [exit]
unless exists? dir: select so 'thru-cache [exit]
log: dir/read-thru.log
if test [
unless exists? log [exit]
filter: any [filter "*"]
foreach [path url] transcode read log [
if all [
exists? dir/:path
find/any url filter
find/any/match url filter
][ print [as-green skip path 3 url] ]
]
exit
Expand All @@ -118,8 +121,23 @@ clear-thru: func [
()
]

exists-thru?: func[
"Returns true if the remote file is present in the local disk cache"
url [url! file!] "Remote file address"
][
exists? any [all [file? url url] path-thru url]
]

list-thru: func[
"Prints localy stored URLs"
/only filter [string!] "List only the files that match the filter"
][
sys/log/info 'REBOL ["Used thru-cache directory:" mold so/thru-cache]
clear-thru/:only/test filter
]

log-thru-file: func[path url][
write/append system/options/thru-cache/read-thru.log ajoin [
#"%" copy/part tail path -32 SP url LF
write/append so/thru-cache/read-thru.log ajoin [
#"%" copy/part tail path -32 SP mold url LF
]
]

0 comments on commit f72f9b6

Please sign in to comment.