get('storage'); if (!$storage) { $entity->set('storage', $this->config->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 = []) { $isNew = $entity->isNew(); if ($isNew) { $entity->id = Util::generateId(); if ($entity->has('contents')) { $contents = $entity->get('contents'); if (is_string($contents)) { $this->fileStorageManager->putContents($entity, $contents); } } } $result = parent::save($entity, $options); return $result; } protected function afterRemove(Entity $entity, array $options = []) { parent::afterRemove($entity, $options); $duplicateCount = $this->where([ 'OR' => [ [ 'sourceId' => $entity->getSourceId() ], [ 'id' => $entity->getSourceId() ] ], ])->count(); if ($duplicateCount === 0) { $this->fileStorageManager->unlink($entity); if (in_array($entity->get('type'), $this->imageTypeList)) { $this->removeImageThumbs($entity); } } } public function removeImageThumbs($entity) { foreach ($this->imageThumbList as $suffix) { $filePath = "data/upload/thumbs/".$entity->getSourceId()."_{$suffix}"; if ($this->fileManager->isFile($filePath)) { $this->fileManager->removeFile($filePath); } } } 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'), 'role' => $entity->get('role') )); if ($role) { $attachment->set('role', $role); } $this->save($attachment); return $attachment; } public function getContents(Entity $entity) : ?string { return $this->fileStorageManager->getContents($entity); } public function getFilePath(Entity $entity) : string { return $this->fileStorageManager->getLocalFilePath($entity); } public function hasDownloadUrl(Entity $entity) : bool { return $this->fileStorageManager->hasDownloadUrl($entity); } public function getDownloadUrl(Entity $entity) : string { return $this->fileStorageManager->getDownloadUrl($entity); } }