Ejemplo n.º 1
0
 /**
  * @throws \Exception
  */
 protected function update()
 {
     if (!$this->getFilename() && $this->getId() != 1) {
         $this->setFilename("---no-valid-filename---" . $this->getId());
         throw new \Exception("Asset requires filename, generated filename automatically");
     }
     // set date
     $this->setModificationDate(time());
     if (!$this->getCreationDate()) {
         $this->setCreationDate(time());
     }
     // create foldertree
     $destinationPath = $this->getFileSystemPath();
     $dirPath = dirname($destinationPath);
     if (!is_dir($dirPath)) {
         if (!File::mkdir($dirPath)) {
             throw new \Exception("Unable to create directory: " . $dirPath . " for asset :" . $this->getId());
         }
     }
     $typeChanged = false;
     if ($this->getType() != "folder") {
         if ($this->getDataChanged()) {
             $src = $this->getStream();
             $streamMeta = stream_get_meta_data($src);
             if ($destinationPath != $streamMeta["uri"]) {
                 $dest = fopen($destinationPath, "w+");
                 if ($dest) {
                     stream_copy_to_stream($src, $dest);
                     if (!fclose($dest)) {
                         throw new \Exception("Unable to close file handle " . $destinationPath . " for asset " . $this->getId());
                     }
                 } else {
                     throw new \Exception("Unable to open file: " . $destinationPath . " for asset " . $this->getId());
                 }
             }
             $this->stream = null;
             // set stream to null, so that the source stream isn't used anymore after saving
             @chmod($destinationPath, File::getDefaultMode());
             // check file exists
             if (!is_file($destinationPath)) {
                 throw new \Exception("couldn't create new asset, file " . $destinationPath . " doesn't exist");
             }
             // set mime type
             $mimetype = Mime::detect($this->getFileSystemPath());
             $this->setMimetype($mimetype);
             // set type
             $type = self::getTypeFromMimeMapping($mimetype, $this->getFilename());
             if ($type != $this->getType()) {
                 $this->setType($type);
                 $typeChanged = true;
             }
         }
         // scheduled tasks are saved in $this->saveVersion();
     }
     // save properties
     $this->getProperties();
     $this->getResource()->deleteAllProperties();
     if (is_array($this->getProperties()) and count($this->getProperties()) > 0) {
         foreach ($this->getProperties() as $property) {
             if (!$property->getInherited()) {
                 $property->setResource(null);
                 $property->setCid($this->getId());
                 $property->setCtype("asset");
                 $property->setCpath($this->getPath() . $this->getKey());
                 $property->save();
             }
         }
     }
     // save dependencies
     $d = $this->getDependencies();
     $d->clean();
     foreach ($this->resolveDependencies() as $requirement) {
         if ($requirement["id"] == $this->getId() && $requirement["type"] == "asset") {
             // dont't add a reference to yourself
             continue;
         } else {
             $d->addRequirement($requirement["id"], $requirement["type"]);
         }
     }
     $d->save();
     $this->getResource()->update();
     //set object to registry
     \Zend_Registry::set("asset_" . $this->getId(), $this);
     if (get_class($this) == "Asset" || $typeChanged) {
         // get concrete type of asset
         // this is important because at the time of creating an asset it's not clear which type (resp. class) it will have
         // the type (image, document, ...) depends on the mime-type
         \Zend_Registry::set("asset_" . $this->getId(), null);
         $asset = self::getById($this->getId());
         \Zend_Registry::set("asset_" . $this->getId(), $asset);
     }
     // lastly create a new version if necessary
     // this has to be after the registry update and the DB update, otherwise this would cause problem in the
     // $this->__wakeUp() method which is called by $version->save(); (path correction for version restore)
     if ($this->getType() != "folder") {
         $this->saveVersion(false, false);
     }
     $this->closeStream();
 }
Ejemplo n.º 2
0
 /**
  * @throws \Exception
  */
 protected function update()
 {
     // set date
     $this->setModificationDate(time());
     if (!$this->getCreationDate()) {
         $this->setCreationDate(time());
     }
     // create foldertree
     // use current file name in order to prevent problems when filename has changed
     // (otherwise binary data would be overwritten with old binary data with rename() in save method)
     $destinationPathRelative = $this->getDao()->getCurrentFullPath();
     if (!$destinationPathRelative) {
         // this is happen during a restore from the recycle bin
         $destinationPathRelative = $this->getRealFullPath();
     }
     $destinationPath = PIMCORE_ASSET_DIRECTORY . $destinationPathRelative;
     $dirPath = dirname($destinationPath);
     if (!is_dir($dirPath)) {
         if (!File::mkdir($dirPath)) {
             throw new \Exception("Unable to create directory: " . $dirPath . " for asset :" . $this->getId());
         }
     }
     $typeChanged = false;
     // fix for missing parent folders
     // check if folder of new destination is already created and if not do so
     $newPath = dirname($this->getFileSystemPath());
     if (!is_dir($newPath)) {
         if (!File::mkdir($newPath)) {
             throw new \Exception("Unable to create directory: " . $newPath . " for asset :" . $this->getId());
         }
     }
     if ($this->getType() != "folder") {
         if ($this->getDataChanged()) {
             $src = $this->getStream();
             $streamMeta = stream_get_meta_data($src);
             if ($destinationPath != $streamMeta["uri"]) {
                 $dest = fopen($destinationPath, "w", false, File::getContext());
                 if ($dest) {
                     stream_copy_to_stream($src, $dest);
                     if (!fclose($dest)) {
                         throw new \Exception("Unable to close file handle " . $destinationPath . " for asset " . $this->getId());
                     }
                 } else {
                     throw new \Exception("Unable to open file: " . $destinationPath . " for asset " . $this->getId());
                 }
             }
             $this->stream = null;
             // set stream to null, so that the source stream isn't used anymore after saving
             @chmod($destinationPath, File::getDefaultMode());
             // check file exists
             if (!is_file($destinationPath)) {
                 throw new \Exception("couldn't create new asset, file " . $destinationPath . " doesn't exist");
             }
             // set mime type
             $mimetype = Mime::detect($destinationPath);
             $this->setMimetype($mimetype);
             // set type
             $type = self::getTypeFromMimeMapping($mimetype, $this->getFilename());
             if ($type != $this->getType()) {
                 $this->setType($type);
                 $typeChanged = true;
             }
         }
         // scheduled tasks are saved in $this->saveVersion();
     } else {
         if (!is_dir($destinationPath) && !is_dir($this->getFileSystemPath())) {
             if (!File::mkdir($this->getFileSystemPath())) {
                 throw new \Exception("Unable to create directory: " . $this->getFileSystemPath() . " for asset :" . $this->getId());
             }
         }
     }
     // save properties
     $this->getProperties();
     $this->getDao()->deleteAllProperties();
     if (is_array($this->getProperties()) and count($this->getProperties()) > 0) {
         foreach ($this->getProperties() as $property) {
             if (!$property->getInherited()) {
                 $property->setDao(null);
                 $property->setCid($this->getId());
                 $property->setCtype("asset");
                 $property->setCpath($this->getRealFullPath());
                 $property->save();
             }
         }
     }
     // save dependencies
     $d = $this->getDependencies();
     $d->clean();
     foreach ($this->resolveDependencies() as $requirement) {
         if ($requirement["id"] == $this->getId() && $requirement["type"] == "asset") {
             // dont't add a reference to yourself
             continue;
         } else {
             $d->addRequirement($requirement["id"], $requirement["type"]);
         }
     }
     $d->save();
     $this->getDao()->update();
     //set object to registry
     \Zend_Registry::set("asset_" . $this->getId(), $this);
     if (get_class($this) == "Asset" || $typeChanged) {
         // get concrete type of asset
         // this is important because at the time of creating an asset it's not clear which type (resp. class) it will have
         // the type (image, document, ...) depends on the mime-type
         \Zend_Registry::set("asset_" . $this->getId(), null);
         $asset = self::getById($this->getId());
         \Zend_Registry::set("asset_" . $this->getId(), $asset);
     }
     // lastly create a new version if necessary
     // this has to be after the registry update and the DB update, otherwise this would cause problem in the
     // $this->__wakeUp() method which is called by $version->save(); (path correction for version restore)
     if ($this->getType() != "folder") {
         $this->saveVersion(false, false);
     }
     $this->closeStream();
 }