bug fixed related to avatar
Signed-off-by: nabim777 <nabinalemagar019@gmail.com>
This commit is contained in:
Родитель
252994cb90
Коммит
ba9ab07374
|
@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
|||
- Improve button text visibility when selecting different background images in Nextcloud's UI
|
||||
- Bump packages version
|
||||
- Fix random deactivation of automatically managed project folder
|
||||
- Fix avatar not found in openproject
|
||||
|
||||
## 2.6.3 - 2024-04-17
|
||||
### Changed
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace OCA\OpenProject\Exception;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
class OpenprojectAvatarErrorException extends Exception {
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
* @param Throwable|null $previous
|
||||
*/
|
||||
public function __construct(string $message, int $code = 0, Throwable $previous = null) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@ use OC\User\NoUserException;
|
|||
use OCA\AdminAudit\AuditLogger;
|
||||
use OCA\GroupFolders\Folder\FolderManager;
|
||||
use OCA\OpenProject\AppInfo\Application;
|
||||
use OCA\OpenProject\Exception\OpenprojectAvatarErrorException;
|
||||
use OCA\OpenProject\Exception\OpenprojectErrorException;
|
||||
use OCA\OpenProject\Exception\OpenprojectGroupfolderSetupConflictException;
|
||||
use OCA\OpenProject\Exception\OpenprojectResponseException;
|
||||
|
@ -319,11 +320,41 @@ class OpenProjectAPIService {
|
|||
$response = $this->rawRequest(
|
||||
$accessToken, $openprojectUrl, 'users/'.$openprojectUserId.'/avatar'
|
||||
);
|
||||
$imageMimeType = $response->getHeader('Content-Type');
|
||||
$imageData = $response->getBody();
|
||||
|
||||
// Check if the 'Content-Type' header exists
|
||||
if (empty($imageMimeType)) {
|
||||
throw new OpenprojectAvatarErrorException(
|
||||
'The response does not contain an image content-type.'
|
||||
);
|
||||
}
|
||||
|
||||
// check if response contains image
|
||||
if (!@imagecreatefromstring($imageData)) {
|
||||
throw new OpenprojectAvatarErrorException(
|
||||
'The response contains invalid image data.'
|
||||
);
|
||||
}
|
||||
|
||||
// check mimetype of response with content-type value
|
||||
// Create a temporary file
|
||||
$tempImageFile = tempnam(sys_get_temp_dir(), 'image');
|
||||
file_put_contents($tempImageFile, $imageData);
|
||||
|
||||
// Get the MIME type of the temporary file
|
||||
$imageMimeTypeFromImageData = mime_content_type($tempImageFile);
|
||||
unlink($tempImageFile);
|
||||
if ($imageMimeType != $imageMimeTypeFromImageData) {
|
||||
throw new OpenprojectAvatarErrorException(
|
||||
"The content-type header is '$imageMimeType ' but the mime-type of the image is '$imageMimeTypeFromImageData'."
|
||||
);
|
||||
}
|
||||
return [
|
||||
'avatar' => $response->getBody(),
|
||||
'type' => $response->getHeader('Content-Type'),
|
||||
'avatar' => $imageData,
|
||||
'type' => $imageMimeType ,
|
||||
];
|
||||
} catch (ServerException | ClientException | ConnectException | Exception $e) {
|
||||
} catch (ServerException | ClientException | ConnectException | OpenprojectAvatarErrorException | Exception $e) {
|
||||
$this->logger->debug('Error while getting OpenProject avatar for user ' . $openprojectUserId . ': ' . $e->getMessage(), ['app' => $this->appName]);
|
||||
$avatar = $this->avatarManager->getGuestAvatar($openprojectUserName);
|
||||
$avatarContent = $avatar->getFile(64)->getContent();
|
||||
|
|
|
@ -1237,6 +1237,110 @@ class OpenProjectAPIServiceTest extends TestCase {
|
|||
$this->assertSame('image/jpeg', $result['type']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group ignoreWithPHP8.0
|
||||
* @return void
|
||||
*/
|
||||
public function testGetOpenProjectAvatarWithNoContentType() {
|
||||
$consumerRequest = new ConsumerRequest();
|
||||
$consumerRequest
|
||||
->setMethod('GET')
|
||||
->setPath('/api/v3/users/openProjectUserWithAvatar/avatar')
|
||||
->setHeaders(["Authorization" => "Bearer 1234567890"]);
|
||||
|
||||
$providerResponse = new ProviderResponse();
|
||||
|
||||
$providerResponse
|
||||
->setStatus(Http::STATUS_OK)
|
||||
->setBody(null);
|
||||
|
||||
$this->builder
|
||||
->uponReceiving('a request to get the avatar of a user that has an avatar')
|
||||
->with($consumerRequest)
|
||||
->willRespondWith($providerResponse);
|
||||
$service = $this->getOpenProjectAPIService(null, '1234567890', 'https://nc.my-server.org', 'NCuser');
|
||||
$result = $service->getOpenProjectAvatar(
|
||||
'openProjectUserWithAvatar',
|
||||
'Me',
|
||||
'NCuser'
|
||||
);
|
||||
$this->assertArrayHasKey('avatar', $result);
|
||||
// make sure its an image
|
||||
$this->assertNotFalse(imagecreatefromstring($result['avatar']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @group ignoreWithPHP8.0
|
||||
* @return void
|
||||
*/
|
||||
public function testGetOpenProjectAvatarWithMisMatchContentType() {
|
||||
$consumerRequest = new ConsumerRequest();
|
||||
$consumerRequest
|
||||
->setMethod('GET')
|
||||
->setPath('/api/v3/users/openProjectUserWithAvatar/avatar')
|
||||
->setHeaders(["Authorization" => "Bearer 1234567890"]);
|
||||
|
||||
$providerResponse = new ProviderResponse();
|
||||
|
||||
$providerResponse
|
||||
->setStatus(Http::STATUS_OK)
|
||||
->setHeaders(['Content-Type' => 'image/png'])
|
||||
->setBody(
|
||||
new Binary(
|
||||
__DIR__ . "/../fixtures/openproject-icon.jpg",
|
||||
'image/jpeg'
|
||||
)
|
||||
);
|
||||
|
||||
$this->builder
|
||||
->uponReceiving('a request to get the avatar of a user that has an avatar')
|
||||
->with($consumerRequest)
|
||||
->willRespondWith($providerResponse);
|
||||
$service = $this->getOpenProjectAPIService(null, '1234567890', 'https://nc.my-server.org', 'NCuser');
|
||||
$result = $service->getOpenProjectAvatar(
|
||||
'openProjectUserWithAvatar',
|
||||
'Me',
|
||||
'NCuser'
|
||||
);
|
||||
$this->assertArrayHasKey('avatar', $result);
|
||||
// make sure its an image
|
||||
$this->assertNotFalse(imagecreatefromstring($result['avatar']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group ignoreWithPHP8.0
|
||||
* @return void
|
||||
*/
|
||||
public function testGetOpenProjectAvatarWithInvalidImageData() {
|
||||
$consumerRequest = new ConsumerRequest();
|
||||
$consumerRequest
|
||||
->setMethod('GET')
|
||||
->setPath('/api/v3/users/openProjectUserWithAvatar/avatar')
|
||||
->setHeaders(["Authorization" => "Bearer 1234567890"]);
|
||||
|
||||
$providerResponse = new ProviderResponse();
|
||||
|
||||
$providerResponse
|
||||
->setStatus(Http::STATUS_OK)
|
||||
->setHeaders(['Content-Type' => 'text/plain'])
|
||||
->setBody("Something in text form");
|
||||
|
||||
$this->builder
|
||||
->uponReceiving('a request to get the avatar of a user that has an avatar')
|
||||
->with($consumerRequest)
|
||||
->willRespondWith($providerResponse);
|
||||
$service = $this->getOpenProjectAPIService(null, '1234567890', 'https://nc.my-server.org', 'NCuser');
|
||||
$result = $service->getOpenProjectAvatar(
|
||||
'openProjectUserWithAvatar',
|
||||
'Me',
|
||||
'NCuser'
|
||||
);
|
||||
$this->assertArrayHasKey('avatar', $result);
|
||||
// make sure its an image
|
||||
$this->assertNotFalse(imagecreatefromstring($result['avatar']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group ignoreWithPHP8.0
|
||||
* @return void
|
||||
|
@ -1263,8 +1367,8 @@ class OpenProjectAPIServiceTest extends TestCase {
|
|||
'testUser'
|
||||
);
|
||||
$this->assertArrayHasKey('avatar', $result);
|
||||
//make sure its an image, if something else is returned it will throw an exception
|
||||
imagecreatefromstring($result['avatar']);
|
||||
// make sure its an image
|
||||
$this->assertNotFalse(imagecreatefromstring($result['avatar']));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Загрузка…
Ссылка в новой задаче