2023_02_04_053028_fix_cloud_media_paths.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\Schema;
  5. use Illuminate\Support\Facades\Storage;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Media;
  8. use App\Services\MediaService;
  9. use App\Services\StatusService;
  10. return new class extends Migration
  11. {
  12. /**
  13. * Run the migrations.
  14. *
  15. * @return void
  16. */
  17. public function up()
  18. {
  19. ini_set('memory_limit', '-1');
  20. if(config_cache('pixelfed.cloud_storage') == false) {
  21. return;
  22. }
  23. $disk = Storage::disk(config('filesystems.cloud'));
  24. $startUrl = $disk->url('test');
  25. if(!$startUrl) {
  26. return;
  27. }
  28. $baseUrl = substr($startUrl, 0, -4);
  29. $baseUrlLen = strlen($baseUrl);
  30. foreach(Media::whereNotNull('cdn_url')->lazyById(200, 'id') as $media) {
  31. if($media->cdn_url == null) {
  32. continue;
  33. }
  34. $cdnPath = substr($media->cdn_url, $baseUrlLen);
  35. if(str_starts_with($cdnPath, '/')) {
  36. continue;
  37. }
  38. if(!str_starts_with($cdnPath, 'public/')) {
  39. continue;
  40. }
  41. if($cdnPath != $media->media_path) {
  42. $media->media_path = $cdnPath;
  43. $media->saveQuietly();
  44. if($media->status_id) {
  45. MediaService::del($media->status_id);
  46. StatusService::del($media->status_id);
  47. }
  48. }
  49. }
  50. return;
  51. }
  52. /**
  53. * Reverse the migrations.
  54. *
  55. * @return void
  56. */
  57. public function down()
  58. {
  59. //
  60. }
  61. };