/** * Update the mapped file for Entity (obj) * * @param \Doctrine\ORM\Event\PreUpdateEventArgs $args */ public function preUpdate(PreUpdateEventArgs $args) { $mappings = $this->mappingFactory->fromEventArgs($args); foreach ($mappings as $mapping) { //Uploaded or setted file $file = $mapping->getFilePropertyValue(); $currentFileData = $args->hasChangedField($mapping->getPropertyName()) ? $args->getOldValue($mapping->getPropertyName()) : null; //If no new file if (is_null($file) || !$file instanceof File) { //Preserve old fileData if current file exists, else null if ($currentFileData) { $mapping->setFileDataPropertyValue($this->fileStorage->checkFileExists($currentFileData) ? $currentFileData : null); } } else { if ($file instanceof \Iphp\FileStoreBundle\File\File && $file->isDeleted()) { if ($this->fileStorage->removeFile($currentFileData)) { $mapping->setFileDataPropertyValue(null); } } else { if ($currentFileData && !$this->fileStorage->isSameFile($file, $currentFileData)) { $this->fileStorage->removeFile($currentFileData); } $fileData = $this->fileStorage->upload($mapping, $file); $mapping->setFileDataPropertyValue($fileData); } } } $this->dataStorage->recomputeChangeSet($args); }
/** * 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); } } }