Skip to content

Commit a153040

Browse files
committed
first commit
0 parents  commit a153040

File tree

6 files changed

+238
-0
lines changed

6 files changed

+238
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 laravel-enso
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# File Manager
2+
File manager library for upload, download etc.
3+
4+
## Upgrade from laravel-enso v2
5+
6+
Correct all the includes for the helper classes included in this package

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "laravel-enso/filemanager",
3+
"description": "File manager library for upload, download etc.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "mishu",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.6.4",
14+
"laravel/framework": "5.4.*",
15+
"laravel-enso/helpers": "1.0.*"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"LaravelEnso\\FileManager\\": "src/"
20+
}
21+
}
22+
}

src/FileManager.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace LaravelEnso\FileManager;
4+
5+
class FileManager {
6+
7+
public $uploadedFiles;
8+
private $path;
9+
private $status = null;
10+
11+
12+
public function __construct($path) {
13+
14+
$this->uploadedFiles = collect();
15+
$this->path = $path;
16+
$this->status = new FileManagerStatus;
17+
}
18+
19+
/** Starts the 2 step upload process for a list of files
20+
*
21+
* @param $request - array of files
22+
*/
23+
public function startUpload($request) {
24+
25+
foreach ($request as $file) {
26+
27+
if (!$file->isValid()) {
28+
29+
$this->logError($file);
30+
}
31+
32+
$this->uploadToTemp($file);
33+
}
34+
35+
$this->setStatus(__('Upload'));
36+
}
37+
38+
/** Starts the upload process for a single file. Method might be called multiple times if needed, followed by
39+
* by a single commitUpload call, that will complete the upload for all given files.
40+
*
41+
* @param $file
42+
*/
43+
public function startSingleFileUpload($file) {
44+
45+
if (!$file->isValid()) {
46+
47+
$this->logError($file);
48+
}
49+
50+
$this->uploadToTemp($file);
51+
$this->setStatus(__('Upload'));
52+
}
53+
54+
public function commitUpload() {
55+
56+
$this->uploadedFiles->each(function ($uploadedFile) {
57+
58+
\Storage::move(env('TEMP_PATH') . '/' . $uploadedFile['saved_name'],
59+
$this->path . '/' . $uploadedFile['saved_name']);
60+
});
61+
62+
return $this->status;
63+
}
64+
65+
public function delete(String $fileName) {
66+
67+
\Storage::delete($this->path . '/' . $fileName);
68+
$this->setStatus(__('Delete'));
69+
70+
return $this->status;
71+
}
72+
73+
/** Load file from disk and give it back within a wrapper containing also mimeType
74+
*
75+
* @param String $fileSavedName
76+
*
77+
* @return FileWrapper
78+
*/
79+
public function getFile(String $fileSavedName) {
80+
81+
$file = \Storage::get($this->path . '/' . $fileSavedName);
82+
$mimeType = \Storage::getMimeType($this->path . '/' . $fileSavedName);
83+
84+
return new FileWrapper($file, $mimeType);
85+
}
86+
87+
public function getStatus() {
88+
89+
return $this->status;
90+
}
91+
92+
/************* private functions **************/
93+
94+
private function setStatus(String $operation) {
95+
96+
$this->status->level = $this->status->errors->count() ? 'error' : 'success';
97+
$this->status->message = $this->status->errors->count() ? $operation . ' encountered ' . $this->status->errors->count() . ' errors'
98+
: $operation . ' was successfull';
99+
}
100+
101+
private function logError($file) {
102+
103+
$this->status->errors->push([
104+
105+
'error' => __('File is not valid'),
106+
'file' => $file,
107+
]);
108+
}
109+
110+
//TODO on uploadCommit, we should probably cleanup the object for reuse
111+
private function cleanupOnUploadCommit() {
112+
113+
$this->status = null;
114+
$this->uploadedFiles = collect();
115+
}
116+
117+
private function uploadToTemp($file) {
118+
119+
$fileName = $file->getClientOriginalName();
120+
$fileSavedName = md5($fileName . \Date::now()) . '.' . $file->getClientOriginalExtension();
121+
$fileSize = $file->getClientSize();
122+
$file->move(storage_path('app/' . env('TEMP_PATH')), $fileSavedName);
123+
124+
$this->uploadedFiles->push([
125+
'original_name' => $fileName,
126+
'saved_name' => $fileSavedName,
127+
'size' => $fileSize,
128+
]);
129+
}
130+
}

src/FileManagerStatus.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace LaravelEnso\FileManager;
4+
5+
use LaravelEnso\Helpers\Classes\AbstractObject;
6+
7+
class FileManagerStatus extends AbstractObject
8+
{
9+
public $level;
10+
public $message;
11+
public $errors;
12+
13+
public function __construct()
14+
{
15+
$this->errors = collect();
16+
}
17+
}
18+

src/FileWrapper.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace LaravelEnso\FileManager;
4+
5+
use LaravelEnso\Helpers\Classes\AbstractObject;
6+
7+
class FileWrapper extends AbstractObject
8+
{
9+
10+
public $file;
11+
public $originalName;
12+
public $statusCode;
13+
public $mimeType;
14+
15+
public function __construct($file, String $mimeType)
16+
{
17+
18+
$this->file = $file;
19+
$this->statusCode = 200;
20+
$this->mimeType = $mimeType;
21+
}
22+
23+
public function getDownloadResponse() {
24+
25+
return $this->getResponse('attachment');
26+
}
27+
28+
public function getInlineResponse() {
29+
30+
return $this->getResponse('inline');
31+
}
32+
33+
private function getResponse(String $contentDisposition)
34+
{
35+
return response()->make($this->file, $this->statusCode, [
36+
37+
'Content-Type' => $this->mimeType,
38+
'Content-Disposition' => $contentDisposition . '; filename="' . $this->originalName . '"',
39+
]);
40+
}
41+
}

0 commit comments

Comments
 (0)