DownloadController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\PostFileDownLog;
  9. use App\Models\DTO\SearchData;
  10. use Exception;
  11. class DownloadController extends Controller
  12. {
  13. use AgentTrait;
  14. private Board $boardModel;
  15. private PostFile $postFileModel;
  16. private PostFileDownLog $postFileDownloadLogModel;
  17. public function __construct(Board $board, PostFile $postFile, PostFileDownLog $postFileDownLog)
  18. {
  19. $this->boardModel = $board;
  20. $this->postFileModel = $postFile;
  21. $this->postFileDownloadLogModel = $postFileDownLog;
  22. }
  23. /**
  24. * 게시판 > 첨부파일 (다운로드)
  25. * @method GET
  26. * @see /admin/board/file/download
  27. */
  28. public function index(Request $request)
  29. {
  30. $params = SearchData::fromRequest($request);
  31. $params->boardID = $request->get('board_id');
  32. $postFileLogData = $this->postFileDownloadLogModel->data($params);
  33. if ($postFileLogData->rows > 0) {
  34. $num = listNum($postFileLogData->total, $params->page, $params->perPage);
  35. foreach ($postFileLogData->list as $i => $row) {
  36. $row->num = $num--;
  37. $row->postURL = route('board.post.view', [$row->code, $row->post_id]);
  38. $row->fileLink = route('admin.board.file.download.show', $row->post_file_id);
  39. $row->editURL = route('admin.board.file.download.edit', $row->id);
  40. $row->fileSize = byteFormat($row->file_size, 2);
  41. $row->fileExists = file_exists($row->file_path);
  42. $row->browser = $this->browser($row->user_agent);
  43. $row->platform = $this->platform($row->user_agent);
  44. $row->download = number_format($row->download);
  45. $row->createdAt = dateBr($row->created_at);
  46. $postFileLogData->list[$i] = $row;
  47. }
  48. }
  49. return view('admin.board.file.download.index', [
  50. 'postFileLogData' => $postFileLogData,
  51. 'params' => $params,
  52. 'boardData' => $this->boardModel->all()
  53. ]);
  54. }
  55. /**
  56. * 게시판 > 첨부파일 (다운로드) 삭제
  57. * @method DELETE
  58. * @see /admin/board/file/download/destroy
  59. */
  60. public function destroy(Request $request)
  61. {
  62. try {
  63. $chk = $request->post('chk');
  64. if ($chk) {
  65. foreach ($chk as $postFileDownLogID) {
  66. $postFileDownloadLog = $this->postFileDownloadLogModel->findOrNew($postFileDownLogID);
  67. if(!$postFileDownloadLog->exists) {
  68. throw new Exception($postFileDownLogID . '번 다운로드 이력은 존재하지 않습니다.');
  69. }
  70. if(!$postFileDownloadLog->delete()) {
  71. throw new Exception($postFileDownLogID . '번 다운로드 기록을 삭제할 수 없습니다.');
  72. }
  73. }
  74. }
  75. $message = '다운로드 기록이 삭제되었습니다.';
  76. return redirect()->route('admin.board.file.download.index')->with('message', $message);
  77. } catch (Exception $e) {
  78. return back()->withErrors($e->getMessage())->withInput();
  79. }
  80. }
  81. /**
  82. * 게시판 > 첨부파일(다운로드) 실행
  83. * @method GET
  84. * @see /admin/board/file/download/show/{pk}
  85. */
  86. public function show(int $postFileID)
  87. {
  88. $fileModel = $this->postFileModel->findOrFail($postFileID);
  89. if(!file_exists($fileModel->file_path)) {
  90. return back()->with('message', '해당 파일이 존재하지 않습니다.');
  91. }
  92. return response()->download($fileModel->file_path, $fileModel->origin_name);
  93. }
  94. /**
  95. * 게시판 > 첨부파일(다운로드) 상세 보기
  96. * @method GET
  97. * @see /admin/board/file/download/{pk}/edit
  98. */
  99. public function edit(int $postFileDownLogID)
  100. {
  101. $log = $this->postFileDownloadLogModel->findOrFail($postFileDownLogID);
  102. $log->device = $this->device($log->user_agent);
  103. $log->platform = $this->platform($log->user_agent);
  104. $log->browser = $this->browser($log->user_agent);
  105. $log->fileLink = route('admin.board.file.download.show', $postFileDownLogID);
  106. $log->fileSize = byteFormat($log->postFile->file_size, 2);
  107. $log->download = number_format($log->postFile->download);
  108. return view('admin.board.file.download.edit', [
  109. 'log' => $log
  110. ]);
  111. }
  112. }