Преглед изворни кода

Fix remote profile avatar urls when storing locally

Daniel Supernault пре 2 година
родитељ
комит
b0422d4f26

+ 1 - 1
app/Services/MediaStorageService.php

@@ -248,7 +248,7 @@ class MediaStorageService {
 
 		$avatar->media_path = $base . $path;
 		$avatar->is_remote = true;
-		$avatar->cdn_url = $permalink;
+		$avatar->cdn_url = $local ? config('app.url') . $permalink : $permalink;
 		$avatar->size = $head['length'];
 		$avatar->change_count = $avatar->change_count + 1;
 		$avatar->last_fetched_at = now();

+ 41 - 0
database/migrations/2022_10_09_043758_fix_cdn_url_in_avatars_table.php

@@ -0,0 +1,41 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Support\Facades\Cache;
+use App\Services\AccountService;
+use App\Avatar;
+
+class FixCdnUrlInAvatarsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        $baseUrl = 'https://' . config('pixelfed.domain.app');
+        Avatar::whereNotNull('cdn_url')
+        ->chunk(50, function($avatars) use($baseUrl) {
+            foreach($avatars as $avatar) {
+                if(substr($avatar->cdn_url, 0, 23) === '/storage/cache/avatars/') {
+                    $avatar->cdn_url = $baseUrl . $avatar->cdn_url;
+                    $avatar->save();
+                }
+                Cache::forget('avatar:' . $avatar->profile_id);
+                AccountService::del($avatar->profile_id);
+            }
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+    }
+}