Author Topic: Filename/disposition modification in script - can't find where to implement it  (Read 9703 times)

0 Members and 1 Guest are viewing this topic.

Offline Falcon4

  • Occasional poster
  • *
  • Posts: 13
    • View Profile
    • falconfour.com
Sooo... I've been using HFS for some years now... and I still haven't really dug into how to work with scripting.

I have a simple function in mind that I would love HFS to take care of: providing an alternative return ("save as", or "content-disposition") file name, different from the URL filename.

HFS has been providing the "engine" behind my hosting site hostfile.org, and it does the job pretty well. I love that I can monitor the current requests and transfers with infinitely more granularity and control than the Apache+PHP+MySQL backend that the site runs on. It actually inspired me to bring the site back online for new registrations and uploads again. Cool.

But the site is now missing one function that was provided by its old (and very buggy) PHP-based file-server. That old system would chunk out the file's data inside the PHP script, generating and sending all the file-related headers (MIME, content-length, content-range I/O, modified, etc... I had to generate it all in the script). But it did one thing right: gave me control over choosing if the browser will save (content-disposition: attachment) or open (content-disposition: inline) the file... and what the "Save As" filename was (content-disposition: filename=xyz123.abc). That way I can actually serve the original filename back to requesting clients... for example, someone uploads "My Great Video (2-11-2011).mkv", the script strips and stores the filename as "mygreatvideo2112011.mkv" and provides that as the file URL, but when someone goes to download the file, it'll be downloaded as the original filename once again.

So what I wanted to do was pretty simple: pass the "original filename" and attach/inline mode into HFS using a query string, and HFS will serve the file accordingly.

Since I could find absolutely zero information whatsoever online (probably thanks to Google's search relevance meltdown), I have all the commands I need, but that page doesn't even touch on where to actually utilize any of those commands. HFS just serves files, so how do I create an "interpreted script" file? Heck, for the security of my site, I'm a little concerned that would even be possible... so how on earth do I even use those script commands? I don't have to write my own template, do I? :/

Offline rejetto

  • Administrator
  • Tireless poster
  • *
  • Posts: 12949
    • View Profile
scripting is available both in pages generation (that is, the template)
and in events (same syntax, but sections don't represent pieces of the page but events/happenings).
Consider the other pages in the wiki related to scripting. You need this
http://www.rejetto.com/wiki/index.php/HFS:_Event_scripts#Naming_events
and event [download name]
and also [upload name] if you want to change after an upload.

Offline Falcon4

  • Occasional poster
  • *
  • Posts: 13
    • View Profile
    • falconfour.com
Woohoo! I set up a test HFS on my main PC, with a simple test structure of virtual/real files/folders... then I made "hfs.events". I added the "[connect] {.disconnect.}" tag to the file, and BAM, it worked perfectly. Then I changed "[connect] to [request]", and "{.disconnect.}" to "{.redirect|http://google.com.}", and BAM, I got redirected to Google.

I think I'm getting the hang of this! :)

Yeah, the reason I didn't find those different wiki pages, is because they're not referenced together... the Wiki structure is kinda deficient in that area - for example, on the PHP documentation site, you can see the "topic tree" in a side navigation bar, so I can just go "up" and learn more about "scripting" itself, if I was reading about "scripting commands"... or I can go to the next topic and read about "scripting events"... but on the Wiki, I have "tunnel vision", there are no links to other subjects within the same area. Really makes it kinda hard to learn without editing the URL and hoping I land on a useful page :/

Thanks for the info! Now I can finally get the load off my DSL connection and moved over to the secondary... :D

edit: speaking of wiki, currently having difficulty locating the "what variables are available in the scripting" page, e.g. to locate the command for "give me the requested filename". Yeah, the wiki is really, really, really a PITA to navigate without navigation links...

edit edit: Yeah, I'm lost. There are no links anywhere to any of the scripting commands or anything... see? http://www.rejetto.com/wiki/index.php/HFS_Documentation_%28English%29 - there's nothing about scripting, and no back-references from the scripting pages to "see more in this category"... so I'm lost again. Can you please, please, please post some list of Wiki articles about scripting? :(
edit edit edit: OK, found it: http://www.rejetto.com/wiki/index.php/HFS:_Template_symbols - but I had to run a "search" for "symbols" after I read that the term "symbols" is what I'm looking for instead of "variables". But if there was just a link to "Macros", "Symbols", "Events", etc., I would've just clicked on one of 'em :P
« Last Edit: March 21, 2011, 01:28:34 AM by Falcon4 »

Offline r][m

  • Tireless poster
  • ****
  • Posts: 347
    • View Profile
Searching the forum I find:
http://www.rejetto.com/forum/index.php/topic,1939.msg1032472.html#msg1032472
But I agree, the wiki is difficult to find things in.

Offline rejetto

  • Administrator
  • Tireless poster
  • *
  • Posts: 12949
    • View Profile
i renamed "Template macros" in "Template scripting (macros)"
and made some more little changes to help it become clearer.
The wiki can be widely improved, and good thing is that everyone can help being it a wiki. ;-)
Personally i use the search field a lot when moving in the wiki, just because we lack a useful side tree, like you say.

About symbols, I understand why most people would call them "variables", but i decided (many years ago) to call them symbols because in programming a variable is something you can change.
« Last Edit: March 21, 2011, 12:48:29 PM by rejetto »

Offline Falcon4

  • Occasional poster
  • *
  • Posts: 13
    • View Profile
    • falconfour.com
Ah, I see... I'll try and help out there however I can with the navigation, maybe I can unify all the various pages with a tree-level navigation at the top of each article. Still haven't been able to get the simple "take this and move it there" system working, though... try as I may, I simply cannot find a way to implement "debugging" in the event scripts, such that I can get HFS to put anything on the screen other than what was requested. That is, if I write:

[request]
i like %item-name%
{.notify|do you like %item-name%?.}

... all I get is a balloon popup saying, literally, "do you like %item-name%?". If I'm lucky, that is. I don't get any "i like" output on the browser, even if the file exists, it's like it's ignoring the non-scripting-commands completely. And even if it is by design that event-statements don't output any extra data, shouldn't there be some kind of "echo" function/macro? Also tried the other "{.if ... hey... .}" thing posted in the original "events" thread, and I just couldn't get that to work with my "file.txt" either... :-/
[download]
{.if|{.is substring|%item-name%|file.}| {:
  {.notify|got %item-name%.}
:}.}

I feel kinda dumb here... I mean, I look around and it seems like I'm the only guy that just doesn't "get it". Or am I just the only one that's actually tried to get a start from scratch?  :-\ Maybe I could understand this easier if I had some examples of working event scripts to go on... that's one thing the PHP docs is really strong with: "here's an example to show what you can do with this". Wiki docs are almost completely void of actual syntax-valid examples (most aren't even properly written - e.g. "after the list | A" - OK, so how would I write that as an actual macro? I'm guessing "{.after the list|parameters.}"?)... I can't be the only one totally lost on this stuff, come on, I'm generally a pretty smart cookie :D
« Last Edit: March 21, 2011, 03:59:55 PM by Falcon4 »

Offline rejetto

  • Administrator
  • Tireless poster
  • *
  • Posts: 12949
    • View Profile
Ah, I see... I'll try and help out there however I can with the navigation, maybe I can unify all the various pages with a tree-level navigation at the top of each article.

thanks for the good will, but i would go for a solution like this unless it would be automated, so that it stays up to date with the content.

Quote
Still haven't been able to get the simple "take this and move it there" system working, though...


don't know what you are referring to

Quote
try as I may, I simply cannot find a way to implement "debugging" in the event scripts,


for debugging, did you try the "debug" menu? :)
i usually enable the macros.log

Quote
[request]
i like %item-name%
{.notify|do you like %item-name%?.}

... all I get is a balloon popup saying, literally, "do you like %item-name%?".

then %item-name% is not available in the [request] event

Quote
If I'm lucky, that is. I don't get any "i like" output on the browser, even if the file exists
,

because you are in an event.
the "raw" output of the event is discarded, unless differently stated.

Quote
shouldn't there be some kind of "echo" function/macro?


to output WHERE ? to get to the log use {.add to log.}
to save in a file try {.append.}

Offline Falcon4

  • Occasional poster
  • *
  • Posts: 13
    • View Profile
    • falconfour.com
Well, the "take this and move it there" I want to do is:
incoming: server:280/any/path/to/file.txt
redirects to: server:281/any/path/to/file.txt

Figure it's pretty simple, but without being able to read "%item-name%" from [request], how can this be done? What good is [request] without being able to read the... um... request, the %item-name%? :-\

As for the event output, I saw the "debug" menu but it wasn't very useful to me if I can't test it against... you know, actual requests and data... it lets me write a temporary script and read its output but that doesn't do me any good if I'm testing in a non-functional context. That is, how can I test if a URL is being parsed and processed properly, and the browser is receiving the right headers, if I have neither a browser nor a request to work with?

"to output WHERE?" - well, that's easy! Dump it into the response! That's what makes sense to me... if there's something in [request] being output somewhere, it should be output to the client... say, for example, I want to add some kind of page header to every request in an HTML folder... I could add that to the [request] event. Or if I'm debugging, like here, I could use the "non-functions" to dump some debug info out to the browser, like the return value of some macros I'm experimenting with and don't know how they behave. Since I can't test live requests with the "debug" window, that'd be the only way I could do it... unless of course I use {.add to log.} (I'm guessing that's the actual syntax?). But then I'd have to go back to the HFS window to view the response instead of just looking at the browser window I've already got open ;)

Offline rejetto

  • Administrator
  • Tireless poster
  • *
  • Posts: 12949
    • View Profile
Figure it's pretty simple, but without being able to read "%item-name%" from [request], how can this be done? What good is [request] without being able to read the... um... request, the %item-name%? :-\

try  {.replace|:280|:281|%url%.}

Quote
As for the event output, I saw the "debug" menu but it wasn't very useful to me if I can't test it against... you know, actual requests and data... it lets me write a temporary script and read its output but that doesn't do me any good if I'm testing in a non-functional context. That is, how can I test if a URL is being parsed and processed properly, and the browser is receiving the right headers, if I have neither a browser nor a request to work with?

you are describing "run script", while i pointed you to "enable macros.log"

Quote
"to output WHERE?" - well, that's easy! Dump it into the response! That's what makes sense to me...


it's a way, but not so obvious: if you are actually serving a page, or an image, you would get a mess merging page and debug data.
maybe {.disconnection reason.} can do for you.

Quote
if there's something in [request] being output somewhere, it should be output to the client...

i meant to add a command for such a purpose, that will skip the template

Quote
say, for example, I want to add some kind of page header to every request in an HTML folder... I could add that to the [request] event.

all pages' output is supposed to stay in the template.
your case would go in section [special:begin]

Quote
Or if I'm debugging, like here, I could use the "non-functions" to dump some debug info out to the browser, like the return value of some macros I'm experimenting with and don't know how they behave.


maybe we should output to the browser console, like firebug extension. That would be a great option! inside the browser but not messing

Offline Falcon4

  • Occasional poster
  • *
  • Posts: 13
    • View Profile
    • falconfour.com
try  {.replace|:280|:281|%url%.}
Ah-HAH! I knew these wiki pages were lying to me... %url% isn't listed in the HFS symbols page: http://www.rejetto.com/wiki/index.php/HFS:_Template_symbols - I was looking for that, too! Sure enough, %url% works fine... I tried "{.notify|got %url%.}" on "[request]" and it now pops up the path just like I was looking for... bah, documentation. :P

it's a way, but not so obvious: if you are actually serving a page, or an image, you would get a mess merging page and debug data.
Yeah, but it's the way it's always done with PHP and other web-languages... even HTML itself is a mess of data and commands. I guess it's fair enough, though... any output could very well break the file server, and the whole point of HFS is to be a file-server, but it should have some way to indicate to the admin that "hey dumdum, something in your script is trying to tell you something", maybe in the log view window ;)

maybe {.disconnection reason.} can do for you.
Actually, {.notify|blahblah.} has been coming most in handy, as it lets me see the value that the HFS-macros are seeing within the program, so that's probably what I'll end up using in place of echo and friends. :)

all pages' output is supposed to stay in the template.
your case would go in section [special:begin]
Aye, but I haven't even begun dabbing in templates yet... since my use of HFS lies solely in HFS serving direct links to files, I haven't had a purpose to mess with templates, nor do I know where they would come into play outside of a HFS-generated page (not a file)... but I guess they could start to be useful, the more I look into how it works.
 
maybe we should output to the browser console, like firebug extension. That would be a great option! inside the browser but not messing
That could work if you can find a way to output it... never used firedebug, but I'd think it would use HTTP-headers?
« Last Edit: March 22, 2011, 04:13:02 PM by Falcon4 »

Offline rejetto

  • Administrator
  • Tireless poster
  • *
  • Posts: 12949
    • View Profile
Quote
Ah-HAH! I knew these wiki pages were lying to me... %url% isn't listed in the HFS symbols page: http://www.rejetto.com/wiki/index.php/HFS:_Template_symbols - I was looking for that, too!

ugh, sorry, i will document it asap.

Quote
Sure enough, %url% works fine... I tried "{.notify|got %url%.}" on "[request]" and it now pops up the path just like I was looking for... bah, documentation.

well, iiirc that's not exactly what you needed, but i think i found out
{.substring||:|%host%.}:280%url%

Quote
yeah, but it's the way it's always done with PHP and other web-languages...

and that's how it is when you use the scripting inside the template.
But here we are talking about events. Events are not bound to browser activity.

Quote
"hey dumdum, something in your script is trying to tell you something", maybe in the log view window

you already have several ways to do that.
{.add to log.}
{.notify.}
{.dialog.}
and the endless ways given by {.save.} and {.exec.}
what's wrong with them?
anyway, i'm still talking about generic events.
for browser activity i hope to get a macro to write to the browser's console. It's just to make it handier, i think it's already possible through {.add header.}

Quote
That could work if you can find a way to output it... never used firedebug, but I'd think it would use HTTP-headers?

i'm quite sure it does

Offline Falcon4

  • Occasional poster
  • *
  • Posts: 13
    • View Profile
    • falconfour.com
Sorry for the delayed reply... after I got it working I kinda just let it fall off the radar.

Thanks, first and foremost, for your help with this! I certainly don't mean to act like "give give give, fix fix fix, this is broken, omg omg"... I just forget to emphasize the things that are working - that is, everything but what I've mentioned :) The wiki is helpful in letting me locate the macros I need to use, and explaining how to use them (even if they're not each written in fully proper syntax, I can figure it out most of the time).

As a result of that, plus debug logging - which I put to some use in realizing that oh-crap, my PHP training had me using "{.if param|param|param.}", instead of "{.if|param|param|param.}" - I now have a working "sorting-and-redirection" event!

Behold, my first "hello world" script! :D

[request]
{.if|{.substring|F4UBCD||%url%.}|{:
{.redirect|http://server.domain.com:281/F4UBCD45.}:}.}
{.if|{.substring|data||%url%.}|{:
{.redirect|http://domain.com/viewfile.php?file={.cut|7||%url%.}.}:}.}

Yeah, I realize that first example isn't functionally proper, but when I did a full file-redirect to the new URL, the clients (BitTorrent "web seeds") were botching the URL, as I guess uTorrent has a broken redirect-handler... so when they were redirected from server.xyz/F4UBCD45/path/to/file.ext, to othersite.abc/F4UBCD45/path/to/file.ext (spent a while debugging this with tinyget to read the returned headers), they would actually request the non-existent file from HFS: othersite.abc/F4UBCD45/path/to/file.ext/path/to/file.ext. Seems that it uses the redirect as the new "seed root" and appends the file name no matter what... and since I couldn't find a way to filter against User-Agent (BtWebClient/xxyy being the trouble one), and since it's not a "critical" file to be served (merely a convenience), I just broke the redirect as such.

The second filter is an odd one... for some reason, I seem to have users that are hard-linking to the redirected file path, outside the main Apache file-handler (which is now configured to use the port 281 server). So instead of just silently redirecting these trouble files, I figure I'd direct a little more traffic through the site instead... so if it catches a URL using the old data path, it'd strip off the URL and send them to the "viewfile" page that provides a proper link through the Apache redirector. Strange that people would do that, since hits aren't logged outside the PHP redirection script, and if the hits aren't logged, the file is deleted for inactivity. Stupid people. :D
« Last Edit: March 25, 2011, 12:27:54 PM by Falcon4 »

Offline rejetto

  • Administrator
  • Tireless poster
  • *
  • Posts: 12949
    • View Profile
you can filter by user-agent
try {.header|user-agent.}