-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a153040
Showing
6 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "laravel-enso/filemanager", | ||
"description": "File manager library for upload, download etc.", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "mishu", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.6.4", | ||
"laravel/framework": "5.4.*", | ||
"laravel-enso/helpers": "1.0.*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"LaravelEnso\\FileManager\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace LaravelEnso\FileManager; | ||
|
||
class FileManager { | ||
|
||
public $uploadedFiles; | ||
private $path; | ||
private $status = null; | ||
|
||
|
||
public function __construct($path) { | ||
|
||
$this->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, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace LaravelEnso\FileManager; | ||
|
||
use LaravelEnso\Helpers\Classes\AbstractObject; | ||
|
||
class FileManagerStatus extends AbstractObject | ||
{ | ||
public $level; | ||
public $message; | ||
public $errors; | ||
|
||
public function __construct() | ||
{ | ||
$this->errors = collect(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace LaravelEnso\FileManager; | ||
|
||
use LaravelEnso\Helpers\Classes\AbstractObject; | ||
|
||
class FileWrapper extends AbstractObject | ||
{ | ||
|
||
public $file; | ||
public $originalName; | ||
public $statusCode; | ||
public $mimeType; | ||
|
||
public function __construct($file, String $mimeType) | ||
{ | ||
|
||
$this->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 . '"', | ||
]); | ||
} | ||
} |