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