/**
* Handles the file upload
*
* @param array $input [description]
*
* @return array Variable response - either pending or file uploaded when all chunks
* are received.
*/
public function handle($input = array())
{
$validator = Validator::make($input, Model::$rules);
$user = User::find($input['user_id']);
if ($validator->fails()) {
throw new Exception("Validation Issues", 500);
}
$filename = time() . $input['flowFilename'];
$input['original_filename'] = $input['flowFilename'];
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$filename = pathinfo($filename, PATHINFO_FILENAME);
if (!isset($input['path'])) {
$input['path'] = $user->id . '/';
}
$storageLocation = storage_path('user-data/' . $input['path']);
if (!is_dir($storageLocation)) {
mkdir($storageLocation, 0755, true);
}
$config = new FlowConfig();
if (!is_dir($storageLocation . 'chunks')) {
mkdir($storageLocation . 'chunks', 0755, true);
}
$config->setTempDir($storageLocation . 'chunks');
$file = new FlowFile($config);
if (isset($_POST['ie-app'])) {
$file->saveChunk();
} else {
if ($file->validateChunk()) {
$file->saveChunk();
} else {
// error, invalid chunk upload request, retry
throw new Exception('Bad request', 400);
}
}
$filename = $this->sanitizeString($filename) . '.' . $extension;
$localPath = $storageLocation . $filename;
if ($file->validateFile() && $file->save($localPath)) {
$input['status'] = 'saved';
$input['size'] = $input['flowTotalSize'];
if (isset($_POST['ie-app'])) {
$input['size'] = filesize($localPath);
} else {
$input['size'] = $input['flowTotalSize'];
}
$input['path'] = $input['path'] . $filename;
$input['filename'] = $filename;
$input['type'] = mime_content_type($localPath);
$file = Model::create($input);
//FIXME should use the transformer
return ['id' => $file->id, 'path' => $file->path, 'links' => $file->links, 'original_filename' => $file->original_filename];
} else {
// This is not a final chunk, continue to upload
return array('pending' => true);
}
}