Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel 9 support #36

Open
wants to merge 1 commit into
base: 2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
}
],
"require": {
"php": "^8.0.2",
"encore/laravel-admin": "*"
},
"require-dev": {
"phpunit/phpunit": "~6.0"
"phpunit/phpunit": "^9.5.20"
},
"autoload": {
"psr-4": {
Expand Down
140 changes: 59 additions & 81 deletions src/MediaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Encore\Admin\Exception\Handler;
use Encore\Admin\Extension;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Local\LocalFilesystemAdapter;

/**
* Class MediaManager.
Expand All @@ -22,7 +24,7 @@ class MediaManager extends Extension
protected $path = '/';

/**
* @var \Illuminate\Filesystem\FilesystemAdapter
* @var FilesystemAdapter
*/
protected $storage;

Expand All @@ -31,12 +33,12 @@ class MediaManager extends Extension
*/
protected $fileTypes = [
'image' => 'png|jpg|jpeg|tmp|gif',
'word' => 'doc|docx',
'ppt' => 'ppt|pptx',
'pdf' => 'pdf',
'code' => 'php|js|java|python|ruby|go|c|cpp|sql|m|h|json|html|aspx',
'zip' => 'zip|tar\.gz|rar|rpm',
'txt' => 'txt|pac|log|md',
'word' => 'doc|docx',
'ppt' => 'ppt|pptx',
'pdf' => 'pdf',
'code' => 'php|js|java|python|ruby|go|c|cpp|sql|m|h|json|html|aspx',
'zip' => 'zip|tar\.gz|rar|rpm',
'txt' => 'txt|pac|log|md',
'audio' => 'mp3|wav|flac|3pg|aa|aac|ape|au|m4a|mpc|ogg',
'video' => 'mkv|rmvb|flv|mp4|avi|wmv|rm|asf|mpeg',
];
Expand All @@ -46,7 +48,7 @@ class MediaManager extends Extension
*
* @param string $path
*/
public function __construct($path = '/')
public function __construct(string $path = '/')
{
$this->path = $path;

Expand All @@ -59,12 +61,12 @@ private function initStorage()

$this->storage = Storage::disk($disk);

if (!$this->storage->getDriver()->getAdapter() instanceof Local) {
if (!$this->storage->getAdapter() instanceof LocalFilesystemAdapter) {
Handler::error('Error', '[laravel-admin-ext/media-manager] only works for local storage.');
}
}

public function ls()
public function ls(): array
{
if (!$this->exists()) {
Handler::error('Error', "File or directory [$this->path] not exists");
Expand All @@ -83,37 +85,17 @@ public function ls()
})->all();
}

/**
* Get full path for a giving fiel path.
*
* @param string $path
*
* @return string
*/
protected function getFullPath($path)
{
return $this->storage->getDriver()->getAdapter()->applyPathPrefix($path);
}

public function download()
{
$fullPath = $this->getFullPath($this->path);

if (File::isFile($fullPath)) {
return response()->download($fullPath);
}

return response('', 404);
$this->storage->download($this->path);
}

public function delete($path)
public function delete($path): bool
{
$paths = is_array($path) ? $path : func_get_args();

foreach ($paths as $path) {
$fullPath = $this->getFullPath($path);

if (is_file($fullPath)) {
if ($this->storage->fileExists($path)) {
$this->storage->delete($path);
} else {
$this->storage->deleteDirectory($path);
Expand All @@ -123,18 +105,16 @@ public function delete($path)
return true;
}

public function move($new)
public function move($new): bool
{
return $this->storage->move($this->path, $new);
}

/**
* @param UploadedFile[] $files
* @param string $dir
*
* @return mixed
*/
public function upload($files = [])
public function upload(array $files = [])
{
foreach ($files as $file) {
$this->storage->putFileAs($this->path, $file, $file->getClientOriginalName());
Expand All @@ -143,78 +123,76 @@ public function upload($files = [])
return true;
}

public function newFolder($name)
public function newFolder($name): bool
{
$path = rtrim($this->path, '/').'/'.trim($name, '/');
$path = rtrim($this->path, '/') . '/' . trim($name, '/');

return $this->storage->makeDirectory($path);
}

public function exists()
public function exists(): bool
{
$path = $this->getFullPath($this->path);

return file_exists($path);
return $this->storage->exists($this->path);
}

/**
* @return array
*/
public function urls()
public function urls(): array
{
return [
'path' => $this->path,
'index' => route('media-index'),
'move' => route('media-move'),
'delete' => route('media-delete'),
'upload' => route('media-upload'),
'path' => $this->path,
'index' => route('media-index'),
'move' => route('media-move'),
'delete' => route('media-delete'),
'upload' => route('media-upload'),
'new-folder' => route('media-new-folder'),
];
}

public function formatFiles($files = [])
public function formatFiles($files = []): Collection
{
$files = array_map(function ($file) {
return [
'download' => route('media-download', compact('file')),
'icon' => '',
'name' => $file,
'preview' => $this->getFilePreview($file),
'isDir' => false,
'size' => $this->getFilesize($file),
'link' => route('media-download', compact('file')),
'url' => $this->storage->url($file),
'time' => $this->getFileChangeTime($file),
'download' => route('media-download', compact('file')),
'icon' => '',
'name' => $file,
'preview' => $this->getFilePreview($file),
'isDir' => false,
'size' => $this->getFilesize($file),
'link' => route('media-download', compact('file')),
'url' => $this->storage->url($file),
'time' => $this->getFileChangeTime($file),
];
}, $files);

return collect($files);
}

public function formatDirectories($dirs = [])
public function formatDirectories($dirs = []): Collection
{
$url = route('media-index', ['path' => '__path__', 'view' => request('view')]);

$preview = "<a href=\"$url\"><span class=\"file-icon text-aqua\"><i class=\"far fa-folder\"></i></span></a>";

$dirs = array_map(function ($dir) use ($preview) {
return [
'download' => '',
'icon' => '',
'name' => $dir,
'preview' => str_replace('__path__', $dir, $preview),
'isDir' => true,
'size' => '',
'link' => route('media-index', ['path' => '/'.trim($dir, '/'), 'view' => request('view')]),
'url' => $this->storage->url($dir),
'time' => $this->getFileChangeTime($dir),
'download' => '',
'icon' => '',
'name' => $dir,
'preview' => str_replace('__path__', $dir, $preview),
'isDir' => true,
'size' => '',
'link' => route('media-index', ['path' => '/' . trim($dir, '/'), 'view' => request('view')]),
'url' => $this->storage->url($dir),
'time' => $this->getFileChangeTime($dir),
];
}, $dirs);

return collect($dirs);
}

public function navigation()
public function navigation(): array
{
$folders = explode('/', $this->path);

Expand All @@ -225,23 +203,23 @@ public function navigation()
$navigation = [];

foreach ($folders as $folder) {
$path = rtrim($path, '/').'/'.$folder;
$path = rtrim($path, '/') . '/' . $folder;

$navigation[] = [
'name' => $folder,
'url' => route('media-index', ['path' => $path]),
'name' => $folder,
'url' => route('media-index', ['path' => $path]),
];
}

return $navigation;
}

public function getFilePreview($file)
public function getFilePreview($file): string
{
switch ($this->detectFileType($file)) {
case 'image':

if ($this->storage->getDriver()->getConfig()->has('url')) {
if ($this->storage->getConfig()['url']) {
$url = $this->storage->url($file);
$preview = "<span class=\"file-icon has-img\"><img src=\"$url\" alt=\"Attachment\"></span>";
} else {
Expand Down Expand Up @@ -297,22 +275,22 @@ protected function detectFileType($file)
return false;
}

public function getFilesize($file)
public function getFilesize($file): string
{
$bytes = filesize($this->getFullPath($file));
$bytes = $this->storage->size($file);

$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];

for ($i = 0; $bytes > 1024; $i++) {
$bytes /= 1024;
}

return round($bytes, 2).' '.$units[$i];
return round($bytes, 2) . ' ' . $units[$i];
}

public function getFileChangeTime($file)
public function getFileChangeTime(string $file): string
{
$time = filectime($this->getFullPath($file));
$time = $this->storage->lastModified($file);

return date('Y-m-d H:i:s', $time);
}
Expand Down