/** * array with 2 items - file (UploadedFile) and delete (checkbox) * @param $fileDataFromForm * @return int */ public function reverseTransform($fileDataFromForm) { //if file field != file upload field - no need to store 'delete' in serialized file data if (isset($fileDataFromForm['delete']) && !$fileDataFromForm['delete']) { unset($fileDataFromForm['delete']); } if ($this->mapping && isset($fileDataFromForm['delete']) && $fileDataFromForm['delete']) { if ($this->mode == self::MODE_FILEDATA_FIELD) { return null; } //Todo: move to uploaderListener //File may no exists try { $this->fileStorage->removeFile($this->mapping->resolveFileName($fileDataFromForm['fileName'])); } catch (\Exception $e) { } } return isset($fileDataFromForm['file']) ? $fileDataFromForm['file'] : ($this->mode == self::MODE_UPLOAD_FIELD ? null : $fileDataFromForm); }
/** * upload field and file data field are SAME ($obj->file) * @param EventArgs $args * @param PropertyMapping $mapping */ protected function updateSeparateProperties(\Doctrine\Common\EventArgs $args, PropertyMapping $mapping) { $uploadedFile = $mapping->getFileUploadPropertyValue(); $currentFileData = $mapping->getFileDataPropertyValue(); $previousFileData = $this->dataStorage->previusFieldDataIfChanged($mapping->getFileDataPropertyName(), $args); $currentFileName = $previousFileData ? $mapping->resolveFileName($previousFileData['fileName']) : null; //delete current file if ($previousFileData && (is_null($currentFileData) || $uploadedFile && $uploadedFile instanceof \Iphp\FileStoreBundle\File\File && $uploadedFile->isDeleted())) { if ($this->fileStorage->removeFile($currentFileName)) { $mapping->setFileDataPropertyValue(null); } } else { if ($uploadedFile && $uploadedFile instanceof File) { //Old value (file) exists and uploaded new file if ($currentFileName && !$this->fileStorage->isSameFile($uploadedFile, $currentFileName)) { //before upload new file delete old file $this->fileStorage->removeFile($currentFileName); } $fileData = $this->fileStorage->upload($mapping, $uploadedFile); $mapping->setFileDataPropertyValue($fileData); } } }
/** * {@inheritDoc} * File may be \Symfony\Component\HttpFoundation\File\File or \Symfony\Component\HttpFoundation\File\UploadedFile */ public function upload(PropertyMapping $mapping, File $file) { $originalName = $this->getOriginalName($file); $mimeType = $this->getMimeType($file); //transform filename and directory name if namer exists in mapping definition list($fileName, $webPath) = $mapping->prepareFileName($originalName, $this); $fullFileName = $mapping->resolveFileName($fileName); //check if file already placed in needed position if (!$this->isSameFile($file, $fullFileName)) { $fileInfo = pathinfo($fullFileName); if ($file instanceof UploadedFile) { $this->checkDirectory($fileInfo['dirname']); $file->move($fileInfo['dirname'], $fileInfo['basename']); } else { $this->copyFile($file->getPathname(), $fileInfo['dirname'], $fileInfo['basename']); } } $fileData = array('fileName' => $fileName, 'originalName' => $originalName, 'mimeType' => $mimeType, 'size' => filesize($fullFileName), 'path' => $webPath); if (!$fileData['path']) { $fileData['path'] = substr($fullFileName, strlen($this->webDir)); } $ext = substr($originalName, strrpos($originalName, '.') + 1); if ((in_array($fileData['mimeType'], array('image/png', 'image/jpeg', 'image/pjpeg')) || in_array($ext, array('jpeg', 'jpg', 'png'))) && function_exists('getimagesize')) { list($width, $height, $type) = @getimagesize($fullFileName); $fileData = array_merge($fileData, array('width' => $width, 'height' => $height)); } return $fileData; }