You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.6 KiB

4 years ago
4 years ago
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Auth;
  4. use App\Models\File;
  5. use App\Models\Task;
  6. use App\Models\Project;
  7. use App\Models\Business;
  8. use Illuminate\Http\Request;
  9. use App\Http\Controllers\Controller;
  10. use App\Http\Resources\FileResource;
  11. use Symfony\Component\HttpFoundation\Response;
  12. class TaskFileController extends Controller
  13. {
  14. public function checkBelonging(int $business, int $project, int $task)
  15. {
  16. $business = Business::findOrFail($business);
  17. $project = Project::findOrFail($project);
  18. $task = Task::find($task);
  19. if (
  20. $business->id !== $project->business_id
  21. || $project->id !== $task['project_id']
  22. // || $task['user_id']!== Auth::id()
  23. ) {
  24. \abort(Response::HTTP_UNAUTHORIZED);
  25. }
  26. return [$business, $project, $task];
  27. }
  28. public function index(int $business, int $project, int $task)
  29. {
  30. // check permissions
  31. // owner project
  32. // admin project
  33. // colleague project
  34. // guest or de active
  35. // return files as file resource
  36. [$business, $project, $task] = $this->checkBelonging($business, $project, $task);
  37. return FileResource::collection($task->files ?? []);
  38. }
  39. public function sync(Request $request,int $business, int $project, int $task)
  40. {
  41. // different size and different validation
  42. // validate
  43. // validate the wallet is not so much in debt
  44. // create record in the db
  45. // put file in s3
  46. // return file resource
  47. [$business, $project, $task] = $this->checkBelonging($business,$project,$task);
  48. $this->validate($request, [
  49. 'files' => 'required|array',
  50. 'files.*' => 'int',
  51. ]);
  52. $files = File::find($request->files)->each(function (File $file) {
  53. if ($file->user_id !== Auth::id()) {
  54. abort(Response::HTTP_UNAUTHORIZED);
  55. }
  56. });
  57. // sync
  58. return FileResource::collection($files);
  59. }
  60. public function download(int $business, int $project, int $task, int $file)
  61. {
  62. // requested file belongs to this project and this business
  63. // check permisson
  64. // create perma link or temp link
  65. // return the file resource or stream it
  66. [$business, $project, $task] = $this->checkBelonging($business, $project, $task);
  67. $file = File::findOrFail($file);
  68. if ($file->user_id !== Auth::id()) {
  69. abort(Response::HTTP_UNAUTHORIZED);
  70. }
  71. return $file->getTemporaryLink();
  72. }
  73. }