public deleteResource ( |
||
$resource | The resource to delete | |
$unpublishResource | boolean | If the resource should be unpublished before deleting it from the storage |
return | boolean | true if the resource was deleted, otherwise FALSE |
public function deleteResource(PersistentResource $resource, $unpublishResource = true)
{
$this->initialize();
$collectionName = $resource->getCollectionName();
$result = $this->resourceRepository->findBySha1($resource->getSha1());
if (count($result) > 1) {
$this->systemLogger->log(sprintf('Not removing storage data of resource %s (%s) because it is still in use by %s other PersistentResource object(s).', $resource->getFilename(), $resource->getSha1(), count($result) - 1), LOG_DEBUG);
} else {
if (!isset($this->collections[$collectionName])) {
$this->systemLogger->log(sprintf('Could not remove storage data of resource %s (%s) because it refers to the unknown collection "%s".', $resource->getFilename(), $resource->getSha1(), $collectionName), LOG_WARNING);
return false;
}
$storage = $this->collections[$collectionName]->getStorage();
if (!$storage instanceof WritableStorageInterface) {
$this->systemLogger->log(sprintf('Could not remove storage data of resource %s (%s) because it its collection "%s" is read-only.', $resource->getFilename(), $resource->getSha1(), $collectionName), LOG_WARNING);
return false;
}
try {
$storage->deleteResource($resource);
} catch (\Exception $exception) {
$this->systemLogger->log(sprintf('Could not remove storage data of resource %s (%s): %s.', $resource->getFilename(), $resource->getSha1(), $exception->getMessage()), LOG_WARNING);
return false;
}
if ($unpublishResource) {
/** @var TargetInterface $target */
$target = $this->collections[$collectionName]->getTarget();
$target->unpublishResource($resource);
$this->systemLogger->log(sprintf('Removed storage data and unpublished resource %s (%s) because it not used by any other PersistentResource object.', $resource->getFilename(), $resource->getSha1()), LOG_DEBUG);
} else {
$this->systemLogger->log(sprintf('Removed storage data of resource %s (%s) because it not used by any other PersistentResource object.', $resource->getFilename(), $resource->getSha1()), LOG_DEBUG);
}
}
$resource->setDeleted();
$this->resourceRepository->remove($resource);
return true;
}
/** * Convert an object from $source to an \Neos\Media\Domain\Model\AssetInterface implementation * * @param mixed $source * @param string $targetType must implement 'Neos\Media\Domain\Model\AssetInterface' * @param array $convertedChildProperties * @param PropertyMappingConfigurationInterface $configuration * @return Error|AssetInterface The converted Asset, a Validation Error or NULL * @throws InvalidTargetException */ public function convertFrom($source, $targetType, array $convertedChildProperties = array(), PropertyMappingConfigurationInterface $configuration = null) { $object = null; if (is_string($source) && $source !== '') { $source = array('__identity' => $source); } if (isset($convertedChildProperties['resource']) && $convertedChildProperties['resource'] instanceof PersistentResource) { $resource = $convertedChildProperties['resource']; if (isset($this->resourcesAlreadyConvertedToAssets[$resource->getSha1()])) { $object = $this->resourcesAlreadyConvertedToAssets[$resource->getSha1()]; } // This is pretty late to override the targetType, but usually you want to determine the model type from the resource when a new resource was uploaded... $targetType = $this->applyModelMappingStrategy($targetType, $resource, $source); } if ($object === null) { if ($configuration !== null && $configuration->getConfigurationValue(self::class, self::CONFIGURATION_ONE_PER_RESOURCE) === true && isset($convertedChildProperties['resource'])) { $resource = $convertedChildProperties['resource']; $possibleAsset = $this->assetRepository->findOneByResourceSha1($resource->getSha1()); if ($possibleAsset !== null) { $this->resourceManager->deleteResource($resource); return $possibleAsset; } } $object = parent::convertFrom($source, $targetType, $convertedChildProperties, $configuration); } if ($object instanceof AssetInterface) { $object = $this->applyTypeSpecificHandling($object, $source, $convertedChildProperties, $configuration); if ($object !== null) { $this->resourcesAlreadyConvertedToAssets[$object->getResource()->getSha1()] = $object; if (isset($resource) && $resource !== $object->getResource()) { $this->resourceManager->deleteResource($resource); } } } return $object; }