FEATURE #1417: add uploaded files overview
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Models\File;
|
||||
|
||||
class FileManager
|
||||
{
|
||||
/**
|
||||
* @param File $file
|
||||
*
|
||||
* @return \Illuminate\Contracts\Routing\UrlGenerator|string
|
||||
*/
|
||||
public static function getPreview(File $file)
|
||||
{
|
||||
list($type, $subtype) = explode('/', $file->mime_type);
|
||||
|
||||
switch ($type) {
|
||||
case 'image':
|
||||
|
||||
// try to check if file have thumbnail
|
||||
if (file_exists(config('app.thumbnail_url') . '/' . $file->filename)) {
|
||||
$url = config('app.thumbnail_url') . '/' . $file->filename;
|
||||
} else {
|
||||
$url = config('app.upload_url') . '/' . $file->filename;
|
||||
}
|
||||
|
||||
return '<img src="' . $url . '" alt="' . $file->description . '" />';
|
||||
break;
|
||||
case 'text':
|
||||
return '<i class="fa fa-file-text-o"></i>';
|
||||
break;
|
||||
case 'application':
|
||||
switch ($subtype) {
|
||||
case 'pdf':
|
||||
return '<i class="fa fa-file-pdf-o"></i>';
|
||||
break;
|
||||
default:
|
||||
return '<i class="fa fa-newspaper-o"></i>';
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return '<i class="fa fa-question"></i>';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param File $file
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getFormattedSize(File $file)
|
||||
{
|
||||
$bytes = $file->file_size;
|
||||
|
||||
$label = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
|
||||
for ($i = 0; $bytes >= 1024 && $i < (count($label) - 1); $bytes /= 1024, $i++) {
|
||||
;
|
||||
}
|
||||
|
||||
return (round($bytes, 2) . " " . $label[$i]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user