Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-11-12 22:05:57

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,402
Website GitHub

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.

Download smd_remote_file

Revision history
————————

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

#2 2007-11-13 07:58:05

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,058
Website GitHub Mastodon Twitter

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

#3 2007-11-13 13:14:01

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: smd_remote_file: Manage remote URL downloads via TXP's Files tab

Nice work :) Filefront and Textpattern here we go, to the stars :)

Cheers!

Offline

#4 2007-11-14 07:10:52

pieman
Member
From: Bristol, UK
Registered: 2005-09-22
Posts: 491
Website

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

#6 2007-11-15 07:21:20

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,626
GitHub Twitter

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

#7 2007-11-27 21:05:09

aswihart
Member
From: Pittsburgh, PA
Registered: 2006-07-22
Posts: 345
Website

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

#8 2007-11-27 21:17:36

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,402
Website GitHub

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

#9 2007-11-28 06:00:46

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,330
Website Mastodon

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

#10 2007-11-28 09:07:57

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,402
Website GitHub

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:

  1. 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
  2. Have all links function exactly as normal with site_url/file_download/id
  3. 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

#11 2007-11-28 09:14:33

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,330
Website Mastodon

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

pretext() builds $pretext.

(it just “appears” and has the correct filename in it)

Oh, the wonders of PHP ;-)

At least this is what I think, without a debugger at hand right now…

Offline

#12 2007-11-28 09:25:05

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,402
Website GitHub

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

Board footer

Powered by FluxBB