/**
  * @param LifecycleEventArgs $args
  * @throws \Exception
  * @throws \Symfony\Component\Filesystem\Exception\IOException
  */
 protected function upload(LifecycleEventArgs $args)
 {
     $entity = $args->getEntity();
     if ($entity instanceof FileLifecycleInterface && null !== $entity->getFile()) {
         if ($entity->getPreviousPath() != null && $this->filesystem->has($entity->getPreviousPath())) {
             $this->filesystem->delete($entity->getPreviousPath());
         }
         $tmpFilename = sha1(uniqid(mt_rand(), true));
         $tmpDir = $this->stripTrailingSlash(sys_get_temp_dir());
         $tmpPath = $tmpDir . '/' . $tmpFilename;
         $entity->getFile()->move($tmpDir, $tmpFilename);
         try {
             $this->filesystem->upload($tmpPath, $entity->getPath());
             $this->getLocalFilesystem()->remove($tmpPath);
         } catch (IOException $e) {
             $this->logger->error('Failed to delete file {path}', array('path' => $tmpPath, 'class' => get_class($this)));
             throw $e;
         } catch (\Exception $e) {
             $this->logger->error('Failed to upload file {file}', array('file' => $entity->getFile(), 'class' => get_class($this)));
             $this->getLocalFilesystem()->remove($tmpPath);
             throw $e;
         }
         $entity->setFile(null);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function copyToFilesystem($path, FilesystemInterface $targetFilesystem, $targetPath, $overwrite = false)
 {
     $tempDir = $this->stripTrailingSlash(sys_get_temp_dir()) . DIRECTORY_SEPARATOR . sha1(uniqid(mt_rand(), true));
     if (!@mkdir($tempDir)) {
         $error = error_get_last();
         throw new \RuntimeException(sprintf('Failed to create directory %s (%s)', $tempDir, strip_tags($error['message'])));
     }
     try {
         $this->download($path, $tempDir);
         $targetFilesystem->upload($tempDir . '/' . basename($path), $targetPath, $overwrite);
         $this->getLocalFilesystem()->remove($tempDir);
     } catch (\Exception $e) {
         $this->getLocalFilesystem()->remove($tempDir);
         throw $e;
     }
 }