Check if ifile is present and add fallback mechanism

Signed-off-by: Christian Wolf <github@christianwolf.email>
fix/wrong-image-type
Christian Wolf 3 years ago
parent 7fc3f8ef9f
commit 9fc057b078

@ -670,30 +670,55 @@ class OC_Image implements \OCP\IImage {
*/ */
private function updateImageTypes($buffer) { private function updateImageTypes($buffer) {
if ($this->valid()) { if ($this->valid()) {
$finfo = new finfo(FILEINFO_MIME_TYPE); if ($this->fileInfo) {
$this->mimeType = $finfo->buffer($buffer); $this->mimeType = $this->fileInfo->buffer($buffer);
switch ($this->mimeType) {
case 'image/gif': switch ($this->mimeType) {
$this->imageType = IMAGETYPE_GIF; case 'image/gif':
break; $this->imageType = IMAGETYPE_GIF;
case 'image/jpeg': break;
$this->imageType = IMAGETYPE_JPEG; case 'image/jpeg':
break; $this->imageType = IMAGETYPE_JPEG;
case 'image/png': break;
$this->imageType = IMAGETYPE_PNG; case 'image/png':
break; $this->imageType = IMAGETYPE_PNG;
case 'image/xbm': break;
$this->imageType = IMAGETYPE_XBM; case 'image/xbm':
break; $this->imageType = IMAGETYPE_XBM;
case 'image/vnd.wap.wbmp': break;
$this->imageType = IMAGETYPE_WBMP; case 'image/vnd.wap.wbmp':
break; $this->imageType = IMAGETYPE_WBMP;
case 'image/bmp': break;
$this->imageType = IMAGETYPE_BMP; case 'image/bmp':
break; $this->imageType = IMAGETYPE_BMP;
case 'image/webp': break;
$this->imageType = IMAGETYPE_WEBP; case 'image/webp':
break; $this->imageType = IMAGETYPE_WEBP;
break;
default:
return false;
}
return true;
} else {
// Fallback: Use temporary file
$this->logger->debug('OC_Image->updateImageTypes, finfo not set, using temporary file as a fallback', ['app' => 'core']);
$tmpFile = tmpfile();
$tmpFileName = stream_get_meta_data($tmpFile)['uri'];
fwrite($tmpFile, $buffer);
fflush($tmpFile);
if (filesize($tmpFileName) < 12) {
return false;
}
$this->imageType = exif_imagetype($tmpFileName);
$this->mimeType = image_type_to_mime_type($this->imageType);
fclose($tmpFile);
return true;
} }
} }
} }
@ -713,7 +738,9 @@ class OC_Image implements \OCP\IImage {
imagealphablending($this->resource, false); imagealphablending($this->resource, false);
imagesavealpha($this->resource, true); imagesavealpha($this->resource, true);
} }
$this->updateImageTypes($str); if (!$this->updateImageTypes($str)) {
return false;
}
if (!$this->resource) { if (!$this->resource) {
$this->logger->debug('OC_Image->loadFromFile, could not load', ['app' => 'core']); $this->logger->debug('OC_Image->loadFromFile, could not load', ['app' => 'core']);
@ -735,7 +762,9 @@ class OC_Image implements \OCP\IImage {
$data = base64_decode($str); $data = base64_decode($str);
if ($data) { // try to load from string data if ($data) { // try to load from string data
$this->resource = @imagecreatefromstring($data); $this->resource = @imagecreatefromstring($data);
$this->updateImageTypes($data); if ($this->updateImageTypes($data)) {
return false;
}
if (!$this->resource) { if (!$this->resource) {
$this->logger->debug('OC_Image->loadFromBase64, could not load', ['app' => 'core']); $this->logger->debug('OC_Image->loadFromBase64, could not load', ['app' => 'core']);
return false; return false;

Loading…
Cancel
Save