Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
smd_remote_file: Manage remote URL downloads via TXP's Files tab
After a brief techie discussion on ways in which I could achieve integration between remote URLs and local files, the omnipotent Ruud pointed me in a particular direction with a cunning and neat hack.
Never one to let an opportunity slip by for making someone’s life easier I took the idea, ran with it, modified it and came up with this plugin.
In a nutshell, you can upload large files to third party file sharing sites such as fileden, tell TextPattern’s Files tab about them (via a new field in the admin interface) and then manage their properties as if they were local files in your /files
directory. This means you can use the standard file_download_*
TXP tags/forms for filtering and displaying files. Uploading more than one version of the same file to multiple providers allows for random load balancing of content, spreading bandwidth usage.
You can use <txp:smd_file_download_link>
instead of the native <txp:file_download_link>
to take advantage of a couple of minor enhancements.
One tiny caveat is that the download counters won’t work for remote files at the moment. There’s details on that in the help and if anyone can offer suggestions on how I can implement it, I’m all eyes. I’ve had several semi-successful trials but have yet to find a good solution that doesn’t cripple other functionality of the plugin. Download counters work in v0.2 and up.
An extra tag smd_file_download_image
allows you to display an image within your files form, based on the filename of the download. Very handy if you’re offering videos or mp3s for download because the images can be relevant stills or artwork.
Take the plugin away for a spin, report any goodness, badness or improvement ideas here and, above all, happy downloading.
Revision history
————————
- 12 Nov 07 | 0.1 | Initial release
- 04 Dec 07 | 0.2 | Added download counter and some status messages
- 04 Dec 07 | 0.3 | Removed .link when using
smd_file_download_link
. Addedshow_link
attribute. Improved error handling code. Download count only increases if file sizes match - 05 Dec 07 | 0.31 | File is checked for error condition prior to download in line with core changeset r2720 (thanks Mary)
- 12 Dec 07 | 0.32 | Enhanced ifmissing to include image support. Changed the ?file attribute to ?ref (it’s a better name)
- 03 Feb 08 | 0.4 | For TXP 4.0.6+ only ; improved upload/replace URL ; reduced timeout
- 02 Apr 08 | 0.41 | gzipped | Added smd_file_download_name (thanks aswihart)
- 09 Apr 08 | 0.42 | gzipped | Fixed minor edge case bug in smd_file_download_link
- 10 Jun 08 | 0.43 | gzipped | Fixed spurious characters in the single tag version of smd_file_download_link (thanks iblastoff)
Last edited by Bloke (2009-04-11 21:20:12)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: smd_remote_file: Manage remote URL downloads via TXP's Files tab
Hi Stef… Thanks… Looks like this will be of use.
Last edited by colak (2007-11-13 07:58:18)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Offline
Re: smd_remote_file: Manage remote URL downloads via TXP's Files tab
this is a great idea…
Offline
#5 2007-11-15 06:44:33
- Niconemo
- Member
- From: Rhône-Alpes, France
- Registered: 2005-04-18
- Posts: 557
Re: smd_remote_file: Manage remote URL downloads via TXP's Files tab
Yum ! Looks very interesting ! Thank you.
Nico
Offline
Re: smd_remote_file: Manage remote URL downloads via TXP's Files tab
It’s great Stef. Tks lot.
Best regards,
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: smd_remote_file: Manage remote URL downloads via TXP's Files tab
This is one of the most useful plugins to come along since I have been using Textpattern, much appreciated. I have been wanting to use a plugin like this with Amazon S3 for a while now. I have donated for your effort, Stef. Please keep trying to get the download counter to work!
Offline
Re: smd_remote_file: Manage remote URL downloads via TXP's Files tab
aswihart wrote:
I have donated for your effort, Stef. Please keep trying to get the download counter to work!
Many thanks Andrew. And believe me, I’m trying: it’s driving me mad! There has to be a way of intercepting the call to download_file so I can adjust the database and credit the download. It’s probably staring me in the face, as usual ;-) Will keep you posted.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: smd_remote_file: Manage remote URL downloads via TXP's Files tab
Bloke wrote:
There has to be a way of intercepting the call to download_file so I can adjust the database and credit the download.
There’s an event hook at the start of a download.
Offline
Re: smd_remote_file: Manage remote URL downloads via TXP's Files tab
wet wrote:
There’s an event hook at the start of a download
Thanks wet, yeah, I saw that and tried to use it without much success before I released the plugin. I managed to hook into the event alright, but because the page is essentially “new” at that point, pretty much the only global thing I could find available was $pretext
. No matter what I tried I couldn’t get my filename variable or value of the choose
parameter from the previous page into the global scope so I could use it in the callback. I think the only avenue might be a cookie :-(
What intrigued me was that, two lines further on from that in publish.php is the line:
$fullpath = build_file_path($file_base_path,$filename);
I cannot see how or where $filename comes from (it just “appears” and has the correct filename in it) so I was trying to figure out if I could somehow “inject” the chosen remote filename into wherever that $filename variable magically appears. My initial idea was to update the database in the callback and then replace that variable with my ‘remote’ filename. But of course the download then fails when it returns from the callback (‘file not found’).
Having been unsuccessful with pretty much every trick I had, I gave up and released the plugin in the hope someone far cleverer could point me in the right direction for v0.2.
The fallback is this, if you think it would work:
- Somehow pass to the global scope the result of the
choose
option from smd_file_download. Via a cookie if I must, but any other way would be preferable - Have all links function exactly as normal with
site_url/file_download/id
- The callback looks at $filename and if it’s a .link file, opens it, reads the contents, grabs the
choose
variable from wherever it’s hiding, selects either a random or specific file from the list depending on what the smd_download_file tag said, updates the download_count and serves the file, then exits, i.e. never reaching the “official” download part of publish.php for remote files
Annoyingly it’d mean duplicating a lot of code from lines 130-178 of publish.php in my callback, but I can’t see another way of “switching” filenames and letting the callback return so the rest of the download process can begin. I was trying to reuse the code already in TXP to serve the download to the browser.
[ Incidentally, the advantage of handling everything in the callback is that smd_file_download becomes optional. If people don’t care about the random option, they can use the built-in file_download tag instead. The callback would still be called, any non-.link file would be served as normal (the callback would do nothing) but any .link file would be processed. If $choose is not set, the callback would simply pick the 1st entry in the file. ]
So, should I let the callback do everything and duplicate the core code, or do you think there’s a better way of letting the callback “do something” that the remaining part of publish.php can work with?
Thanks in advance for any input.
Last edited by Bloke (2007-11-28 09:09:42)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: smd_remote_file: Manage remote URL downloads via TXP's Files tab
Bloke wrote:
I managed to hook into the event alright, but because the page is essentially “new” at that point, pretty much the only global thing I could find available was
$pretext
.
That should suffice.
What intrigued me was that, two lines further on from that in publish.php is the line:
$fullpath = build_file_path($file_base_path,$filename);
I cannot see how or where $filename comes from
(it just “appears” and has the correct filename in it)
At least this is what I think, without a debugger at hand right now…
Offline
Re: smd_remote_file: Manage remote URL downloads via TXP's Files tab
wet wrote:
pretext() builds $pretext
Holy cow, ok. I see that now. So pretext() sets the extra array var(s) if dealing with a file download. Got it, thanks.
But still, the only way I can see of passing it my variables is to pass them as a query string, then reading $pretext['qs']
(or as a cookie and use cs()
). Both a tad messy, and I still have the problem of duplicating core code in the callback. Hmmmm.
Thanks for pointing that out though wet! I learn something new every day about the way this core code ticks.
EDIT: I never spotted the call to pretext() before, because the function is defined as preText(...)
but is referenced as pretext()
and my text editor’s Find is set to match case… d’oh!
Last edited by Bloke (2007-11-28 09:48:20)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline