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
3.3 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
  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\Models\File;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Response;
  11. use Illuminate\Support\Facades\Storage;
  12. class FileController extends Controller
  13. {
  14. public function show($collection, $uuid, $ext, Request $request, ImageProcessor $imageProcessor)
  15. {
  16. if ($request->castParams == 'resource') {
  17. return new FileResource(app()->file);
  18. }
  19. if (!is_null(array_intersect(array_keys($request->all()), $this->availableParams))) {
  20. $data = $imageProcessor->processToBuffer(app()->file->getPath(), $request->all());
  21. $response = Response::make($data, 200);
  22. $response->header('Content-Type','image/webp');
  23. return $response;
  24. }
  25. return response()->file(app()->file->getPath());
  26. }
  27. public function store(FileStoreRequest $request, ImageProcessor $imageProcessor)
  28. {
  29. $request->file = $imageProcessor->convertImage($request->file->path(), '/tmp/' . app()->uuid . '.' . app()->collection->ext);
  30. if (!is_null(app()->collection->process)) {
  31. $request->file = $imageProcessor->process($request->file->path(), '/tmp/' . app()->uuid . '.' . app()->collection->ext, app()->collection->process);
  32. }
  33. $fileResource = null;
  34. DB::transaction(function () use ($request, &$fileResource) {
  35. $uuid = app()->uuid;
  36. $fileResource = File::create([
  37. 'uuid' => $uuid,
  38. 'original_name' => $request->name,
  39. 'public' => $request->public,
  40. 'ext' => $request->file->extension(),
  41. 'mimetype' => $request->file->getMimeType(),
  42. 'width' => getimagesize($request->file)[0],
  43. 'height' => getimagesize($request->file)[1],
  44. 'file_size' => $request->file->getSize(),
  45. 'sort' => $request->file->getSize(),
  46. 'alts' => $request->alts,
  47. 'description' => $request->description,
  48. 'user_id' => auth()->id(),
  49. 'ip' => $request->ip(),
  50. 'collection_id' => app()->collection->id,
  51. 'published_at' => $request->published_at,
  52. 'server_path' => '/' . date('y') . '/' . date('m') . '/',
  53. ]);
  54. if (!app()->collection->tmp_support && app()->collection->count == 1) {
  55. File::where('user_id', auth()->id())->delete();
  56. }
  57. $storedFile = Storage::disk(app()->collection->disk)->putFileAs($fileResource->server_path, $request->file, $fileResource->uuid . '.' . app()->collection->ext);
  58. if (app()->collection->public) {
  59. Storage::disk(app()->collection->disk)->setVisibility($storedFile, 'public');
  60. }
  61. });
  62. return new FileResource($fileResource);
  63. }
  64. public function update(FileUpdateRequest $request)
  65. {
  66. app()->file->update($request->all());
  67. return new FileResource(app()->file);
  68. }
  69. public function destroy()
  70. {
  71. if (app()->file->trashed()) {
  72. return app()->file->restore();
  73. }
  74. return app()->file->delete();
  75. }
  76. }