diff options
author | Christian Hesse <mail@eworm.de> | 2024-04-06 01:13:27 +0200 |
---|---|---|
committer | Christian Hesse <mail@eworm.de> | 2024-04-06 01:13:27 +0200 |
commit | fadc59c562c73a6c825c6f92f68b3be818e01d7d (patch) | |
tree | 160561dc0b578fdaf2c789cd7c79dbfd562e1f42 /global-functions.rsc | |
parent | 86d0f71acb760793c8ccd5320351320f684ce052 (diff) | |
parent | 402f847db2b7446a67634d3720d824a7e6b87e71 (diff) |
Merge branch 'FetchHuge' into next
Diffstat (limited to 'global-functions.rsc')
-rw-r--r-- | global-functions.rsc | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/global-functions.rsc b/global-functions.rsc index e19ed65..ab8860f 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -4,7 +4,7 @@ # Michael Gisbers <michael@gisbers.de> # https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md # -# requires RouterOS, version=7.12 +# requires RouterOS, version=7.13 # # global functions # https://git.eworm.de/cgit/routeros-scripts/about/ @@ -32,6 +32,7 @@ :global DownloadPackage; :global EitherOr; :global EscapeForRegEx; +:global FetchHuge; :global FetchUserAgentStr; :global FormatLine; :global FormatMultiLines; @@ -389,6 +390,52 @@ :return $Return; } +# fetch huge data to file, read in chunks +:set FetchHuge do={ + :local ScriptName [ :tostr $1 ]; + :local Url [ :tostr $2 ]; + :local CheckCert [ :tobool $3 ]; + + :global CleanName; + :global FetchUserAgentStr; + :global GetRandom20CharAlNum; + :global IfThenElse; + :global LogPrint; + :global MkDir; + :global WaitForFile; + + :set CheckCert [ $IfThenElse ($CheckCert = false) "no" "yes-without-crl" ]; + + :local FileName ("tmpfs/" . [ $CleanName $ScriptName ]); + :if ([ $MkDir $FileName ] = false) do={ + $LogPrint error $0 ("Failed creating directory!"); + :return false; + } + + :set FileName ($FileName . "/" . [ $CleanName $0 ] . "-" . [ $GetRandom20CharAlNum ]); + :do { + /tool/fetch check-certificate=$CheckCert $Url dst-path=$FileName \ + http-header-field=({ [ $FetchUserAgentStr $ScriptName ] }) as-value; + } on-error={ + :if ([ $WaitForFile $FileName 500ms ] = true) do={ + /file/remove $FileName; + } + $LogPrint debug $0 ("Failed downloading from: " . $Url); + :return false; + } + $WaitForFile $FileName; + + :local FileSize [ /file/get $FileName size ]; + :local Return ""; + :local VarSize 0; + :while ($VarSize < $FileSize) do={ + :set Return ($Return . ([ /file/read offset=$VarSize chunk-size=32768 file=$FileName as-value ]->"data")); + :set VarSize [ :len $Return ]; + } + /file/remove $FileName; + :return $Return; +} + # generate user agent string for fetch :set FetchUserAgentStr do={ :local Caller [ :tostr $1 ]; |