modxcms/revolution — pull request #11371 by exside (2014-05-14 19:08:27+00:00)
Title: Enhancement: Added download() method to modFileHandler

This is a modified and properly branched version of this PR: https://github.com/modxcms/revolution/pull/680

As of @MarkH s comment from that PR
> Is this perhaps more appropriate/useful when added to media sources, so it's not limited to files? Speaking of which, I think it's already possible to download files from the Files tab, so this has to be implemented somewhere already..?

I did a bit of research and found a lot of repeating code which mostly does, what the new method download() does, e.g. setting headers etc. As already commented in the old PR: modMediaSource btw. modFileMediaSource already make use of the modFileHandler class, so it's easy to access this through the mediaSource as can be seen in the modified download.class.php processor (core/processors/browser/).

Together with the new method I also improved (hopefully) the two existing methods getSize() and getContents() to be able to return something, before a file really exists in the filesystem. This is helpful when offering a download from data that is created on the fly but doesn't have to be saved somewhere in the filesystem, like a csv/json export etc. Like this we can set the file content via setContent($content) but never have to really save/create a physical file and can do:

```
$modx->getService('file', 'modFileHandler');

$fileobj = $modx->file->make('path/to/the/file.ext'); // in this case this just doesn't matter, this would be the path where the file was created if we do $fileobj->save()/$fileobj->create() though

$fileobj->setContent($content);
$fileobj->download();
```

the changed getSize() method now also allows to return the size of temporarily set content which is not possible by filesize() alone.