public function handleFileUpload($options = array())
{
if (!isset($options['folder'])) {
throw new \Exception("You must pass the 'folder' option to distinguish this set of files from others");
}
$options = array_merge($this->options, $options);
$allowedExtensions = $options['allowed_extensions'];
// Build a regular expression like /(\.gif|\.jpg|\.jpeg|\.png)$/i
$allowedExtensionsRegex = '/(' . implode('|', array_map(function ($extension) {
return '\\.' . $extension;
}, $allowedExtensions)) . ')$/i';
$sizes = isset($options['sizes']) && is_array($options['sizes']) ? $options['sizes'] : array();
$filePath = $options['file_base_path'] . '/' . $options['folder'];
$webPath = $options['web_base_path'] . '/' . $options['folder'];
foreach ($sizes as &$size) {
$size['upload_dir'] = $filePath . '/' . $size['folder'] . '/';
$size['upload_url'] = $webPath . '/' . $size['folder'] . '/';
}
$originals = $options['originals'];
$uploadDir = $filePath . '/' . $originals['folder'] . '/';
foreach ($sizes as &$size) {
@mkdir($size['upload_dir'], 0777, true);
}
@mkdir($uploadDir, 0777, true);
$upload_handler = new \PunkAve\FileUploaderBundle\BlueImp\UploadHandler(array('upload_dir' => $uploadDir, 'upload_url' => $webPath . '/' . $originals['folder'] . '/', 'script_url' => $options['request']->getUri(), 'image_versions' => $sizes, 'accept_file_types' => $allowedExtensionsRegex, 'max_number_of_files' => $options['max_number_of_files'], 'max_file_size' => $options['max_file_size']));
// From https://github.com/blueimp/jQuery-File-Upload/blob/master/server/php/index.php
// There's lots of REST fanciness here to support different upload methods, so we're
// keeping the blueimp implementation which goes straight to the PHP standard library.
// TODO: would be nice to port that code fully to Symfonyspeak.
header('Pragma: no-cache');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Content-Disposition: inline; filename="files.json"');
header('X-Content-Type-Options: nosniff');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: OPTIONS, HEAD, GET, POST, PUT, DELETE');
header('Access-Control-Allow-Headers: X-File-Name, X-File-Type, X-File-Size');
switch ($_SERVER['REQUEST_METHOD']) {
case 'OPTIONS':
break;
case 'HEAD':
case 'GET':
$upload_handler->get();
break;
case 'POST':
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
$upload_handler->delete();
} else {
$upload_handler->post();
}
break;
case 'DELETE':
$upload_handler->delete();
break;
default:
header('HTTP/1.1 405 Method Not Allowed');
}
// Without this Symfony will try to respond; the BlueImp upload handler class already did,
// so it's time to hush up
exit(0);
}