1
0

FetchMissingMediaMimeType.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Media;
  5. use Illuminate\Support\Facades\Http;
  6. use App\Services\MediaService;
  7. use App\Services\StatusService;
  8. class FetchMissingMediaMimeType extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'app:fetch-missing-media-mime-type';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Command description';
  22. /**
  23. * Execute the console command.
  24. */
  25. public function handle()
  26. {
  27. foreach(Media::whereNotNull(['remote_url', 'status_id'])->whereNull('mime')->lazyByIdDesc(50, 'id') as $media) {
  28. $res = Http::retry(2, 100, throw: false)->head($media->remote_url);
  29. if(!$res->successful()) {
  30. continue;
  31. }
  32. if(!in_array($res->header('content-type'), explode(',',config('pixelfed.media_types')))) {
  33. continue;
  34. }
  35. $media->mime = $res->header('content-type');
  36. if($res->hasHeader('content-length')) {
  37. $media->size = $res->header('content-length');
  38. }
  39. $media->save();
  40. MediaService::del($media->status_id);
  41. StatusService::del($media->status_id);
  42. $this->info('mid:'.$media->id . ' (' . $res->header('content-type') . ':' . $res->header('content-length') . ' bytes)');
  43. }
  44. }
  45. }