Bläddra i källkod

Store remote avatars locally if S3 not enabled

Daniel Supernault 3 år sedan
förälder
incheckning
b4bd0400c2

+ 8 - 3
app/Jobs/AvatarPipeline/RemoteAvatarFetch.php

@@ -32,6 +32,13 @@ class RemoteAvatarFetch implements ShouldQueue
 	*/
 	public $deleteWhenMissingModels = true;
 
+	/**
+	 * The number of times the job may be attempted.
+	 *
+	 * @var int
+	 */
+	public $tries = 1;
+
 	/**
 	* Create a new job instance.
 	*
@@ -99,9 +106,7 @@ class RemoteAvatarFetch implements ShouldQueue
 		$avatar->remote_url = $icon['url'];
 		$avatar->save();
 
-		if(config_cache('pixelfed.cloud_storage')) {
-			MediaStorageService::avatar($avatar);
-		}
+		MediaStorageService::avatar($avatar, config_cache('pixelfed.cloud_storage') == false);
 
 		return 1;
 	}

+ 7 - 6
app/Services/MediaStorageService.php

@@ -27,9 +27,9 @@ class MediaStorageService {
 		return;
 	}
 
-	public static function avatar($avatar)
+	public static function avatar($avatar, $local = false)
 	{
-		return (new self())->fetchAvatar($avatar);
+		return (new self())->fetchAvatar($avatar, $local);
 	}
 
 	public static function head($url)
@@ -177,11 +177,12 @@ class MediaStorageService {
 		unlink($tmpName);
 	}
 
-	protected function fetchAvatar($avatar)
+	protected function fetchAvatar($avatar, $local = false)
 	{
 		$url = $avatar->remote_url;
+		$driver = $local ? 'local' : config('filesystems.cloud');
 
-		if($url == null || Helpers::validateUrl($url) == false) {
+		if(empty($url) || Helpers::validateUrl($url) == false) {
 			return;
 		}
 
@@ -220,7 +221,7 @@ class MediaStorageService {
 			return;
 		}
 
-		$base = 'cache/avatars/' . $avatar->profile_id;
+		$base = ($local ? 'public/cache/' : 'cache/') . 'avatars/' . $avatar->profile_id;
 		$ext = $head['mime'] == 'image/jpeg' ? 'jpg' : 'png';
 		$path = Str::random(20) . '_avatar.' . $ext;
 		$tmpBase = storage_path('app/remcache/');
@@ -229,7 +230,7 @@ class MediaStorageService {
 		$data = file_get_contents($url, false, null, 0, $head['length']);
 		file_put_contents($tmpName, $data);
 
-		$disk = Storage::disk(config('filesystems.cloud'));
+		$disk = Storage::disk($driver);
 		$file = $disk->putFileAs($base, new File($tmpName), $path, 'public');
 		$permalink = $disk->url($file);
 

+ 2 - 6
app/Util/ActivityPub/Helpers.php

@@ -586,9 +586,7 @@ class Helpers {
 						$profile->webfinger = Purify::clean($webfinger);
 						$profile->last_fetched_at = now();
 						$profile->save();
-						if(config_cache('pixelfed.cloud_storage') == true) {
-							RemoteAvatarFetch::dispatch($profile);
-						}
+						RemoteAvatarFetch::dispatch($profile);
 						return $profile;
 					});
 				});
@@ -603,9 +601,7 @@ class Helpers {
 					$profile->sharedInbox = isset($res['endpoints']) && isset($res['endpoints']['sharedInbox']) && Helpers::validateUrl($res['endpoints']['sharedInbox']) ? $res['endpoints']['sharedInbox'] : null;
 					$profile->save();
 				}
-				if(config_cache('pixelfed.cloud_storage') == true) {
-					RemoteAvatarFetch::dispatch($profile);
-				}
+				RemoteAvatarFetch::dispatch($profile);
 			}
 			return $profile;
 		});

+ 1 - 1
config/federation.php

@@ -33,7 +33,7 @@ return [
 	],
 
 	'avatars' => [
-		'store_local' => false
+		'store_local' => env('REMOTE_AVATARS', true),
 	],
 
 	'nodeinfo' => [