-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
feat(files_sharing): fetch remote avatars for external shares #62355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leftybournes
wants to merge
6
commits into
master
Choose a base branch
from
leftybournes/feat/federatedshares-show-avatars
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5bbcc9c
feat(files_sharing): fetch remote avatars for external shares
leftybournes 7551692
test(files_sharing): add tests for fetching remote avatars
leftybournes 32a4bfb
fixup! feat(files_sharing): fetch remote avatars for external shares
leftybournes 7b9f2f1
fixup! fixup! feat(files_sharing): fetch remote avatars for external …
leftybournes eb74733
fixup! fixup! fixup! feat(files_sharing): fetch remote avatars for ex…
leftybournes 56fde46
fixup! test(files_sharing): add tests for fetching remote avatars
leftybournes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-only | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OC\Avatar; | ||
|
|
||
| use OCP\Federation\ICloudId; | ||
| use OCP\Federation\ICloudIdManager; | ||
| use OCP\Files\NotFoundException; | ||
| use OCP\Files\SimpleFS\ISimpleFile; | ||
| use OCP\Files\SimpleFS\ISimpleFolder; | ||
| use OCP\Http\Client\IClientService; | ||
| use OCP\IConfig; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| class RemoteAvatar extends Avatar { | ||
| private const IMAGE_CACHE_AGE = 60 * 60 * 24; // One day | ||
|
|
||
| private ICloudId $cloudId; | ||
|
|
||
| public function __construct( | ||
| protected ISimpleFolder $folder, | ||
| protected string $userId, | ||
| protected IConfig $config, | ||
| protected LoggerInterface $logger, | ||
| ) { | ||
| parent::__construct($config, $logger); | ||
|
|
||
| $cloudIdManager = \OCP\Server::get(ICloudIdManager::class); | ||
| $this->cloudId = $cloudIdManager->resolveCloudId($userId); | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function exists(): bool { | ||
| return true; | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function getDisplayName(): string { | ||
| return $this->cloudId->getDisplayId(); | ||
| } | ||
|
|
||
| /** | ||
| * Setting avatars isn't implemented for remote accounts | ||
| */ | ||
| #[\Override] | ||
| public function set($data): void { | ||
| } | ||
|
|
||
| /** | ||
| * Removing avatars isn't implemented for remote accounts | ||
| */ | ||
| #[\Override] | ||
| public function remove(bool $silent = false): void { | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function getFile(int $size, bool $darkTheme = false): ISimpleFile { | ||
| $avatarExists = false; | ||
| if ($size === -1) { | ||
| $filename = 'avatar' . ($darkTheme ? '-dark' : ''); | ||
| } else { | ||
| $filename = 'avatar' . ($darkTheme ? '-dark' : '') . '.' . $size; | ||
| } | ||
|
|
||
| // check first if a png avatar exists | ||
| $ext = 'png'; | ||
| try { | ||
| $file = $this->folder->getFile($filename . '.' . $ext); | ||
| $avatarExists = true; | ||
| } catch (NotFoundException $e) { | ||
| $this->logger->debug('Unable to find avatar with .png extension. Trying .jpg'); | ||
| } | ||
|
|
||
| // if the png avatar doesn't exist, check if there's a jpg | ||
| if (!$avatarExists) { | ||
| $ext = 'jpg'; | ||
| try { | ||
| $file = $this->folder->getFile($filename . '.' . $ext); | ||
| $avatarExists = true; | ||
| } catch (NotFoundException $e) { | ||
| $this->logger->debug('No avatar found'); | ||
| } | ||
| } | ||
|
|
||
| if ($avatarExists) { | ||
| // check if a remote avatar is at least a day old, in case a new avatar was uploaded | ||
| $isAvatarOld = (time() - $file->getMTime()) >= self::IMAGE_CACHE_AGE; | ||
| if (!$isAvatarOld) { | ||
| return $file; | ||
| } | ||
| } | ||
|
|
||
| $url = rtrim($this->cloudId->getRemote(), '/') . '/index.php/avatar/' . rawurlencode($this->cloudId->getUser()) . '/' . $size; | ||
| if ($darkTheme) { | ||
| $url .= '/dark'; | ||
| } | ||
|
|
||
| $clientService = \OCP\Server::get(IClientService::class); | ||
| $client = $clientService->newClient(); | ||
| $response = $client->get($url, [ | ||
| 'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates', false) | ||
| ]); | ||
|
|
||
| $contentType = $response->getHeader('Content-Type'); | ||
| if (strpos($contentType, 'image/') === false) { | ||
| throw new \Exception('Unknown filetype'); | ||
| } | ||
|
|
||
| $avatar = $response->getBody(); | ||
| if (is_resource($avatar)) { | ||
| $avatar = stream_get_contents($avatar); | ||
| if ($avatar === false) { | ||
| throw new \Exception('Failed to fetch remote avatar'); | ||
| } | ||
| } | ||
|
|
||
| $ext = $contentType === 'image/jpg' || $contentType === 'image/jpeg' ? 'jpg' : 'png'; | ||
| return $this->folder->newFile($filename . '.' . $ext, $avatar); | ||
| } | ||
|
|
||
| /** | ||
| * Handling user changes isn't implemented for remote accounts | ||
| */ | ||
| #[\Override] | ||
| public function userChanged(string $feature, $oldValue, $newValue): void { | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function isCustomAvatar(): bool { | ||
| return true; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems a bit strict. If a user uploads their own avatar it is probably stored and served in the original format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I modified it to also check for jpgs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a lot more formats that people actually use. Maybe just limit the check to ensure the mimetype starts with
image/?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense and it makes the check easier. Just pushed the change.