Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 50 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| FileManager | |
0.00% |
0 / 50 |
|
0.00% |
0 / 6 |
210 | |
0.00% |
0 / 1 |
| addMetaDataParameter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| generateFileName | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
30 | |||
| createMetaData | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| upload | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| delete | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| deleteDirectory | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Helpers\FileManagement\S3; |
| 4 | |
| 5 | use App\Http\Helpers\FileManagement\S3\model\FileMetaData; |
| 6 | use App\Http\Models\Auth\AdminUser; |
| 7 | use Illuminate\Support\Facades\Storage; |
| 8 | |
| 9 | abstract class FileManager |
| 10 | { |
| 11 | const IMAGES = 'images'; |
| 12 | |
| 13 | const THUMBNAILS = 'thumbnails'; |
| 14 | |
| 15 | const DOCUMENTS = 'documents'; |
| 16 | |
| 17 | const OTHERS = 'others'; |
| 18 | |
| 19 | private $storagePath = 'romeo'; |
| 20 | |
| 21 | private $metaData = []; |
| 22 | |
| 23 | public function addMetaDataParameter(array $metaData) |
| 24 | { |
| 25 | $this->metaData = array_merge($this->metaData, $metaData); |
| 26 | } |
| 27 | |
| 28 | private function generateFileName($file) |
| 29 | { |
| 30 | $user = request()->user(); |
| 31 | $this->addMetaDataParameter(['user_id' => $user->getKey()]); |
| 32 | if ($user instanceof AdminUser) { |
| 33 | $prefix = 'admin'; |
| 34 | } else { |
| 35 | $prefix = $user->email; |
| 36 | } |
| 37 | |
| 38 | $key = strtoupper(uniqid(date('Ymd'))); |
| 39 | if (get_called_class() === ImageManager::class) { |
| 40 | $fileName = $prefix.'/'.static::IMAGES.'/IMG'.$key; |
| 41 | $this->addMetaDataParameter(['category' => static::IMAGES]); |
| 42 | } elseif (get_called_class() === ThumbnailManager::class) { |
| 43 | $fileName = $prefix.'/'.static::THUMBNAILS.'/TIMG'.$key; |
| 44 | $this->addMetaDataParameter(['category' => static::THUMBNAILS]); |
| 45 | } elseif (get_called_class() === DocumentManager::class) { |
| 46 | $fileName = $prefix.'/'.static::DOCUMENTS.'/DOC'.$key; |
| 47 | $this->addMetaDataParameter(['category' => static::DOCUMENTS]); |
| 48 | } else { |
| 49 | $fileName = $prefix.'/'.static::OTHERS.'/OTH'.$key; |
| 50 | $this->addMetaDataParameter(['category' => static::OTHERS]); |
| 51 | } |
| 52 | |
| 53 | $fileName .= '.'.$file->getClientOriginalExtension(); |
| 54 | |
| 55 | return $fileName; |
| 56 | } |
| 57 | |
| 58 | private function createMetaData($file, $fileName) |
| 59 | { |
| 60 | $metaData = [ |
| 61 | 'name' => basename($fileName), |
| 62 | 'original_name' => $file->getClientOriginalName(), |
| 63 | 'size' => $file->getSize(), |
| 64 | 'type' => $file->getClientMimeType(), |
| 65 | 'extension' => $file->getClientOriginalExtension(), |
| 66 | 'path' => $this->storagePath.'/'.$fileName, |
| 67 | ]; |
| 68 | $metaData = array_merge($metaData, $this->metaData); |
| 69 | |
| 70 | return FileMetadata::create($metaData); |
| 71 | } |
| 72 | |
| 73 | public function upload($file, $public = true) |
| 74 | { |
| 75 | $fileName = $this->generateFileName($file); |
| 76 | $path = $this->storagePath.'/'.$fileName; |
| 77 | $result = Storage::disk('s3')->put($path, file_get_contents($file), $public ? 'public' : []); |
| 78 | if (! $result) { |
| 79 | response()->json(['error' => 'Unable to upload file']); |
| 80 | } |
| 81 | $url = Storage::disk('s3')->url($path); |
| 82 | $this->addMetaDataParameter([ |
| 83 | 'public_access' => $public, |
| 84 | 'url' => $url, |
| 85 | ]); |
| 86 | $result = $this->createMetaData($file, $fileName); |
| 87 | |
| 88 | return $result; |
| 89 | } |
| 90 | |
| 91 | public function delete(array $paths) |
| 92 | { |
| 93 | // $paths = array_map(function ($path) { |
| 94 | // return substr($path, strpos($path, $this->storagePath)); |
| 95 | // }, $paths); |
| 96 | |
| 97 | $result = Storage::disk('s3')->delete($paths); |
| 98 | if ($result) { |
| 99 | FileMetadata::whereIn('path', $paths)->delete(); |
| 100 | } |
| 101 | |
| 102 | return $result; |
| 103 | } |
| 104 | |
| 105 | public function deleteDirectory($directory, $userId) |
| 106 | { |
| 107 | $result = Storage::disk('s3')->deleteDirectory($directory); |
| 108 | |
| 109 | if ($result) { |
| 110 | FileMetadata::whereIn('user_id', $userId)->delete(); |
| 111 | } |
| 112 | |
| 113 | return $result; |
| 114 | } |
| 115 | } |