Skip to content

Commit

Permalink
Fixed filename limit #1hwx70q
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed Oct 5, 2021
1 parent 1520c21 commit b11fa98
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/Traits/Uploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Common\Media as MediaModel;
use App\Utilities\Date;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use MediaUploader;

trait Uploads
Expand All @@ -19,6 +20,8 @@ public function getMedia($file, $folder = 'settings', $company_id = null)

$path = $this->getMediaFolder($folder, $company_id);

$file_name = $this->getMediaFileName($file);

return MediaUploader::makePrivate()
->beforeSave(function(MediaModel $media) {
$media->company_id = company_id();
Expand All @@ -27,6 +30,7 @@ public function getMedia($file, $folder = 'settings', $company_id = null)
})
->fromSource($file)
->toDirectory($path)
->useFilename($file_name)
->upload();
}

Expand Down Expand Up @@ -129,4 +133,37 @@ public function isLocalStorage()
{
return config('filesystems.disks.' . config('filesystems.default') . '.driver') == 'local';
}

public function getMediaFileName($file): string
{
$file_name = $this->filename($file);

if (Str::length($file_name) > '110') {
$file_name = Str::limit($file_name, 110);
}

return $file_name . '.' . $this->extension($file);
}

/**
* {@inheritdoc}
*/
public function filename($file): string
{
return pathinfo((string)$file->getClientOriginalName(), PATHINFO_FILENAME);
}

/**
* {@inheritdoc}
*/
public function extension($file): string
{
$extension = $file->getClientOriginalExtension();

if ($extension) {
return $extension;
}

return (string)$file->guessExtension();
}
}

0 comments on commit b11fa98

Please sign in to comment.