AttachmentResource.php 1.9 KB

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