MediaView is a bleeding bastard

Posted in CakePHP on 08.05.2008.

Due to some issues with my downloads, I've decided to try MediaView. Too bad for me.

Yesterday, I found out that I have some trouble with my downloads. Lucky for me, I got some great help. NOT.

Anyway, I've decided to try MediaView, and oh boy.. MediaView is a royal pain in the ass.

First, all I got was a blank page. I asked myself WTH, and then decided to look into the source of MediaView itself. Let me tell you, it's not something you would expect.

First of all, if you set your file size in controller, that size will be appended to the file name as a string. Wait, what? Let me elaborate.

if ($size) {
    $id = $id . "_$size";
}

Well, that's interesting, to say the least. Why the hell would I want my file to have a file name which end in a byte size string??

Moving on.. If you specify a $path..you need to specify a path relative to the APP root. This is not an option. How amusing. Still, this makes an amazing combo with the fact that $id is actually a file name!

if (is_null($name)) {
    $name = $id;
}

And now for the fun part. The main if-block in the render method:

if (file_exists($path)
    && isset($extension)
    && array_key_exists($extension, $this->mimeType)
    && connection_status() == 0) {

This really is the most amazing bit. MediaView->mimeType contains a list of extensions and their mime-types. If your file extension is not there, what do you get? A blank page! No error message. No debug message. A blank page.

This makes for a fun developing experience, since even the most ridiculous extensions like .phps or .rar are not listed in the mimeType array. And this makes me wonder, why is there no fallback mechanism? If my file type is not found in mimeType array, why not see if function_exists('mime_content_type') and use that? In the end, if that fails too, why not just put "application/octet-stream" and let the developer deal with it.

Having all this in mind, I wouldn't say that MediaView is something we could call production-ready. I don't know. I might be getting it all wrong, but if I need to hack the file and add my own mime types in it, it's a long way from being good as the rest of the cake.

You can see the whole file here: MediaView source

In the end, this is how I got it working..

$fakeFile = new File($download['Download']['display_file_name']);

$path = FILES_REL; // path relative to APP
$id = $download['Download']['real_file_name'];
$name = $fakeFile->name();
$extension = $fakeFile->ext();
$download = true;

$this->view = 'media';
Configure::write('debug', 0);
$this->set(compact('path', 'id', 'name', 'extension', 'download'));

Well, good luck with it!

Article comments — View · Add


Page 1 of 1

Daniel Hofstetter :: 08.05.2008 10:36:52
I never used the MediaView yet, but depending on your requirements it may be easier to put the files for downloading into a subfolder of app/webroot
lecterror :: 09.05.2008 02:06:20
Well, it really depends on requirements, I wanted to count the number of downloads so webroot was out of the question for me.

But if putting files for download is the *only* thing you want, then you're absolutely right.

Thanks for commenting!