1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebDevEtc\BlogEtc\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Contracts\View\Factory; |
8
|
|
|
use Illuminate\View\View; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use WebDevEtc\BlogEtc\Middleware\UserCanManageBlogPosts; |
11
|
|
|
use WebDevEtc\BlogEtc\Models\UploadedPhoto; |
12
|
|
|
use WebDevEtc\BlogEtc\Requests\UploadImageRequest; |
13
|
|
|
use WebDevEtc\BlogEtc\Services\PostsService; |
14
|
|
|
use WebDevEtc\BlogEtc\Services\UploadsService; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class BlogEtcAdminController. |
18
|
|
|
*/ |
19
|
|
|
class ManageUploadsController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var UploadsService |
23
|
|
|
*/ |
24
|
|
|
private $uploadsService; |
25
|
|
|
/** |
26
|
|
|
* @var PostsService |
27
|
|
|
*/ |
28
|
|
|
private PostsService $postsService; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* BlogEtcAdminController constructor. |
32
|
|
|
*/ |
33
|
|
|
public function __construct(UploadsService $uploadsService, PostsService $postsService) |
34
|
|
|
{ |
35
|
|
|
$this->middleware(UserCanManageBlogPosts::class); |
36
|
|
|
$this->uploadsService = $uploadsService; |
37
|
|
|
$this->postsService = $postsService; |
38
|
|
|
|
39
|
|
|
if (!config('blogetc.image_upload_enabled')) { |
40
|
|
|
throw new RuntimeException('The blogetc.php config option is missing or has not enabled image uploading'); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Show the main listing of uploaded images. |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function index() |
50
|
|
|
{ |
51
|
|
|
return view('blogetc_admin::imageupload.index', [ |
52
|
|
|
'uploaded_photos' => UploadedPhoto::orderBy('id', 'desc') |
53
|
|
|
->paginate(10), |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* show the form for uploading a new image. |
59
|
|
|
* |
60
|
|
|
* @return Factory|View |
61
|
|
|
*/ |
62
|
|
|
public function create() |
63
|
|
|
{ |
64
|
|
|
return view('blogetc_admin::imageupload.create', []); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Save a new uploaded image. |
69
|
|
|
* |
70
|
|
|
* @throws Exception |
71
|
|
|
*/ |
72
|
|
|
public function store(UploadImageRequest $request) |
73
|
|
|
{ |
74
|
|
|
// Uses some legacy code - this will be refactored and fixed soon! |
75
|
|
|
$processed_images = $this->uploadsService->legacyProcessUploadedImagesSingle($request); |
76
|
|
|
|
77
|
|
|
return view('blogetc_admin::imageupload.uploaded', ['images' => $processed_images]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function deletePostImage(int $postId) |
81
|
|
|
{ |
82
|
|
|
return view('blogetc_admin::imageupload.delete-post-image', ['postId' => $postId]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function deletePostImageConfirmed(int $postId) |
86
|
|
|
{ |
87
|
|
|
$post = $this->postsService->findById($postId); |
88
|
|
|
|
89
|
|
|
$deletedSizes = $this->uploadsService->deletePostImage($post); |
90
|
|
|
$this->postsService->clearImageSizes($post, $deletedSizes); |
91
|
|
|
|
92
|
|
|
return view('blogetc_admin::imageupload.deleted-post-image'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|