/** * Store a file content. * * @param UploadContext $context * @param $content * @param array $metadataMap * @return UploadedFile */ public function store(UploadContext $context, $content, array $metadataMap = array()) { $name = $this->namingStrategy->getName($context); $directory = $this->storageStrategy->getDirectory($context, $name); $path = $directory . '/' . $name; $adapter = $this->filesystem->getAdapter(); if ($adapter instanceof MetadataSupporter) { $adapter->setMetadata($path, $this->resolveMetadataMap($context, $metadataMap)); } $this->filesystem->write($path, $content); $file = $this->filesystem->get($path); return new UploadedFile($this, $file); }
/** * {@inheritDoc} */ public function clearCache($maxAge) { $delTime = time() - (int) $maxAge; $num = 0; foreach ($this->temporaryFilesystem->keys() as $key) { if (!$this->temporaryFilesystem->getAdapter()->isDirectory($key)) { if ($delTime > $this->temporaryFilesystem->mtime($key)) { $this->temporaryFilesystem->delete($key); $num++; } } } return $num; }
/** * Update attachment entity before upload * * @param File $entity */ public function preUpload(File $entity) { if ($entity->isEmptyFile()) { if ($entity->getFilename() !== null && $this->filesystem->has($entity->getFilename())) { $this->filesystem->delete($entity->getFilename()); } $entity->setFilename(null); $entity->setExtension(null); $entity->setOriginalFilename(null); } if ($entity->getFile() !== null && $entity->getFile()->isFile()) { $entity->setOwner($this->securityFacadeLink->getService()->getLoggedUser()); $file = $entity->getFile(); if ($entity->getFilename() !== null && $this->filesystem->has($entity->getFilename())) { $this->filesystem->delete($entity->getFilename()); } $entity->setExtension($file->guessExtension()); if ($file instanceof UploadedFile) { $entity->setOriginalFilename($file->getClientOriginalName()); $entity->setMimeType($file->getClientMimeType()); $entity->setFileSize($file->getClientSize()); } else { $entity->setOriginalFilename($file->getFileName()); $entity->setMimeType($file->getMimeType()); $entity->setFileSize($file->getSize()); } $entity->setFilename($this->generateFileName($entity->getExtension())); $fsAdapter = $this->filesystem->getAdapter(); if ($fsAdapter instanceof MetadataSupporter) { $fsAdapter->setMetadata($entity->getFilename(), ['contentType' => $entity->getMimeType()]); } } }
/** * @param \Gaufrette\Filesystem $filesystem * @param \spec\Gaufrette\MetadataAdapter $adapter */ function it_sets_content_of_file($filesystem, $adapter) { $adapter->setMetadata('filename', array())->shouldNotBeCalled(); $filesystem->getAdapter()->willReturn($adapter); $filesystem->write('filename', 'some content', true)->shouldBeCalled()->willReturn(21); $this->setContent('some content')->shouldReturn(21); $this->getContent('filename')->shouldReturn('some content'); }
public function __construct(Filesystem $filesystem, $bufferSize, $streamWrapperPrefix, $prefix) { if (!$filesystem->getAdapter() instanceof StreamFactory) { throw new \InvalidArgumentException('The filesystem used as chunk storage must implement StreamFactory'); } $this->filesystem = $filesystem; $this->bufferSize = $bufferSize; $this->prefix = $prefix; $this->streamWrapperPrefix = $streamWrapperPrefix; }
/** * @param Media $media */ public function removeMedia(Media $media) { $adapter = $this->fileSystem->getAdapter(); # Remove the file from filesystem $fileKey = $this->getFilePath($media); if ($adapter->exists($fileKey)) { $adapter->delete($fileKey); } # Remove the files containing folder if there's nothing left $folderPath = $this->getFileFolderPath($media); if ($adapter->exists($folderPath) && $adapter->isDirectory($folderPath) && !empty($folderPath)) { $allMyKeys = $adapter->keys(); $everythingfromdir = preg_grep('/' . $folderPath, $allMyKeys); if (count($everythingfromdir) === 1) { $adapter->delete($folderPath); } } $media->setRemovedFromFileSystem(true); }
/** * Push a local image/variation to the remote. * * If it is not hydrated this function will throw an exception * * @param Image $image * @param bool $overwrite * * @return $this * * @throws ImageManagerException * @throws ObjectAlreadyExistsException * @throws \Exception */ public function push(Image $image, $overwrite = true) { if (!$image->isHydrated() && $image instanceof ImageVariation) { // A pull on a variation will check if the variation exists, if not create it $this->pull($image); } if (!$image->isHydrated()) { throw new ImageManagerException(self::ERR_NOT_HYDRATED); } if (!$overwrite && $this->tagExists($image->getKey()) === true) { throw new ObjectAlreadyExistsException(self::ERR_ALREADY_EXISTS); } $adapter = $this->filesystem->getAdapter(); if ($adapter instanceof MetadataSupporter) { $metadata = []; if ($image->getMimeType()) { // Set image ContentType on remote filesystem $metadata['ContentType'] = $image->getMimeType(); } $adapter->setMetadata($image->getKey(), $metadata); } // Retrieve source image metadata $metadata = null; if (!$image instanceof ImageVariation) { $image_manipulation = new ImageInspector(); $metadata = $image_manipulation->getImageMetadata($image); } try { $this->filesystem->write($image->getKey(), $image->getData(), $overwrite); $image->__friendSet('persistent', true); $this->tag($image->getKey(), $metadata); } catch (FileAlreadyExists $e) { $this->tag($image->getKey(), $metadata); throw new ObjectAlreadyExistsException(self::ERR_ALREADY_EXISTS); } return $this; }
/** * Whether this filesystem has an existing directory at the given path. * * @param string $path The path to the directory, relative to this filesystem. * * @return boolean */ private function isDirectory($path) { $adapter = $this->gaufrette->getAdapter(); return $adapter->isDirectory($this->getGaufrettePath($path)); }
/** * Get Virtual Files Action * * @param string $file * * @throws NotFoundHttpException * @return Response */ public function fileAction($file = null) { if (null == $file) { return $this->redirect($this->generateUrl('vfs_files', array('file' => '/'))); } $cacheDirectory = $this->getParameter('adapter_cache_dir'); $local = new LocalAdapter($cacheDirectory, true); $localadapter = new LocalAdapter($this->getParameter('adapter_files')); $adapter = new CacheAdapter($localadapter, $local, 3600); $fsAvatar = new Filesystem($adapter); if ($this->endswith($file, '/')) { if ($fsAvatar->has($file)) { $this->gvars['title'] = $this->translate('indexof', array('%dir%' => $this->generateUrl('vfs_files', array('file' => $file)))); $this->gvars['pagetitle'] = $this->translate('indexof_raw', array('%dir%' => $this->generateUrl('vfs_files', array('file' => $file)))); $path = substr($file, 1); $fs = $fsAvatar->listKeys($path); $listfiles = array(); foreach ($fs['dirs'] as $key) { $fulldir = $key . '/'; $dirname = $key; if (substr($key, 0, strlen($path)) == $path) { $dirname = substr($dirname, strlen($path)); } if (!strstr($dirname, '/')) { $listfiles[$fulldir] = $dirname; } } foreach ($fs['keys'] as $key) { $fullfile = $key; $filename = $key; if (substr($key, 0, strlen($path)) == $path) { $filename = substr($filename, strlen($path)); } if (!strstr($filename, '/')) { $listfiles[$fullfile] = $filename; } } $this->gvars['fs'] = $listfiles; return $this->renderResponse('AcfResBundle:Vfs:list_files.html.twig', $this->gvars); } else { throw new NotFoundHttpException(); } } if ($fsAvatar->has($file)) { if ($fsAvatar->getAdapter()->isDirectory($file)) { $file .= '/'; return $this->redirect($this->generateUrl('vfs_files', array('file' => $file))); } $reqFile = $fsAvatar->get($file); $response = new Response(); $response->headers->set('Content-Type', 'binary'); $response->setContent($reqFile->getContent()); return $response; } else { throw new NotFoundHttpException(); } }
public function __construct(\Iterator $iterator, $regex, Filesystem $fs) { parent::__construct($iterator); $this->regex = $regex; $this->fsAdapter = $fs->getAdapter(); }
/** * @When I call method :method of current fs adapter */ public function iCallMethodOfCurrentAdapter($method) { $method = new \ReflectionMethod($this->filesystem->getAdapter(), $method); $this->methodOutput = $method->invoke($this->filesystem->getAdapter()); }
/** * {@inheritdoc} */ public function getLastModified() { return $this->filesystem->getAdapter()->mtime($this->prefix); }