addDependency('container'); $this->addDependency('config'); } protected function getFileStorageManager() { return $this->getInjection('container')->get('fileStorageManager'); } protected function getConfig() { return $this->getInjection('config'); } public function beforeSave(Entity $entity, array $options = array()) { parent::beforeSave($entity, $options); $storage = $entity->get('storage'); if (!$storage) { $entity->set('storage', $this->getConfig()->get('defaultFileStorage', null)); } if ($entity->isNew()) { if (!$entity->has('size') && $entity->has('contents')) { $entity->set('size', mb_strlen($entity->get('contents'))); } } } public function save(Entity $entity, array $options = array()) { $isNew = $entity->isNew(); $result = parent::save($entity, $options); if ($isNew) { if (!empty($entity->id) && $entity->has('contents')) { $contents = $entity->get('contents'); $this->getFileStorageManager()->putContents($entity, $contents); } } return $result; } protected function afterRemove(Entity $entity, array $options = array()) { parent::afterRemove($entity, $options); $this->getFileStorageManager()->unlink($entity); } public function getCopiedAttachment(Entity $entity, $role = null) { $attachment = $this->get(); $attachment->set(array( 'sourceId' => $entity->getSourceId(), 'name' => $entity->get('name'), 'type' => $entity->get('type'), 'size' => $entity->get('size') )); if ($role) { $attachment->set('role', $role); } $this->save($attachment); return $attachment; } public function getContents(Entity $entity) { return $this->getFileStorageManager()->getContents($entity); } public function getFilePath(Entity $entity) { return $this->getFileStorageManager()->getLocalFilePath($entity); } public function hasDownloadUrl(Entity $entity) { return $this->getFileStorageManager()->hasDownloadUrl($entity); } public function getDownloadUrl(Entity $entity) { return $this->getFileStorageManager()->getDownloadUrl($entity); } }