33use CodeIgniter \Files \File as CIFile ;
44use CodeIgniter \Model ;
55use Tatter \Files \Entities \File ;
6+ use Tatter \Files \Exceptions \FilesException ;
67use Tatter \Thumbnails \Exceptions \ThumbnailsException ;
78
89class FileModel extends Model
@@ -45,6 +46,35 @@ class FileModel extends Model
4546
4647 //--------------------------------------------------------------------
4748
49+ /**
50+ * Normalizes and creates (if necessary) the storage and thumbnail paths.
51+ *
52+ * @return string The normalized storage path
53+ *
54+ * @throws FilesException
55+ */
56+ public static function storage (): string
57+ {
58+ // Normalize the path
59+ $ storage = realpath (config ('Files ' )->storagePath ) ?: config ('Files ' )->storagePath ;
60+ $ storage = rtrim ($ storage , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR ;
61+ if (! is_dir ($ storage ) && ! @mkdir ($ storage , 0775 , true ))
62+ {
63+ throw FilesException::forDirFail ($ storage );
64+ }
65+
66+ // Normalize the path
67+ $ thumbnails = $ storage . 'thumbnails ' ;
68+ if (! is_dir ($ thumbnails ) && ! @mkdir ($ thumbnails , 0775 , true ))
69+ {
70+ throw FilesException::forDirFail ($ thumbnails );
71+ }
72+
73+ return $ storage ;
74+ }
75+
76+ //--------------------------------------------------------------------
77+
4878 /**
4979 * Associates a file with a user
5080 *
@@ -127,8 +157,7 @@ public function createFromFile(CIFile $file, string $originalName = null): File
127157 ];
128158
129159 // Normalize paths
130- $ storage = realpath (config ('Files ' )->storagePath ) ?: config ('Files ' )->storagePath ;
131- $ storage = rtrim ($ storage , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR ;
160+ $ storage = self ::storage ();
132161 $ filePath = $ file ->getRealPath () ?: $ file ->__toString ();
133162
134163 // Determine if we need to move the file
@@ -150,10 +179,11 @@ public function createFromFile(CIFile $file, string $originalName = null): File
150179
151180 // Try to create a Thumbnail
152181 $ thumbnail = pathinfo ($ row ['localname ' ], PATHINFO_FILENAME );
153- $ thumbPath = $ storage . 'thumbnails ' . DIRECTORY_SEPARATOR . $ thumbnail ;
182+ $ output = $ storage . 'thumbnails ' . DIRECTORY_SEPARATOR . $ thumbnail ;
183+
154184 try
155185 {
156- service ('thumbnails ' )->create ($ filePath , $ thumbPath );
186+ service ('thumbnails ' )->create ($ file -> __toString () , $ output );
157187
158188 // If it succeeds then update the database
159189 $ this ->update ($ fileId , [
@@ -162,8 +192,8 @@ public function createFromFile(CIFile $file, string $originalName = null): File
162192 }
163193 catch (\Throwable $ e )
164194 {
165- log_message ('debug ' , $ e ->getMessage ());
166- log_message ('debug ' , 'Unable to create thumbnail for ' . $ row ['filename ' ]);
195+ log_message ('error ' , $ e ->getMessage ());
196+ log_message ('error ' , 'Unable to create thumbnail for ' . $ row ['filename ' ]);
167197 }
168198
169199 // Return the File entity
0 commit comments