/**
  * get custom mapping for resource if have
  *
  * @param array                   $config   array of global resource config
  * @param ResourceObjectInterface $resource resource
  *
  * @return null|array
  */
 protected function getResourceMapping($config, ResourceObjectInterface $resource)
 {
     if ($resource->getMapping() && isset($config['mappings'][$resource->getMapping()])) {
         return $config['mappings'][$resource->getMapping()];
     } elseif ($resource->getMapping()) {
         $message = sprintf('The resource mapping configuration "%s" doest not exist.', $resource->getMapping());
         throw new \RuntimeException($message);
     }
     return null;
 }
 /**
  * Set the name of resource using the context and resource mapping information
  *
  * @param ResourceObjectInterface $resource
  * @param string                  $baseName default name
  * @param object                  $context  context to resolve tokens
  */
 protected function setResourceName(ResourceObjectInterface $resource, $baseName = null, $context = null)
 {
     if ($baseName) {
         if (strpos($baseName, $resource->getFile()->guessExtension()) === false) {
             $filename = $baseName . '.' . $resource->getFile()->guessExtension();
         } else {
             $filename = $baseName;
         }
     } else {
         if ($resource->getId()) {
             $filename = $resource->getName();
         } else {
             //create unique name
             $filename = sha1(uniqid(mt_rand(), true)) . '.' . $resource->getFile()->guessExtension();
         }
     }
     $mapping = $this->getResourceMapping($this->config, $resource);
     if (isset($mapping['name'])) {
         $filename = $this->parseName($mapping['name'], $context, $baseName);
         $filename .= '.' . $resource->getFile()->guessExtension();
         $resource->setName($filename);
         $filename = basename($filename);
     }
     $resource->setName($filename);
 }
 /**
  * @inheritdoc
  */
 public function deleteFile(ResourceObjectInterface $resource)
 {
     $path = $this->getLocationConfig('path', $resource->getLocation(), $this->config);
     if ($resource->getRelativePath()) {
         $path .= $resource->getRelativePath();
     }
     $oldFile = $path . DIRECTORY_SEPARATOR . $resource->getName();
     $this->fileSystem->remove($oldFile);
     return !$this->fileSystem->exists($oldFile);
 }
 /**
  * loadResource
  *
  * @param ResourceObjectInterface $resource
  *
  * @throws \Exception
  */
 protected function loadResource(ResourceObjectInterface $resource)
 {
     $resolverName = $this->getLocationConfig('resolver', $resource->getLocation(), $this->config);
     $resolver = $this->resolverManager->get($resolverName);
     $resolver->setConfig($this->config);
     $resource->setFile($resolver->getFile($resource));
     $resource->setUrl($resolver->getUrl($resource));
 }