Files
core/app/Helpers/FileManager.php
T
Michal Kváček 1a8b07971e FEATURE #1442: add uploaded file detail
This commit adds functionality for editing keywords and description of selected file. This commit also includes JS library for syntax highlighting (https://highlighjs.org)
2016-02-08 19:55:09 +01:00

140 lines
3.8 KiB
PHP

<?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]);
}
/**
* Get preview for file detail
*
* @param File $file
*
* @return \Illuminate\Contracts\Routing\UrlGenerator|string
*/
public static function getLargePreview(File $file)
{
list($type, $subtype) = explode('/', $file->mime_type);
$url = config('app.upload_url') . '/' . $file->filename;
$path = config('app.upload_path') . DIRECTORY_SEPARATOR . $file->filename;
switch ($type) {
case 'image':
return '<img src="' . $url . '" alt="' . $file->description . '" />';
break;
case 'text':
$string = '<pre><code>';
$string .= htmlspecialchars(file_get_contents($path));
$string .= '</pre></code>';
return $string;
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 self::getPreview($file);
break;
}
}
/**
* @param File $file
*
* @return array
*/
public static function getAdditionalInfo(File $file)
{
list($type, $subtype) = explode('/', $file->mime_type);
$path = config('app.upload_path') . DIRECTORY_SEPARATOR . $file->filename;
switch ($type) {
case 'text':
return [
trans('admin/fileManager.encoding') => mb_detect_encoding(file_get_contents($path))
];
break;
case 'image':
list ($width, $height) = getimagesize($path);
return [
trans('admin/fileManager.resolution') => $height . ' x ' . $width,
];
break;
}
return [];
}
}