moved move_to_external_storage into HelperService.php

This commit is contained in:
Peter Papp
2021-03-14 13:11:21 +01:00
parent 419a1d8180
commit 9297eae1d7
2 changed files with 43 additions and 42 deletions

View File

@@ -419,7 +419,7 @@ class FileManagerService
$this->helper->check_user_storage_capacity($user_id, $file_size, $temp_filename);
// Create thumbnail
$thumbnail = self::get_image_thumbnail('chunks/' . $temp_filename, $disk_file_name, $user_id);
$thumbnail = $this->helper->get_image_thumbnail('chunks/' . $temp_filename, $disk_file_name, $user_id);
// Move finished file from chunk to file-manager directory
$disk_local->move('chunks/' . $temp_filename, "files/$user_id/$disk_file_name");
@@ -486,44 +486,4 @@ class FileManagerService
]);
}
}
/**
* Create thumbnail for images
*
* @param string $file_path
* @param string $filename
* @param string $user_id
* @param $file
* @return string|null
*/
private function get_image_thumbnail(string $file_path, string $filename, string $user_id)
{
$local_disk = Storage::disk('local');
// Create thumbnail from image
if (in_array($local_disk->mimeType($file_path), ['image/gif', 'image/jpeg', 'image/jpg', 'image/png', 'image/webp'])) {
// Get thumbnail name
$thumbnail = 'thumbnail-' . $filename;
// Create intervention image
$image = Image::make($local_disk->path($file_path))->orientate();
// Resize image
$image->resize(512, null, function ($constraint) {
$constraint->aspectRatio();
})->stream();
// Store thumbnail to disk
$local_disk->put("files/$user_id/$thumbnail", $image);
}
// Return thumbnail as svg file
if ($local_disk->mimeType($file_path) === 'image/svg+xml') {
$thumbnail = $filename;
}
return $thumbnail ?? null;
}
}

View File

@@ -9,6 +9,7 @@ use DB;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image;
use Symfony\Component\HttpKernel\Exception\HttpException;
class HelperService
@@ -99,7 +100,7 @@ class HelperService
* @param string $filename
* @param string|null $thumbnail
*/
function move_to_external_storage(string $filename, ?string $thumbnail): void
function move_to_external_storage($filename, $thumbnail = null): void
{
$disk_local = Storage::disk('local');
@@ -157,4 +158,44 @@ class HelperService
$disk_local->delete('files/' . $file);
}
}
/**
* Create image thumbnail from gif, jpeg, jpg, png, webp or svg
*
* @param string $file_path
* @param string $filename
* @param string $user_id
* @return string|null
*/
function get_image_thumbnail($file_path, $filename, $user_id)
{
$local_disk = Storage::disk('local');
// Create thumbnail from image
if (in_array($local_disk->mimeType($file_path), ['image/gif', 'image/jpeg', 'image/jpg', 'image/png', 'image/webp'])) {
// Get thumbnail name
$thumbnail = "thumbnail-$filename";
// Create intervention image
$image = Image::make($local_disk->path($file_path))
->orientate();
// Resize image
$image->resize(512, null, function ($constraint) {
$constraint->aspectRatio();
})->stream();
// Store thumbnail to disk
$local_disk->put("files/$user_id/$thumbnail", $image);
}
// Return thumbnail as svg file
if ($local_disk->mimeType($file_path) === 'image/svg+xml') {
$thumbnail = $filename;
}
return $thumbnail ?? null;
}
}