UploadController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Http\Controllers\Admin\Board\File;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Traits\AgentTrait;
  6. use App\Models\Board;
  7. use App\Models\PostFile;
  8. use App\Models\DTO\SearchData;
  9. use Exception;
  10. class UploadController extends Controller
  11. {
  12. use AgentTrait;
  13. private Board $boardModel;
  14. private PostFile $postFileModel;
  15. public function __construct(Board $board, PostFile $postFile)
  16. {
  17. $this->boardModel = $board;
  18. $this->postFileModel = $postFile;
  19. }
  20. /**
  21. * 게시판 > 첨부파일(Upload)
  22. * @method GET
  23. * @see /admin/board/file/upload
  24. */
  25. public function index(Request $request)
  26. {
  27. $params = SearchData::fromRequest($request);
  28. $params->boardID = $request->get('board_id');
  29. $postFileData = $this->postFileModel->data($params);
  30. if ($postFileData->rows > 0) {
  31. $num = listNum($postFileData->total, $params->page, $params->perPage);
  32. foreach ($postFileData->list as $i => $row) {
  33. $row->num = $num--;
  34. $row->postURL = route('board.post.view', [$row->code, $row->post_id]);
  35. $row->fileLink = route('admin.board.file.download.show', $row->id);
  36. $row->editURL = route('admin.board.file.upload.edit', $row->id);
  37. $row->fileSize = byteFormat($row->file_size, 2);
  38. $row->fileExists = file_exists($row->file_path);
  39. $row->download = number_format($row->download);
  40. $postFileData->list[$i] = $row;
  41. }
  42. }
  43. return view('admin.board.file.upload.index', [
  44. 'postFileData' => $postFileData,
  45. 'params' => $params,
  46. 'boardData' => $this->boardModel->all()
  47. ]);
  48. }
  49. /**
  50. * 게시판 > 첨부파일(Upload) 삭제
  51. * @method DELETE
  52. * @see /admin/board/file/upload/destroy
  53. */
  54. public function destroy(Request $request)
  55. {
  56. try {
  57. $chk = $request->post('chk');
  58. if ($chk) {
  59. foreach ($chk as $postFileID) {
  60. $postFile = $this->postFileModel->findOrNew($postFileID);
  61. if(!$postFile->exists) {
  62. throw new Exception($postFileID . '번 파일은 존재하지 않습니다.');
  63. }
  64. if(!$this->postFileModel->remove($postFileID)) {
  65. throw new Exception($postFileID . "번 파일을 삭제할 수 없습니다.");
  66. }
  67. }
  68. }
  69. $message = '첨부파일이 삭제되었습니다.';
  70. return redirect()->route('admin.board.file.upload.index')->with('message', $message);
  71. } catch (Exception $e) {
  72. return back()->withErrors($e->getMessage())->withInput();
  73. }
  74. }
  75. /**
  76. * 게시판 > 첨부파일(Upload) 상세 보기
  77. * @method GET
  78. * @see /admin/board/file/upload/{pk}/edit
  79. */
  80. public function edit(int $fileID)
  81. {
  82. $file = $this->postFileModel->findOrFail($fileID);
  83. $file->device = $this->device($file->user_agent);
  84. $file->platform = $this->platform($file->user_agent);
  85. $file->browser = $this->browser($file->user_agent);
  86. $file->fileLink = route('admin.board.file.download.show', $file->file_id);
  87. $file->fileSize = byteFormat($file->file_size, 2);
  88. $file->download = number_format($file->download);
  89. return view('admin.board.file.upload.edit', [
  90. 'file' => $file
  91. ]);
  92. }
  93. }