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.

89 lines
2.6 KiB

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