1+ <?php
2+ /**
3+ * Author: Habib urRehman
4+ * Email : chaudryhabib2@gmail.com
5+ */
6+ namespace OldRavian \FileUploader \Abstracts ;
7+
8+ use OldRavian \FileUploader \Contracts \FileUploaderContract ;
9+ use Illuminate \Http \UploadedFile ;
10+ abstract class AbstractFileUploader implements FileUploaderContract
11+ {
12+ /**
13+ * @var $uploadError
14+ */
15+ public $ uploadError ;
16+
17+ /**
18+ * Default settings
19+ *
20+ */
21+ protected $ settings ;
22+
23+ public function __construct ()
24+ {
25+ $ this ->initializeDefaults ();
26+ }
27+
28+ /**
29+ * Initialize default settings
30+ *
31+ * @return void
32+ */
33+ protected function initializeDefaults ()
34+ {
35+ $ this ->settings = config ('old-ravian-file-uploader ' );
36+ }
37+
38+ /**
39+ * Handle the uploaded file
40+ *
41+ * @param Illuminate\Http\UploadedFile|string $target
42+ * @param array $settings
43+ *
44+ * @return array|false
45+ */
46+ public function upload ($ target , array $ settings = [])
47+ {
48+ $ settings = array_merge ($ this ->settings , $ settings );
49+
50+ if ($ this ->uploadValidate ($ target , $ settings )) {
51+ return $ this ->storeFile ($ target , $ settings );
52+ }
53+
54+ return false ;
55+ }
56+
57+ /**
58+ * @param Illuminate\Http\UploadedFile|string $target
59+ * @param array $settings
60+ * @return array|false
61+ */
62+ public function uploadMany (array $ targets , array $ settings = []){
63+ $ arrs =[];
64+ foreach ($ targets as $ target ){
65+ $ res = $ this ->upload ($ target , $ settings );
66+ if ($ res ===false ){
67+ return false ;
68+ }
69+ $ arrs [] = $ res ;
70+ }
71+ return $ arrs ;
72+ }
73+
74+ /**
75+ * Validate the uploaded file for extension & size
76+ *
77+ * @return bool
78+ */
79+ protected function uploadValidate (UploadedFile |string $ uploadedFile , $ settings ){
80+ if (!$ this ->isValid ($ uploadedFile )) {
81+ return false ;
82+ }
83+
84+ if (!in_array ($ this ->getExtension ($ uploadedFile ), $ settings ['allowed_extensions ' ])) {
85+ $ this ->uploadError = "Extension {$ this ->getExtension ($ uploadedFile )} is not allowed " ;
86+ return false ;
87+ }
88+
89+ if ($ this ->getFileSize ($ uploadedFile ) > $ settings ['max_file_size ' ]) {
90+ $ this ->uploadError = "Input file cannot exceed {$ settings ['max_file_size ' ]} bytes " ;
91+ return false ;
92+ }
93+
94+ return true ;
95+ }
96+
97+ protected function isValid (UploadedFile |string $ uploadedFile ){
98+ return true ;
99+ }
100+
101+ /**
102+ * Return the file extension as extracted from the origin file name
103+ *
104+ * @param Illuminate\Http\UploadedFile|string $uploadedFile
105+ *
106+ * @return string
107+ */
108+ abstract protected function getExtension (UploadedFile |string $ uploadedFile );
109+
110+ /**
111+ * Return the file size
112+ *
113+ * @param Illuminate\Http\UploadedFile|string $uploadedFile
114+ *
115+ * @return double
116+ */
117+ abstract protected function getFileSize (UploadedFile |string $ uploadedFile );
118+
119+ /**
120+ * Generate an unique name for storing file
121+ *
122+ * @param Illuminate\Http\UploadedFile|string $uploadedFile
123+ *
124+ * @return string
125+ */
126+ protected function getUniqueName (UploadedFile |string $ uploadedFile ){
127+ $ originName = $ this ->getOriginName ($ uploadedFile );
128+ $ uniqueString = uniqid (rand (), true )."_ " .$ originName ."_ " .getmypid ()."_ " .gethostname ()."_ " .time ();
129+ return md5 ($ uniqueString ).". " .$ this ->getExtension ($ uploadedFile );
130+ }
131+
132+ /**
133+ * Return the original filename
134+ *
135+ * @param Illuminate\Http\UploadedFile|string $uploadedFile
136+ *
137+ * @return string
138+ */
139+ protected function getOriginName (UploadedFile |string $ uploadedFile ){
140+ return "" .$ this ->getExtension ($ uploadedFile );
141+ }
142+
143+
144+ /**
145+ * Physically store the uploaded file
146+ *
147+ * @param Illuminate\Http\UploadedFile|string $uploadedFile
148+ * @param array $settings
149+ *
150+ * @return array
151+ */
152+ abstract protected function storeFile (UploadedFile |string $ uploadedFile , $ settings );
153+ }
0 commit comments