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.

88 lines
2.6 KiB

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