Software > HTML & templates

[Question] How to download and save an external file (Macro)

(1/5) > >>

LeoNeeson:
» Update: February 10, 2020
Latest version/revision: HERE.

» Change log:
May 27, 2018 > Fixed a serious security hole found on this script (by Mars)
August 02, 2019 > Using %encoded-folder% as the destination (by danny).

» To-do list:
- Progress bar (when uploading)
- Show a link (to the uploaded file)


» ATTENTION: The text below, is left as-is (only as a reference), so, it's not recommended for public use, because in those previous/initials stages of this script, a serious security hole was found (that allowed access to any file on the entire hard disk hosting HFS), which was solved HERE (thanks to Mars). There is no need to panic, but if you use an old version, just be sure to restrict the access of this script only to yourself. It is recommended to always use the latest version.


Hi there! I was reading these wiki pages (Template macros & scripting commands), trying to being able to download and save an external file to the current folder (Remote upload), but I can't get it working.

Please check my code (in my example, there is an URL already loaded, of a Tiny Core Linux ISO file). The following code works fine, but it's NOT recommended as it could overload the RAM if you are downloading a big file (I only leave this as reference):

THIS CODE WORKS:

--- Code: ---<form method='post'>
URL: <input name='url' value="http://distro.ibiblio.org/tinycorelinux/9.x/x86/release/Core-9.0.iso">
<br>Filename: <input name='dest' value="%folder%Core-9.0-GOOD.iso">
<br><input type='submit'>
</form>
{.set|url|{.postvar|url.}.}
{.break|if={.not|{.^url.}.}.}
{.set|dest| {.or|{.filename|{.postvar|dest.}.}|{.filename|{.^url.}.}|downloaded.} .}

{.load|{.^url.}|var=data.}
{.and
| {.length|{.save| ./{.^dest.} |var=data.}.}
| Downloaded {.^dest.} for {.length|var=data.} bytes
.}

--- End code ---

The following is the recommended code by rejetto here (chinking the file every 1MB), but it doesn't work as expected (the download has a never ending loop). This code not only doesn't work, but besides that, it also have missing the text "Downloaded Core-9.0.iso for 13256704 bytes" and the event doesn't get logged on HFS's log.

THIS CODE DOESN'T WORK: (why?)

--- Quote ---<form method='post'>
URL: <input name='url' value="http://distro.ibiblio.org/tinycorelinux/9.x/x86/release/Core-9.0.iso">
<br>Filename: <input name='dest' value="%folder%Core-9.0-BAD.iso">
<br><input type='submit'>
</form>
{.set|url|{.postvar|url.}.}
{.break|if={.not|{.^url.}.}.}
{.set|dest| {.or|{.filename|{.postvar|dest.}.}|{.filename|{.^url.}.}|downloaded.} .}

{.set|from|0.}
{.set|chunk|1000000.}
{.save|{.^dest.}|.}
{.while|chunk|{:
   {.load|{.^url.}|var=data|size={.^chunk.}|from={.^from.}.}
   {.if|{.length|var=data.}
      | {:{.append|{.^dest.}|var=data.}
         {.inc|from|{.length|var=data.}.}
        :}
      | {:{.set|chunk|0.}:}.}
:}|timeout=0.}

--- End quote ---

Could someone point me where is the fault?... :-\

LeoNeeson:
Thank you! I did a deep search on the forum, and I'm ashamed that I did this very same question only FOUR years ago, here... (I've totally forgot that thread. LOL!, I'm getting older). I've also found those rejetto's posts about this, from back 2009: here and here.

I've edited the message above with an updated code.

LeoNeeson:

--- Quote from: danny on May 16, 2018, 09:17:04 AM ---The example has a variable of a larger size than possible.

So, did you try this:  {.set|chunk|4000.}

. . . maybe not optimized, but at least it is possible to have a variable that size.

--- End quote ---
I've tried and it has the same effect (it doesn't work properly). I'm curious to know why the first code works (limited by the RAM size) but not the second one... :-\

LeoNeeson:
@Rejetto/Mars: It would be great if you could check this (just to be sure there is no bug in HFS). After doing several tests, and I've found out that if you use a local file instead of a remote external file (as remote upload url), the first code works fine (loading the file into the RAM), but the second code (based on this post), creates a infinite endless loop archive (which you can't stop unless you kill the hfs.exe process). I guess this is not a normal behavior (perhaps a bug?).

Try yourself, testing the files included the in the attached zip file.
Cheers,
Leo.-

LeoNeeson:
@Rejetto/Mars: It would be great if you could check this (just to be sure there is no bug in HFS). After doing several tests, and I've found out that if you use a local file instead of a remote external file (as remote upload url), the first code works fine (loading the file into the RAM), but the second code (based on this post), creates a infinite endless loop archive (which you can't stop unless you kill the hfs.exe process). I guess this is not a normal behavior (perhaps a bug?).

Try yourself, testing the files included the in the attached zip file.
Cheers,
Leo.-

EDIT: I know there is no point doing a 'Remote upload' of a local file. It was just a way of testing the script code without having to download a file (if it works with a local file, it should work with a remote external file, right?)

Navigation

[0] Message Index

[#] Next page

Go to full version