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.

98 lines
3.1 KiB

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\Jobs\FileConversionQueue;
  6. use App\Models\File;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Storage;
  10. class FileController extends Controller
  11. {
  12. public function show($collection, $uuid, $ext)
  13. {
  14. dump($collection, $uuid, $ext);
  15. }
  16. public function private($collection, $path)
  17. {
  18. Storage::disk(app()->collection->disk)->download($path);
  19. }
  20. public function store(Request $request)
  21. {
  22. // skip policy ---and first level----------
  23. $validated = $request->validate([
  24. "file" => [
  25. "mimes:" . app()->collection->getExts(),
  26. "mimetypes:" . app()->collection->getMimeTypes(),
  27. "dimensions:min_width=" . app()->collection->min_width . ",min_height=" . app()->collection->min_height . ',max_width=' . app()->collection->max_width . ',max_height=' . app()->collection->max_height,
  28. "max:" . app()->collection->max_size,
  29. "min:" . app()->collection->min_size,
  30. ],
  31. "alts" => [app()->collection->alt_required ? "required" : "null", 'array'],
  32. "alts.*" => [app()->collection->alt_required ? "required" : "null"],
  33. "description" => [app()->collection->description_required ? "required" : "null"],
  34. ]);
  35. DB::beginTransaction();
  36. $urlStorage = '/' . date('y') . '/' . date('m') . app()->getFileUuid;
  37. try {
  38. $file = File::create([
  39. 'uuid' => app()->getFileUuid,
  40. 'original_name' => $request->name,
  41. 'public' => $request->public,
  42. 'ext' => $request->file->extension(),
  43. 'mimetype' => $request->file->getMimeType(),
  44. 'width' => getimagesize($request->file)[0],
  45. 'height' => getimagesize($request->file)[1],
  46. 'file_size' => $request->file->getSize(),
  47. 'sort' => $request->file->getSize(),
  48. 'alts' => $request->alts,
  49. 'description' => $request->description,
  50. 'user_id' => auth()->id(),
  51. 'ip' => $request->ip(),
  52. 'collection_id' => app()->collection->id,
  53. 'published_at' => $request->published_at,
  54. 'server_path' => $urlStorage
  55. ]);
  56. if (!app()->collection->tmp_support && app()->collection->count == 1) {
  57. File::where('user_id', auth()->id())->delete();
  58. }
  59. $file = $request->file->storeAs($urlStorage, app()->getFileUuid, app()->collection->disk);
  60. if (app()->collection->public) {
  61. Storage::setVisibility($file, 'public');
  62. }
  63. DB::commit();
  64. } catch (\Exception $e) {
  65. DB::rollback();
  66. }
  67. FileConversionQueue::dispatch($file, app()->collection);
  68. return new FileResource($file);
  69. }
  70. public function update(Request $request, $id)
  71. {
  72. //
  73. }
  74. public function destroy($id)
  75. {
  76. //
  77. }
  78. }