Skip to content

Commit

Permalink
storage fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lahirulhr committed May 24, 2022
1 parent 527ee84 commit 014886a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 146 deletions.
110 changes: 0 additions & 110 deletions Controller/MediaController.php

This file was deleted.

20 changes: 0 additions & 20 deletions Controller/MediaControllerInterface.php

This file was deleted.

13 changes: 11 additions & 2 deletions config/nova-tinymce5-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@
],
'apiKey' => env('TINYMCE_API_KEY', ''),

'storage_path' => 'tinymce',

'image_url_path' => '/storage/tinymce/',
/*
|--------------------------------------------------------------------------
| Images related setup
|--------------------------------------------------------------------------
|
*/

'storage_path' => 'tinymce/',

'disk' => env('FILESYSTEM_DRIVER','local'),

'media_handler' => \Kraftbit\NovaTinymce5Editor\Controller\MediaController::class

],
];

29 changes: 15 additions & 14 deletions src/Controller/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function getImages(Request $request)
return [
'id' => $itm->id,
'name' => $itm->name,
'preview_url' => url(config('nova-tinymce5-editor.options.image_url_path') . $itm->file_name),
'image_url' => url(config('nova-tinymce5-editor.options.image_url_path') . $itm->file_name),
'preview_url' => $this->getImageUrl($itm->file_name),
'image_url' => $this->getImageUrl($itm->file_name),
'size' => $this->sizeFilter($itm->file_size)
];
});
Expand All @@ -33,8 +33,8 @@ public function searchImages(string $query)
return [
'id' => $itm->id,
'name' => $itm->name,
'preview_url' => url(config('nova-tinymce5-editor.options.image_url_path') . $itm->file_name),
'image_url' => url(config('nova-tinymce5-editor.options.image_url_path') . $itm->file_name),
'preview_url' => $this->getImageUrl($itm->file_name),
'image_url' => $this->getImageUrl($itm->file_name),
'size' => $this->sizeFilter($itm->file_size)
];
});
Expand All @@ -47,13 +47,13 @@ public function uploadImage(Request $request)
foreach ($request->file('images') as $file){

$fileName = Str::random() . "." . $file->getClientOriginalExtension();
$file->storeAs(config('nova-tinymce5-editor.options.storage_path'), $fileName);
$file->storeAs(Str::finish(config('nova-tinymce5-editor.options.storage_path'),"/"), $fileName, ['disk' => config('nova-tinymce5-editor.options.disk')]);

$rec = DB::table('tinymce_images')->insertGetId([
'name' => $file->getClientOriginalName(),
'file_name' => $fileName,
'file_size' => $file->getSize(),
'disk' => config('filesystems.default'),
'disk' => config('nova-tinymce5-editor.options.disk'),
'created_at' => now()
]);

Expand All @@ -62,9 +62,9 @@ public function uploadImage(Request $request)
$out[] = [
'id' => $rec->id,
'name' => $file->getClientOriginalName(),
'preview_url' => url(config('nova-tinymce5-editor.options.image_url_path') . $fileName),
'image_url' => url(config('nova-tinymce5-editor.options.image_url_path') . $fileName),
'size' => $this->sizeFilter($file->getSize())
'preview_url' => $this->getImageUrl($rec->file_name),
'image_url' => $this->getImageUrl($rec->file_name),
'size' => $this->sizeFilter($rec->file_size)
];

}
Expand All @@ -75,23 +75,19 @@ public function uploadImage(Request $request)
return [];
}


public function deleteImages(Request $request)
{
foreach ($request->get('images') as $img) {
$file = DB::table('tinymce_images')->where('id', $img)->first();
// remove file from storage
Storage::delete(config('nova-tinymce5-editor.options.storage_path')."/".$file->file_name);
Storage::disk(config('nova-tinymce5-editor.options.disk'))->delete(config('nova-tinymce5-editor.options.storage_path')."/".$file->file_name);
// remove db entry
DB::table('tinymce_images')->delete($img);
}

return response()->json("Succesfuly deleted !");
}




private function sizeFilter($bytes)
{
$label = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
Expand All @@ -102,4 +98,9 @@ private function sizeFilter($bytes)
);
return (round($bytes, 2) . " " . $label[$i]);
}

private function getImageUrl($image)
{
return Storage::disk(config('nova-tinymce5-editor.options.disk'))->url(config('nova-tinymce5-editor.options.storage_path' ). $image);
}
}

0 comments on commit 014886a

Please sign in to comment.