From a1530401605a8b563a07645a43ef178fde162977 Mon Sep 17 00:00:00 2001 From: mishu Date: Sun, 19 Mar 2017 18:44:35 +0200 Subject: [PATCH] first commit --- LICENSE | 21 ++++++ README.md | 6 ++ composer.json | 22 +++++++ src/FileManager.php | 130 ++++++++++++++++++++++++++++++++++++++ src/FileManagerStatus.php | 18 ++++++ src/FileWrapper.php | 41 ++++++++++++ 6 files changed, 238 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/FileManager.php create mode 100644 src/FileManagerStatus.php create mode 100644 src/FileWrapper.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a8fe188 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 laravel-enso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..777588d --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# File Manager +File manager library for upload, download etc. + +## Upgrade from laravel-enso v2 + +Correct all the includes for the helper classes included in this package diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..bafff01 --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "name": "laravel-enso/filemanager", + "description": "File manager library for upload, download etc.", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "mishu", + "email": "mishu@xtelecom.ro" + } + ], + "require": { + "php": ">=5.6.4", + "laravel/framework": "5.4.*", + "laravel-enso/helpers": "1.0.*" + }, + "autoload": { + "psr-4": { + "LaravelEnso\\FileManager\\": "src/" + } + } +} diff --git a/src/FileManager.php b/src/FileManager.php new file mode 100644 index 0000000..9dc9b23 --- /dev/null +++ b/src/FileManager.php @@ -0,0 +1,130 @@ +uploadedFiles = collect(); + $this->path = $path; + $this->status = new FileManagerStatus; + } + + /** Starts the 2 step upload process for a list of files + * + * @param $request - array of files + */ + public function startUpload($request) { + + foreach ($request as $file) { + + if (!$file->isValid()) { + + $this->logError($file); + } + + $this->uploadToTemp($file); + } + + $this->setStatus(__('Upload')); + } + + /** Starts the upload process for a single file. Method might be called multiple times if needed, followed by + * by a single commitUpload call, that will complete the upload for all given files. + * + * @param $file + */ + public function startSingleFileUpload($file) { + + if (!$file->isValid()) { + + $this->logError($file); + } + + $this->uploadToTemp($file); + $this->setStatus(__('Upload')); + } + + public function commitUpload() { + + $this->uploadedFiles->each(function ($uploadedFile) { + + \Storage::move(env('TEMP_PATH') . '/' . $uploadedFile['saved_name'], + $this->path . '/' . $uploadedFile['saved_name']); + }); + + return $this->status; + } + + public function delete(String $fileName) { + + \Storage::delete($this->path . '/' . $fileName); + $this->setStatus(__('Delete')); + + return $this->status; + } + + /** Load file from disk and give it back within a wrapper containing also mimeType + * + * @param String $fileSavedName + * + * @return FileWrapper + */ + public function getFile(String $fileSavedName) { + + $file = \Storage::get($this->path . '/' . $fileSavedName); + $mimeType = \Storage::getMimeType($this->path . '/' . $fileSavedName); + + return new FileWrapper($file, $mimeType); + } + + public function getStatus() { + + return $this->status; + } + + /************* private functions **************/ + + private function setStatus(String $operation) { + + $this->status->level = $this->status->errors->count() ? 'error' : 'success'; + $this->status->message = $this->status->errors->count() ? $operation . ' encountered ' . $this->status->errors->count() . ' errors' + : $operation . ' was successfull'; + } + + private function logError($file) { + + $this->status->errors->push([ + + 'error' => __('File is not valid'), + 'file' => $file, + ]); + } + + //TODO on uploadCommit, we should probably cleanup the object for reuse + private function cleanupOnUploadCommit() { + + $this->status = null; + $this->uploadedFiles = collect(); + } + + private function uploadToTemp($file) { + + $fileName = $file->getClientOriginalName(); + $fileSavedName = md5($fileName . \Date::now()) . '.' . $file->getClientOriginalExtension(); + $fileSize = $file->getClientSize(); + $file->move(storage_path('app/' . env('TEMP_PATH')), $fileSavedName); + + $this->uploadedFiles->push([ + 'original_name' => $fileName, + 'saved_name' => $fileSavedName, + 'size' => $fileSize, + ]); + } +} diff --git a/src/FileManagerStatus.php b/src/FileManagerStatus.php new file mode 100644 index 0000000..9470658 --- /dev/null +++ b/src/FileManagerStatus.php @@ -0,0 +1,18 @@ +errors = collect(); + } +} + diff --git a/src/FileWrapper.php b/src/FileWrapper.php new file mode 100644 index 0000000..ec45cac --- /dev/null +++ b/src/FileWrapper.php @@ -0,0 +1,41 @@ +file = $file; + $this->statusCode = 200; + $this->mimeType = $mimeType; + } + + public function getDownloadResponse() { + + return $this->getResponse('attachment'); + } + + public function getInlineResponse() { + + return $this->getResponse('inline'); + } + + private function getResponse(String $contentDisposition) + { + return response()->make($this->file, $this->statusCode, [ + + 'Content-Type' => $this->mimeType, + 'Content-Disposition' => $contentDisposition . '; filename="' . $this->originalName . '"', + ]); + } +}