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.

97 lines
3.5 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\FileStoreRequest;
  4. use App\Http\Requests\FileUpdateRequest;
  5. use App\Http\Resources\FileResource;
  6. use App\Image\ImageProcessor;
  7. use App\Jobs\FileConversionQueue;
  8. use App\Models\File;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Storage;
  12. use Illuminate\Support\Facades\Validator;
  13. use Symfony\Component\Console\Input\Input;
  14. use function PHPUnit\Framework\isNull;
  15. class FileController extends Controller
  16. {
  17. public function show($collection, $uuid, $ext, Request $request, ImageProcessor $imageProcessor)
  18. {
  19. if ($request->castParams == 'resource') {
  20. return new FileResource(app()->file);
  21. }
  22. if (!is_null(array_intersect(array_keys($request->all()), $this->availableParams))) {
  23. $imageProcessor->process(app()->file->getPath(), app()->file->getModifiedPath(), $request->all());
  24. return response()->file(app()->file->getModifiedPath());
  25. }
  26. return response()->file(app()->file->getPath());
  27. }
  28. // for local private we didn't talk about it
  29. // public function private($collection, $path)
  30. // {
  31. // Storage::disk('local')->download($path);
  32. // }
  33. public function store(FileStoreRequest $request, ImageProcessor $imageProcessor)
  34. {
  35. $request->file = $imageProcessor->convertImage($request->file->path(), '/tmp/' . app()->uuid . '.' . app()->collection->ext);
  36. if (!is_null(app()->collection->process)) {
  37. $request->file = $imageProcessor->process($request->file->path(), '/tmp/' . app()->uuid . '.' . app()->collection->ext, app()->collection->process);
  38. }
  39. $fileResource = null;
  40. DB::transaction(function () use ($request, &$fileResource) {
  41. $uuid = app()->uuid;
  42. $fileResource = File::create([
  43. 'uuid' => $uuid,
  44. 'original_name' => $request->name,
  45. 'public' => $request->public,
  46. 'ext' => $request->file->extension(),
  47. 'mimetype' => $request->file->getMimeType(),
  48. 'width' => getimagesize($request->file)[0],
  49. 'height' => getimagesize($request->file)[1],
  50. 'file_size' => $request->file->getSize(),
  51. 'sort' => $request->file->getSize(),
  52. 'alts' => $request->alts,
  53. 'description' => $request->description,
  54. 'user_id' => auth()->id(),
  55. 'ip' => $request->ip(),
  56. 'collection_id' => app()->collection->id,
  57. 'published_at' => $request->published_at,
  58. 'server_path' => '/' . date('y') . '/' . date('m') . '/',
  59. ]);
  60. if (!app()->collection->tmp_support && app()->collection->count == 1) {
  61. File::where('user_id', auth()->id())->delete();
  62. }
  63. $storedFile = Storage::disk(app()->collection->disk)->putFileAs($fileResource->server_path, $request->file, $fileResource->uuid . '.' . app()->collection->ext);
  64. if (app()->collection->public) {
  65. Storage::disk(app()->collection->disk)->setVisibility($storedFile, 'public');
  66. }
  67. });
  68. return new FileResource($fileResource);
  69. }
  70. public function update(FileUpdateRequest $request)
  71. {
  72. app()->file->update($request->all());
  73. return new FileResource(app()->file);
  74. }
  75. public function destroy()
  76. {
  77. if (app()->file->trashed()) {
  78. return app()->file->restore();
  79. }
  80. return app()->file->delete();
  81. }
  82. }