Skip to content

Commit 123ed23

Browse files
committed
Fix thumbnail bug
1 parent 8ec1681 commit 123ed23

3 files changed

Lines changed: 50 additions & 10 deletions

File tree

src/Controllers/Files.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ public function __construct(FilesConfig $config = null, FileModel $model = null)
5656
$this->model = $model ?? model('FileModel');
5757

5858
// Verify the storage directory
59-
if (! is_dir($this->config->storagePath) && ! @mkdir($this->config->storagePath, 0775, true))
60-
{
61-
throw FilesException::forDirFail($this->config->storagePath);
62-
}
59+
FileModel::storage();
6360
}
6461

6562
/**

src/Models/FileModel.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use CodeIgniter\Files\File as CIFile;
44
use CodeIgniter\Model;
55
use Tatter\Files\Entities\File;
6+
use Tatter\Files\Exceptions\FilesException;
67
use Tatter\Thumbnails\Exceptions\ThumbnailsException;
78

89
class 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

tests/unit/ModelTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,17 @@ public function testCreateFromPathAssignsToUser()
7171

7272
$this->assertCount(1, $result);
7373
}
74+
75+
public function testCreateAddsThumbnail()
76+
{
77+
helper('filesystem');
78+
$file = $this->model->createFromPath($this->testPath);
79+
80+
$thumbnail = pathinfo($file->localname, PATHINFO_FILENAME);
81+
$array = $file->toRawArray();
82+
$this->assertEquals($thumbnail, $array['thumbnail']);
83+
84+
$path = FileFaker::storage() . 'thumbnails' . DIRECTORY_SEPARATOR . $thumbnail;
85+
$this->assertFileExists($path);
86+
}
7487
}

0 commit comments

Comments
 (0)