/**
* Process of uploading file
*
* @param Closure $callback
* @param Validator $validator
* @return array
*/
public function process(Closure $callback = null, Validator $validator = null)
{
$fileId = $this->request->get($this->fileIdAttr);
$files = $this->request->files;
if (!$fileId) {
$file = new File();
$file->user_id = $this->user->id;
$file->addNewFromFiles($files);
$fileId = $file->id;
}
$localFileSystem = $this->localFileSystem;
$remoteFileSystem = $this->remoteFileSystem;
$tempPath = $this->tempPath . '/' . $fileId;
$destPath = $this->destPath . '/' . $fileId;
$tempAbsolutePath = $this->basicPath . $this->tempPath . '/' . $fileId;
!$localFileSystem->has($tempPath) ? $localFileSystem->getAdapter()->createDir($tempAbsolutePath) : '';
if (!$validator) {
$validator = $this->validator;
}
$fileSystem = new FileUploaderSystem();
$pathResolver = new PathResolver($this->basicPath . $this->tempPath . '/' . $fileId);
$filesArray = $this->getFilesInfoArray($files);
$serverArray = $this->request->server->all();
$fileUploader = new FileUpload($filesArray['file'], $serverArray);
$fileUploader->setPathResolver($pathResolver);
$fileUploader->setFileSystem($fileSystem);
$fileUploader->addValidator($validator);
$callbackResult = null;
//callback
$fileUploader->addCallback('completed', function (FileUploadFile $files) use($callback, $fileId, $tempPath, $destPath, $localFileSystem, $remoteFileSystem, &$callbackResult) {
$file = new File($fileId);
$file->path = $destPath;
$file->status = $file::STATUS_UPLOADED;
$file->size = $localFileSystem->get($tempPath . '/' . $files->name)->getSize();
$file->save();
$fileContent = $localFileSystem->read($tempPath . '/' . $files->name);
$remoteFileSystem->write($destPath . '/' . $files->name, $fileContent);
$localFileSystem->delete($tempPath . '/' . $files->name);
$localFileSystem->delete($tempPath);
if ($callback) {
$callbackResult = $callback($file);
}
});
list($fileUploadObject, $headers) = $fileUploader->processAll();
$properties = get_object_vars($fileUploadObject[0]);
// O_O mama mia
/* foreach ($properties as $property=> $value) {
if ($property != 'path') {
$info[$property] = $value;
}
} */
unset($properties['path']);
$result = array('info' => $properties);
if ($result['info']['error'] != 0) {
$file->delete();
}
if ($callbackResult) {
$result = array_merge($result, $callbackResult);
}
$result[$this->fileIdAttr] = $fileId;
$this->lastUploadedResult = $result;
return $result;
}