mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-11-23 19:32:29 +01:00
Fixed image folder deletion. Fixes #22.
This commit is contained in:
parent
958ed627dd
commit
40b629d35d
@ -177,14 +177,30 @@ class ImageController extends Controller
|
||||
|
||||
// Delete files
|
||||
$folder = public_path() . dirname($image->url);
|
||||
$pattern = '/' . preg_quote(basename($image->url)). '/';
|
||||
$dir = new RecursiveDirectoryIterator($folder);
|
||||
$ite = new RecursiveIteratorIterator($dir);
|
||||
$files = new RegexIterator($ite, $pattern, RegexIterator::ALL_MATCHES);
|
||||
foreach($files as $path => $file) {
|
||||
unlink($path);
|
||||
$fileName = basename($image->url);
|
||||
|
||||
// Delete thumbnails
|
||||
foreach(glob($folder . '/*') as $file) {
|
||||
if(is_dir($file)) {
|
||||
$thumbName = $file . '/' . $fileName;
|
||||
if(file_exists($file)) {
|
||||
unlink($thumbName);
|
||||
}
|
||||
// Remove thumb folder if empty
|
||||
if(count(glob($file . '/*')) === 0) {
|
||||
rmdir($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete file and database entry
|
||||
unlink($folder . '/' . $fileName);
|
||||
$image->delete();
|
||||
|
||||
// Delete parent folder if empty
|
||||
if(count(glob($folder . '/*')) === 0) {
|
||||
rmdir($folder);
|
||||
}
|
||||
return response()->json('Image Deleted');
|
||||
}
|
||||
|
||||
|
@ -70,6 +70,7 @@ Route::group(['middleware' => 'auth'], function() {
|
||||
Route::get('/images/all', 'ImageController@getAll');
|
||||
Route::put('/images/update/{imageId}', 'ImageController@update');
|
||||
Route::delete('/images/{imageId}', 'ImageController@destroy');
|
||||
Route::get('/images/{imageId}/delete', 'ImageController@destroy');
|
||||
Route::get('/images/all/{page}', 'ImageController@getAll');
|
||||
Route::get('/images/{any}', 'ImageController@getImage')->where('any', '.*');
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user