AttachmentResource.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Contracts\Support\Arrayable;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. use Illuminate\Support\Facades\App;
  7. use Illuminate\Support\Facades\Storage;
  8. class AttachmentResource extends JsonResource
  9. {
  10. /**
  11. * Transform the resource into an array.
  12. *
  13. * @param Request $request
  14. * @return array|Arrayable|\JsonSerializable
  15. */
  16. public function toArray($request)
  17. {
  18. $filename = $this->bucket.'/'.$this->name;
  19. $data = [
  20. 'id' => $this->id,
  21. 'user_uid' => $this->user_uid,
  22. 'name' => $filename,
  23. 'filename' => $filename,
  24. 'title' => $this->title,
  25. 'size' => $this->size,
  26. 'content_type' => $this->content_type,
  27. 'status' => $this->status,
  28. 'created_at' => $this->created_at,
  29. 'updated_at' => $this->updated_at,
  30. ];
  31. if (App::environment('local')) {
  32. $data['url'] = Storage::url($filename);
  33. } else {
  34. $data['url'] = Storage::temporaryUrl($filename, now()->addDays(2));
  35. }
  36. $type = explode('/', $this->content_type);
  37. if ($type[0] === 'image' || $type[0] === 'video') {
  38. $path_parts = pathinfo($this->name);
  39. $small = $this->bucket.'/'.$path_parts['filename'].'_s.jpg';
  40. $middle = $this->bucket.'/'.$path_parts['filename'].'_m.jpg';
  41. if (App::environment('local')) {
  42. $data['thumbnail'] = [
  43. 'small' => Storage::url($small),
  44. 'middle' => Storage::url($middle),
  45. ];
  46. } else {
  47. $data['thumbnail'] = [
  48. 'small' => Storage::temporaryUrl($small, now()->addDays(2)),
  49. 'middle' => Storage::temporaryUrl($middle, now()->addDays(2)),
  50. ];
  51. }
  52. }
  53. return $data;
  54. }
  55. }