Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep extension of uploaded file #52

Open
wants to merge 2 commits into
base: 1.1-release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions RemoteMedia/Provider/Cloudinary/CloudinaryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Netgen\Bundle\RemoteMediaBundle\Exception\MimeCategoryParseException;
use Netgen\Bundle\RemoteMediaBundle\RemoteMedia\Transformation\Registry;
use Netgen\Bundle\RemoteMediaBundle\RemoteMedia\UploadFile;
use Netgen\Bundle\RemoteMediaBundle\RemoteMedia\VariationResolver;
use Netgen\Bundle\RemoteMediaBundle\Core\FieldType\RemoteMedia\Value;
use Netgen\Bundle\RemoteMediaBundle\Exception\TransformationHandlerFailedException;
Expand Down Expand Up @@ -78,6 +79,27 @@ private function appendExtension($publicId, $fileUri)
return $publicId;
}

/**
* @param $publicId
* @param UploadFile $uploadFile
*
* @return string
*/
private function appendExtensionFromUploadFile($publicId, UploadFile $uploadFile)
{
$extension = $uploadFile->originalExtension();
if (empty($extension)) {
return $publicId;
}
$file = new File($uploadFile->uri());
$mimeCategory = $this->parseMimeCategory($file);
// cloudinary handles pdf in a weird way - it is considered an "image" but it delivers it with proper extension on download
if ($extension !== 'pdf' && !in_array($mimeCategory, $this->noExtensionMimeTypes)) {
$publicId .= '.' . $extension;
}
return $publicId;
}

/**
* Prepares upload options for Cloudinary.
* Every image with the same name will be overwritten.
Expand Down Expand Up @@ -121,6 +143,9 @@ protected function prepareUploadOptions($fileName, $fileUri, $options = array())
}

/**
* @deprecated this method will have a different signature in 2.0
* @see \Netgen\Bundle\RemoteMediaBundle\RemoteMedia\Provider\Cloudinary\CloudinaryProvider::uploadWithExtension
*
* Uploads the local resource to remote storage and builds the Value from the response.
*
* @param string $fileUri
Expand All @@ -138,6 +163,57 @@ public function upload($fileUri, $fileName, $options = array())
return Value::createFromCloudinaryResponse($response);
}

/**
* Prepares upload options for Cloudinary.
* Every image with the same name will be overwritten.
*
* @param UploadFile $uploadFile
* @param array $options
*
* @return array
*/
protected function prepareUploadOptionsWithExtension(UploadFile $uploadFile, $options = array())
{
$clean = preg_replace("/[^\p{L}|\p{N}]+/u", '_', $uploadFile->originalFilename());
$cleanFileName = preg_replace("/[\p{Z}]{2,}/u", '_', $clean);
$fileName = rtrim($cleanFileName, '_');
// check if overwrite is set, if it is, do not append random string
$overwrite = isset($options['overwrite']) ? $options['overwrite'] : false;
$invalidate = isset($options['invalidate']) ? $options['invalidate'] : $overwrite;
$publicId = $overwrite ? $fileName : $fileName . '_' . base_convert(uniqid(), 16, 36);
$publicId = $this->appendExtensionFromUploadFile($publicId, $uploadFile);
if (!empty($options['folder'])) {
$publicId = $options['folder'].'/'.$publicId;
}
return array(
'public_id' => $publicId,
'overwrite' => $overwrite,
'invalidate' => $invalidate,
'discard_original_filename' =>
isset($options['discard_original_filename']) ? $options['discard_original_filename'] : true,
'context' => array(
'alt' => !empty($options['alt_text']) ? $options['alt_text'] : '',
'caption' => !empty($options['caption']) ? $options['caption'] : '',
),
'resource_type' => !empty($options['resource_type']) ? $options['resource_type'] : 'auto'
);
}

/**
* Uploads the local resource to remote storage and builds the Value from the response.
*
* @param UploadFile $uploadFile
* @param array $options
*
* @return Value
*/
public function uploadWithExtension(UploadFile $uploadFile, $options = array())
{
$options = $this->prepareUploadOptionsWithExtension($uploadFile, $options);
$response = $this->gateway->upload($uploadFile->uri(), $options);
return Value::createFromCloudinaryResponse($response);
}

/**
* Builds transformation options for the provider to consume.
*
Expand Down
13 changes: 13 additions & 0 deletions RemoteMedia/RemoteMediaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ abstract public function supportsContentBrowser();
abstract public function supportsFolders();

/**
* @deprecated this method will have a different signature in 2.0
* @see \Netgen\Bundle\RemoteMediaBundle\RemoteMedia\RemoteMediaProvider::uploadWithExtension
*
* Uploads the local resource to remote storage and builds the Value from the response.
*
* @param string $fileUri
Expand All @@ -61,6 +64,16 @@ abstract public function supportsFolders();
*/
abstract public function upload($fileUri, $fileName, $options = array());

/**
* Uploads the local resource to remote storage and builds the Value from the response.
*
* @param \Netgen\Bundle\RemoteMediaBundle\RemoteMedia\UploadFile $uploadFile
* @param array $options
*
* @return mixed
*/
abstract public function uploadWithExtension(UploadFile $uploadFile, $options = array());

/**
* Gets the remote media Variation.
* If the remote media does not support variations, this method should return the Variation
Expand Down
96 changes: 96 additions & 0 deletions RemoteMedia/UploadFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Netgen\Bundle\RemoteMediaBundle\RemoteMedia;

use \eZHTTPFile;
use eZ\Publish\Core\FieldType\Image\Value;

class UploadFile
{
private $uri;

private $originalFilename;

private $originalExtension;

private function __construct() {}

/**
* Constructs UploadFile from given uri.
*
* @param $uri
*
* @return UploadFile
*/
public static function fromUri($uri)
{
$uploadFile = new UploadFile;

$uploadFile->uri = $uri;
$uploadFile->originalFilename = pathinfo($uri, PATHINFO_FILENAME);
$uploadFile->originalExtension = pathinfo($uri, PATHINFO_EXTENSION);

return $uploadFile;
}

/**
* Constructs UploadFile from given eZHTTPFile (usually uploaded through the legacy admin).
*
* @param eZHTTPFile $file
*
* @return UploadFile
*/
public static function fromZHTTPFile(eZHTTPFile $file)
{
$uploadFile = new UploadFile;

$uploadFile->uri = $file->Filename;
$uploadFile->originalFilename = pathinfo($file->OriginalFilename, PATHINFO_FILENAME);
$uploadFile->originalExtension = pathinfo($file->OriginalFilename, PATHINFO_EXTENSION);

return $uploadFile;
}

/**
* Constructs UploadFile from given eZImage field Value.
*
* @param Value $value
* @param $webRoot
*
* @return UploadFile
*/
public static function fromEzImageValue(Value $value, $webRoot)
{
$uploadFile = new UploadFile;

$uploadFile->uri = $webRoot.$value->uri;
$uploadFile->originalFilename = pathinfo($value->fileName, PATHINFO_FILENAME);
$uploadFile->originalExtension = pathinfo($value->fileName, PATHINFO_EXTENSION);

return $uploadFile;
}

/**
* @return string
*/
public function uri()
{
return $this->uri;
}

/**
* @return string
*/
public function originalFilename()
{
return $this->originalFilename;
}

/**
* @return string
*/
public function originalExtension()
{
return $this->originalExtension;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"netgen/admin-ui-bundle": "^2.0",
"matthiasnoback/symfony-config-test": "^2.0",
"matthiasnoback/symfony-dependency-injection-test": "^1.0",
"mikey179/vfsStream": "^1.0",
"mikey179/vfsstream": "^1.0",
"phpunit/phpunit": "^5.7",
"friendsofphp/php-cs-fixer": "^2.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Netgen\Bundle\RemoteMediaBundle\RemoteMedia\UploadFile;

$http = eZHTTPTool::instance();

$file = eZHTTPFile::fetch( 'file' );
Expand All @@ -23,10 +25,8 @@
$container = ezpKernel::instance()->getServiceContainer();
$provider = $container->get( 'netgen_remote_media.provider' );

$value = $provider->upload(
$file->Filename,
pathinfo($file->OriginalFilename, PATHINFO_FILENAME)
);
$uploadFile = UploadFile::fromZHTTPFile($file);
$value = $provider->uploadWithExtension($uploadFile);

$responseData = array(
'media' => !empty($value->resourceId) ? $value : false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Netgen\Bundle\RemoteMediaBundle\RemoteMedia\UploadFile;

$http = eZHTTPTool::instance();

$contentId = $Params['contentobject_id'];
Expand Down Expand Up @@ -29,7 +31,8 @@
$options['folder'] = $folder;
}

$value = $provider->upload($file->Filename, pathinfo($file->OriginalFilename, PATHINFO_FILENAME), $options);
$uploadFile = UploadFile::fromZHTTPFile($file);
$value = $provider->uploadWithExtension($uploadFile, $options);

$attribute = eZContentObjectAttribute::fetch($fieldId, $contentVersionId);
$attribute->setAttribute('data_text', json_encode($value));
Expand Down