helper = $lfm; } public function __get($var_name) { if ($var_name == 'storage') { return $this->helper->getStorage($this->path('url')); } } public function __call($function_name, $arguments) { return $this->storage->$function_name(...$arguments); } public function dir($working_dir) { $this->working_dir = $working_dir; return $this; } public function thumb($is_thumb = true) { $this->is_thumb = $is_thumb; return $this; } public function setName($item_name) { $this->item_name = $item_name; return $this; } public function setModel() { $parent_folder_id = $this->getModelParentFolderId(); if ($this->isDirectory()) { if($this->folder_model == null){ if($parent_folder_id){ $this->folder_model = IQContentFolder::where('name', $this->item_name)->where('folder_id', $parent_folder_id)->first(); } else{ $this->folder_model = new IQContentFolder(); } } }else{ if($this->file_model == null){ if($parent_folder_id){ $this->file_model = IQContentFile::where('name', $this->item_name)->where('folder_id', $parent_folder_id)->first(); }else{ $this->file_model = new IQContentFile(); } } } return $this; } public function getName() { return $this->item_name; } public function getModel() { if ($this->isDirectory()) { return $this->folder_model; }else{ return $this->file_model; } } public function getModelParentFolderId(){ $parent_folder = $this->getModelFolderByPath(); if($parent_folder) { return $parent_folder->id; } return null; } public function getModelFolderByPath($parent = false){ $working_dir = $this->path('working_dir'); $working_dir = substr($working_dir, 0, strrpos($working_dir, '/')); $dirs = explode( "/", $working_dir); $folder_id = null; $folder = null; foreach ($dirs as $dir){ $folder = IQContentFolder::where('name', $dir)->where('folder_id', $folder_id)->first(); if($folder){ $folder_id = $folder->id; $this->parent_dir = $folder; } } return $this->parent_dir; } public function path($type = 'storage') { if ($type == 'working_dir') { // working directory: /{user_slug} return $this->translateToLfmPath($this->normalizeWorkingDir()); } elseif ($type == 'url') { // storage: files/{user_slug} return $this->helper->getCategoryName() . $this->path('working_dir'); } elseif ($type == 'storage') { // storage: files/{user_slug} // storage on windows: files\{user_slug} return $this->translateToOsPath($this->path('url')); } elseif ($type == 'dirname') { return $this->helper->getDirFromPath($this->path('storage')); } else { // absolute: /var/www/html/project/storage/app/files/{user_slug} // absolute on windows: C:\project\storage\app\files\{user_slug} return $this->storage->rootPath() . $this->path('storage'); } } public function translateToLfmPath($path) { return str_replace($this->helper->ds(), Lfm::DS, $path); } public function translateToOsPath($path) { return str_replace(Lfm::DS, $this->helper->ds(), $path); } public function url() { return $this->storage->url($this->path('url')); } public function folders() { $all_folders = array_map(function ($directory_path) { return $this->pretty($directory_path); }, $this->storage->directories()); $folders = array_filter($all_folders, function ($directory) { return $directory->name !== $this->helper->getThumbFolderName(); }); return $this->sortByColumn($folders); } public function recrusiveFolders($parent){ $folders = $this->folders(); $b = []; foreach ($folders as $folder){ $b[$folder->name()] = $folder; $lfm = $folder->getLfm(); $lfm->dir($parent); $a = $lfm->recrusiveFolders($folder->url()); $b[$folder->name()."-childs"] = $a; } return $b; } public function files() { $files = array_map(function ($file_path) { return $this->pretty($file_path); }, $this->storage->files()); return $this->sortByColumn($files); } public function pretty($item_path) { return Container::getInstance()->makeWith(LfmItem::class, [ 'lfm' => (clone $this) ->setName($this->helper->getNameFromPath($item_path)) ->setModel(), 'helper' => $this->helper, ]); } public function delete() { if ($this->isDirectory()) { return $this->storage->deleteDirectory(); } else { return $this->storage->delete(); } } /** * Create folder if not exist. * * @param string $path Real path of a directory. * @return bool */ public function createFolder() { if ($this->storage->exists($this)) { return false; } $parent_folder_id = $this->getModelParentFolderId(); $this->storage->makeDirectory(0777, true, true); if(!$this->is_thumb){ IQContentFolder::create([ 'folder_id' => $parent_folder_id, 'name' => $this->item_name, 'identifier' => $this->item_name, ]); } } public function isDirectory() { if($this->isDirectory !== null){ return $this->isDirectory; } $working_dir = $this->path('working_dir'); $parent_dir = substr($working_dir, 0, strrpos($working_dir, '/')); $parent_directories = array_map(function ($directory_path) { return app(static::class)->translateToLfmPath($directory_path); }, app(static::class)->dir($parent_dir)->directories()); $this->isDirectory = in_array($this->path('url'), $parent_directories); return $this->isDirectory; } /** * Check a folder and its subfolders is empty or not. * * @param string $directory_path Real path of a directory. * @return bool */ public function directoryIsEmpty() { return count($this->storage->allFiles()) == 0; } public function normalizeWorkingDir() { $path = $this->working_dir ?: $this->helper->input('working_dir') ?: $this->helper->getRootFolder(); if ($this->is_thumb) { $path .= Lfm::DS . $this->helper->getThumbFolderName(); } if ($this->getName()) { $path .= Lfm::DS . $this->getName(); } return $path; } /** * Sort files and directories. * * @param mixed $arr_items Array of files or folders or both. * @return array of object */ public function sortByColumn($arr_items) { $sort_by = $this->helper->input('sort_type'); if (in_array($sort_by, ['name', 'time'])) { $key_to_sort = $sort_by; } else { $key_to_sort = 'name'; } uasort($arr_items, function ($a, $b) use ($key_to_sort) { return strcmp($a->{$key_to_sort}, $b->{$key_to_sort}); }); return $arr_items; } public function error($error_type, $variables = []) { return $this->helper->error($error_type, $variables); } // Upload section public function upload($file) { $error = $this->uploadValidator($file); if($error !== 'pass'){ return false; } $new_file_name = $this->getNewName($file); $new_file_path = $this->setName($new_file_name)->path('absolute'); $working_folder_id = $this->getModelParentFolderId(); //event(new ImageIsUploading($new_file_path)); try { $new_file_name = $this->saveFile($file, $new_file_name); } catch (\Exception $e) { \Log::info($e); return $this->error('invalid'); } IQContentFile::create([ 'folder_id' => $working_folder_id, 'name' => $new_file_name, 'identifier' => $new_file_name, 'ext' => $file->guessClientExtension(), 'mine' => $file->getMimeType(), 'size' => $file->getSize() / 1000, 'dimensions' => $this->image_dimensions, 'content' => '', ]); // TODO should be "FileWasUploaded" // event(new ImageWasUploaded($new_file_path)); return $new_file_name; } private function uploadValidator($file) { if (empty($file)) { return $this->error('file-empty'); } elseif (! $file instanceof UploadedFile) { return $this->error('instance'); } elseif ($file->getError() == UPLOAD_ERR_INI_SIZE) { return $this->error('file-size', ['max' => ini_get('upload_max_filesize')]); } elseif ($file->getError() != UPLOAD_ERR_OK) { throw new \Exception('File failed to upload. Error code: ' . $file->getError()); } $new_file_name = $this->getNewName($file); if ($this->setName($new_file_name)->exists() && !config('lfm.over_write_on_duplicate')) { return $this->error('file-exist'); } if (config('lfm.should_validate_mime', false)) { $mimetype = $file->getMimeType(); if (false === in_array($mimetype, $this->helper->availableMimeTypes())) { return $this->error('mime') . $mimetype; } } if (config('lfm.should_validate_size', false)) { // size to kb unit is needed $file_size = $file->getSize() / 1000; if ($file_size > $this->helper->maxUploadSize()) { return $this->error('size') . $file_size; } } return 'pass'; } private function getNewName($file) { $new_file_name = $this->helper ->translateFromUtf8(trim(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME))); if (config('lfm.rename_file') === true) { $new_file_name = uniqid(); } elseif (config('lfm.alphanumeric_filename') === true) { $new_file_name = $this->helper->sanitize($new_file_name); //preg_replace('/[^A-Za-z0-9\-\']/', '_', $new_file_name); } $extension = $file->getClientOriginalExtension(); if ($extension) { $new_file_name .= '.' . $extension; } return $new_file_name; } private function saveFile($file, $new_file_name) { $this->setName($new_file_name)->storage->save($file); $this->makeThumbnail($new_file_name); return $new_file_name; } public function makeThumbnail($file_name) { $original_image = $this->pretty($file_name); if (!$original_image->shouldCreateThumb()) { return; } // create folder for thumbnails $this->setName(null)->thumb(true)->createFolder(); // generate cropped image content $this->setName($file_name)->thumb(true); $image = Image::make($original_image->get()); $this->image_dimensions = $image->width()."x".$image->height(); $image->fit(config('lfm.thumb_img_width', 200), config('lfm.thumb_img_height', 200)); $this->storage->put($image->stream()->detach()); } public function makeThumbnailURL($file_name, $url) { ini_set("allow_url_fopen", 1); // create folder for thumbnails $this->setName(null)->thumb(true)->createFolder(); // generate cropped image content $this->setName($file_name)->thumb(true); $image = Image::make(file_get_contents($url)); // $this->image_dimensions = $image->width()."x".$image->height(); $image->fit(config('lfm.thumb_img_width', 200), config('lfm.thumb_img_height', 200)); $this->storage->put($image->stream()->detach()); } }