Monday, January 30, 2012

Re: CakePHP Media Plugin of David Persson and CDN

Add a behavior that has an afterSave and afterDelete that take care of it (or attach those actions to your model).

Here's a helper function to get a list of files based on your filters set (hardcoded for images, but you can adapt it to use whatever "types" you want). Once you have that list of files, you can move them to your CDN or remove them if they were deleted.

function _getFiles(&$model, $id = null) {
if (!$id) {
return array();
}

$result = $model->read(null, $id);

// get all including filtered
$files = array();
$files[] = array(
'path' => $result[$model->alias]['file'],
'url' => MEDIA_TRANSFER_URL . $result[$model->alias]['dirname'] . '/' . $result[$model->alias]['basename']
);
$extension = Mime_Type::guessExtension($files[0]['path']);
$filters = Configure::read('Media.filter.image');
foreach ($filters as $size => $filter) {
if (isset($filter['convert'])) {
$newextension = Mime_Type::guessExtension($filter['convert']);
} else {
$newextension = $extension;
}
$newbasename = str_replace($extension, $newextension, $result[$model->alias]['basename']);
$path = MEDIA_FILTER . $size . DS . $result[$model->alias]['dirname'] . DS . $newbasename;
$url = MEDIA_FILTER_URL . $size . '/' . $result[$model->alias]['dirname'] . '/'. $newbasename;
$files[] = compact('path', 'url');
}

return $files;
}

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: