|
| 1 | +<?php namespace Tsawler\Laravelfilemanager\controllers; |
| 2 | + |
| 3 | +use App\Http\Controllers\Controller; |
| 4 | +use Illuminate\Support\Facades\Config; |
| 5 | +use Illuminate\Support\Facades\File; |
| 6 | +use Illuminate\Support\Facades\Input; |
| 7 | +use Illuminate\Support\Facades\Redirect; |
| 8 | +use Illuminate\Support\Str; |
| 9 | +use Illuminate\Support\Facades\Session; |
| 10 | +use Illuminate\Support\Facades\View; |
| 11 | +use Intervention\Image\Facades\Image; |
| 12 | + |
| 13 | +/** |
| 14 | + * Class ItemsController |
| 15 | + * @package Tsawler\Laravelfilemanager\controllers |
| 16 | + */ |
| 17 | +class ItemsController extends Controller { |
| 18 | + |
| 19 | + |
| 20 | + /** |
| 21 | + * @var |
| 22 | + */ |
| 23 | + protected $file_location; |
| 24 | + |
| 25 | + |
| 26 | + /** |
| 27 | + * constructor |
| 28 | + */ |
| 29 | + function __construct() |
| 30 | + { |
| 31 | + if (Session::get('lfm_type') == "Images") |
| 32 | + $this->file_location = Config::get('lfm.images_dir'); |
| 33 | + else |
| 34 | + $this->file_location = Config::get('lfm.files_dir'); |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + public function getFiles() |
| 39 | + { |
| 40 | + return "List of files"; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Get the images to load for a selected folder |
| 46 | + * |
| 47 | + * @return mixed |
| 48 | + */ |
| 49 | + public function getImages() |
| 50 | + { |
| 51 | + if (Input::has('base')) |
| 52 | + { |
| 53 | + $files = File::files(base_path($this->file_location . Input::get('base'))); |
| 54 | + $all_directories = File::directories(base_path($this->file_location . Input::get('base'))); |
| 55 | + } else |
| 56 | + { |
| 57 | + $files = File::files(base_path($this->file_location)); |
| 58 | + $all_directories = File::directories(base_path($this->file_location)); |
| 59 | + } |
| 60 | + |
| 61 | + $directories = []; |
| 62 | + |
| 63 | + foreach ($all_directories as $directory) |
| 64 | + { |
| 65 | + if (basename($directory) != "thumbs") |
| 66 | + { |
| 67 | + $directories[] = basename($directory); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + $file_info = []; |
| 72 | + |
| 73 | + foreach ($files as $file) |
| 74 | + { |
| 75 | + $file_name = $file; |
| 76 | + $file_size = number_format((Image::make($file)->filesize() / 1024), 2, ".", ""); |
| 77 | + if ($file_size > 1000) |
| 78 | + { |
| 79 | + $file_size = number_format((Image::make($file)->filesize() / 1024), 2, ".", "") . " Mb"; |
| 80 | + } else |
| 81 | + { |
| 82 | + $file_size = $file_size . " Kb"; |
| 83 | + } |
| 84 | + $file_created = filemtime($file); |
| 85 | + $file_type = Image::make($file)->mime(); |
| 86 | + $file_info[] = [ |
| 87 | + 'name' => $file_name, |
| 88 | + 'size' => $file_size, |
| 89 | + 'created' => $file_created, |
| 90 | + 'type' => $file_type |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + if ((Session::has('lfm_type')) && (Session::get('lfm_type') == "Images")) |
| 95 | + $dir_location = Config::get('lfm.images_url'); |
| 96 | + else |
| 97 | + $dir_location = Config::get('lfm.files_url'); |
| 98 | + |
| 99 | + if (Input::get('show_list') == 1) |
| 100 | + { |
| 101 | + return View::make('laravel-filemanager::images-list') |
| 102 | + ->with('directories', $directories) |
| 103 | + ->with('base', Input::get('base')) |
| 104 | + ->with('file_info', $file_info) |
| 105 | + ->with('dir_location', $dir_location); |
| 106 | + } else |
| 107 | + { |
| 108 | + return View::make('laravel-filemanager::images') |
| 109 | + ->with('files', $files) |
| 110 | + ->with('directories', $directories) |
| 111 | + ->with('base', Input::get('base')) |
| 112 | + ->with('dir_location', $dir_location); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments