Skip to content

Commit

Permalink
refs #107 fix contact photo import, get the photo type from response …
Browse files Browse the repository at this point in the history
…headers

Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
Julien Veyssier committed Aug 24, 2022
1 parent 900c357 commit b0a6545
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
5 changes: 4 additions & 1 deletion lib/Service/GoogleAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ public function simpleRequest(string $userId, string $url, array $params = [], s
if ($respCode >= 400) {
return ['error' => $this->l10n->t('Bad credentials')];
} else {
return ['content' => $body];
return [
'content' => $body,
'headers' => $response->getHeaders(),
];
}
} catch (ServerException | ClientException $e) {
$this->logger->warning('Google API error : '.$e->getMessage(), ['app' => $this->appName]);
Expand Down
48 changes: 30 additions & 18 deletions lib/Service/GoogleContactsAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,31 +192,43 @@ public function importContacts(string $userId, ?string $uri, int $key, ?string $
foreach ($c['photos'] as $photo) {
if (isset($photo['url'])) {
// determine photo type
$type = '';
$type = 'JPEG';
if (preg_match('/\.jpg$/i', $photo['url']) || preg_match('/\.jpeg$/i', $photo['url'])) {
$type = 'JPEG';
} elseif (preg_match('/\.png$/i', $photo['url'])) {
$type = 'PNG';
}
if ($type !== '') {
$photoFile = $this->googleApiService->simpleRequest($userId, $photo['url']);
if (!isset($photoFile['error'])) {
$b64Photo = stripslashes('data:image/' . strtolower($type) . ';base64\,') . base64_encode($photoFile['content']);
try {
$prop = $vCard->createProperty(
'PHOTO',
$b64Photo,
[
'type' => $type,
// 'encoding' => 'b',
]
);
$vCard->add($prop);
} catch (Exception | Throwable $ex) {
$this->logger->warning('Error when setting contact photo "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => $this->appName]);
$photoFile = $this->googleApiService->simpleRequest($userId, $photo['url']);
if (!isset($photoFile['error'])) {
// try again to determine photo type from response headers
if (isset($photoFile['headers'], $photoFile['headers']['Content-Type'])) {
if (is_array($photoFile['headers']['Content-Type']) && count($photoFile['headers']['Content-Type']) > 0) {
$contentType = $photoFile['headers']['Content-Type'][0];
} else {
$contentType = $photoFile['headers']['Content-Type'];
}
break;
if ($contentType === 'image/png') {
$type = 'PNG';
} elseif ($contentType === 'image/jpeg') {
$type = 'JPEG';
}
}

$b64Photo = stripslashes('data:image/' . strtolower($type) . ';base64\,') . base64_encode($photoFile['content']);
try {
$prop = $vCard->createProperty(
'PHOTO',
$b64Photo,
[
'type' => $type,
// 'encoding' => 'b',
]
);
$vCard->add($prop);
} catch (Exception|Throwable $ex) {
$this->logger->warning('Error when setting contact photo "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => $this->appName]);
}
break;
}
}
}
Expand Down

0 comments on commit b0a6545

Please sign in to comment.