/** * {@inheritDoc} */ protected function doUpload(PropertyMapping $mapping, UploadedFile $file, $dir, $name) { $filesystem = $this->getFilesystem($mapping); if ($filesystem->getAdapter() instanceof MetadataSupporter) { $filesystem->getAdapter()->setMetadata($name, array('contentType' => $file->getMimeType())); } $src = new LocalStream($file->getPathname()); $dst = $filesystem->createStream($dir . $name); $src->open(new StreamMode('rb+')); $dst->open(new StreamMode('wb+')); while (!$src->eof()) { $data = $src->read(100000); $dst->write($data); } $dst->close(); $src->close(); }
/** * Copy file from local filesystem to attachment storage with new name * * @param string $localFilePath * @param string $destinationFileName */ public function copyLocalFileToStorage($localFilePath, $destinationFileName) { $src = new LocalStream($localFilePath); $dst = $this->filesystem->createStream($destinationFileName); $src->open(new StreamMode('rb+')); $dst->open(new StreamMode('wb+')); while (!$src->eof()) { $dst->write($src->read(100000)); } $dst->close(); $src->close(); }
/** * Processes a given media * * @param \Oryzone\MediaStorage\Model\Media|\Oryzone\MediaStorage\Model\MediaInterface $media * @param bool $isUpdate * * @throws Exception\VariantProcessingException * @return bool */ protected function processMedia(MediaInterface $media, $isUpdate = FALSE) { $context = $this->getContext($media->getContext()); $provider = $this->getProvider($context->getProviderName(), $context->getProviderOptions()); $variantsTree = $context->buildVariantTree(); $filesystem = $this->getFilesystem($context->getFilesystemName()); if ($media->hasHint(MediaInterface::HINT_NAMING_STRATEGY)) { $namingStrategy = $media->getHint(MediaInterface::HINT_NAMING_STRATEGY); } else { $namingStrategy = $this->getNamingStrategy($context->getNamingStrategyName()); } $generatedFiles = array(); $variantsTree->visit(function (VariantNode $node, $level) use($provider, $context, $media, $filesystem, $namingStrategy, &$generatedFiles, $isUpdate) { $variant = $node->getContent(); $parent = $node->getParent() ? $node->getParent()->getContent() : NULL; if ($isUpdate && $media->hasVariant($variant->getName())) { $existingVariant = $media->getVariantInstance($variant->getName()); if ($existingVariant->isReady()) { $filesystem->delete($existingVariant->getFilename()); } $media->removeVariant($variant->getName()); } $media->addVariant($variant); $file = NULL; if ($provider->getContentType() == ProviderInterface::CONTENT_TYPE_FILE) { if ($parent) { // checks if the parent file has been generated in a previous step if (isset($generatedFiles[$parent->getName()])) { $file = $generatedFiles[$parent->getName()]; } else { //otherwise try to read the file from the storage if the variant is ready //TODO throw new VariantProcessingException(sprintf('Cannot load parent variant ("%s") file for variant "%s" of media "%s"', $parent->getName(), $variant->getName(), $media), $media, $variant); } } else { $source = $media->getContent(); if (is_string($source)) { if (!is_file($source)) { throw new IOException(sprintf('Cannot load file "%s" for media "%s", variant "%s". File not found.', $source, $media, $variant->getName()), $source); } $file = new \SplFileInfo($source); } elseif (is_object($source) && $source instanceof \SplFileInfo) { $file = $source; } else { throw new IOException(sprintf('Object of class "%s" is not an instance of \\SplFileInfo so it cannot be loaded as a file while processing media "%s", variant "%s"', get_class($source), $media, $variant->getName()), $source); } } } switch ($variant->getMode()) { case VariantInterface::MODE_INSTANT: $result = $provider->process($media, $variant, $file); if ($result) { $generatedFiles[$variant->getName()] = $result; $name = $namingStrategy->generateName($media, $variant, $filesystem); if ($result instanceof UploadedFile) { $extension = $result->guessExtension(); } else { $extension = $result->getExtension(); } $name .= '.' . $extension; // read file data as stream and writes it in a single block $src = new Local($result->getPathname()); $src->open(new StreamMode('rb+')); $content = ''; while (!$src->eof()) { $data = $src->read(100000); $content .= $data; } $dst = $filesystem->createFile($name); $dst->setContent($content); $src->close(); $variant->setFilename($name); $variant->setStatus(VariantInterface::STATUS_READY); } break; case VariantInterface::MODE_LAZY: // TODO break; case VariantInterface::MODE_QUEUE: // TODO break; } //updates the variant in the media (to store the new values) $media->addVariant($variant); }); $provider->removeTempFiles(); return TRUE; // marks the media as updated }