Skip to content

Commit

Permalink
Fixed the way images are saved, adding the correct extension type at …
Browse files Browse the repository at this point in the history
…the end of the fileName (#25)

* added image versioning to allow update with the same name
* Test path using pathinfo, removed regex
* removed allowed images format array from fonction
* Add some security
* removed multi extensions
* Images are now saved with valid extension name
* refacto the way to find image extension
* rollback str_contains
* starts_with instead of str_contains in uploadable image service
* cammel case for tony
  • Loading branch information
wprod authored and shaoshiva committed Oct 17, 2017
1 parent cee11ae commit 0bba298
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
"scripts": {
"lint" : [
"php-cs-fixer fix --dry-run --config .php_cs -vv --diff --allow-risky=yes"
"php-cs-fixer fix --dry-run --config .php_cs -vv --diff --allow-risky=yes"
],
"test" : [
"phpunit"
Expand Down
10 changes: 10 additions & 0 deletions src/ModelTraits/UploadableImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ public function fillUploadedImageAttributeValue(string $imageAttributeName, stri
&& $this->isTranslatableAttribute($imageAttributeName)
) {
$this->setTranslation($imageAttributeName, (string) request('locale', $this->getLocale()), $path); // Default value is relevant when using seeders or any environment where we dont have acces to "request".

if (!empty($path)) {
$path = preg_replace('/\?v=.*/', '', $path);
$this->setTranslation($imageAttributeName, (string) request('locale', $this->getLocale()), $path.'?v='.uniqid());
}
} else {
$this->{$imageAttributeName} = $path;

if (!empty($path)) {
$path = preg_replace('/\?v=.*/', '', $path);
$this->{$imageAttributeName} = $path.'?v='.uniqid();
}
}
}

Expand Down
23 changes: 17 additions & 6 deletions src/Services/UploadImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
*/
class UploadImageService extends AbstractUploadService
{
/**
* Allowed image extensions
*
* @var array
*/
protected $allowedExtensions = ['jpeg', 'jpg', 'gif', 'png'];

/**
* Filled with images during model saving
* Images will be used on Model "saved" event
Expand Down Expand Up @@ -50,7 +57,7 @@ public function saveImages(Model $model)

foreach ($this->tmpImages as $imageAttributeName => $image) {
// 1. Get image path
$filePath = $this->getImagePath($imageAttributeName);
$filePath = $this->getImagePath($imageAttributeName, $image->mime);

// 2. Store the image on disk.
\Storage::disk(self::STORAGE_DISK_NAME)->put($filePath, $image->stream());
Expand All @@ -76,16 +83,18 @@ public function saveImages(Model $model)
* @param $imageAttributeName
* @return string
*/
protected function getImagePath(string $imageAttributeName): string
protected function getImagePath(string $imageAttributeName, string $mime): string
{
$extension = str_replace_first('image/', '.', $mime);

$folderName = snake_case(class_basename(get_class($this->model)));
$destination_path = $folderName.'/'.$this->model->getKey().'/'.$imageAttributeName;
$imageSlugAttribute = array_get($this->slugAttributes($this->model->uploadableImages()), $imageAttributeName);

// 1. Generate a filename.
$filename = md5(time()).'.jpg';
$filename = md5(time()).$extension;
if (!empty($imageSlugAttribute)) {
$filename = str_slug($this->model->{$imageSlugAttribute}).'.jpg';
$filename = str_slug($this->model->{$imageSlugAttribute}).$extension;
}

return $destination_path.'/'.$filename;
Expand Down Expand Up @@ -208,6 +217,8 @@ protected function setUploadedImage(string $imageAttributeName)
$originalValue = $this->model->getOriginal($imageAttributeName);
}

$pathExtension = pathinfo($value, PATHINFO_EXTENSION);

// Image is removed
if (empty($value)) {
$this->deleteOldImage($originalValue, $imageAttributeName);
Expand All @@ -223,14 +234,14 @@ protected function setUploadedImage(string $imageAttributeName)
}

// An image is already uploaded, a new one is uploaded
if (ends_with($value, '.jpg') && !empty($originalValue)) {
if (starts_with($pathExtension, $this->allowedExtensions) && !empty($originalValue)) {
$this->setNewImage($value, $originalValue, $imageAttributeName);

return;
}

// No image is uploaded
if (!ends_with($value, '.jpg')) {
if (!starts_with($pathExtension, $this->allowedExtensions)) {
$this->model->fillUploadedImageAttributeValue($imageAttributeName, '');

return;
Expand Down

0 comments on commit 0bba298

Please sign in to comment.