Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
enh: refactor Classifier#getConvertedFilePath
Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Nov 5, 2023
commit 0737a349ffd44638785091389a8ae69fb6c778b3
157 changes: 81 additions & 76 deletions lib/Classifiers/Classifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,106 +261,111 @@ private function getConvertedFilePath(Node $file): string {
return $path;
}

// Create a temporary file *with the correct extension*
$tmpname = $this->tempManager->getTemporaryFile('.jpg');

if (!$this->previewProvider->isAvailable($file) && !($file instanceof File)) {
return $path;
}

$use_gd = $this->config->getSystemValueString('recognize.preview.gd', 'true');
$use_gd_quality = (int)$this->config->getSystemValue('recognize.preview.quality', '100');
try {
$this->logger->debug('generating preview of ' . $file->getId() . ' with dimension '.self::TEMP_FILE_DIMENSION);

$imagetype = exif_imagetype($path); //To troubleshoot console errors, GD does not support all formats.
if (0 < $imagetype && $use_gd == 'true') {
$image = imagecreatefromstring(file_get_contents($path));
$width = imagesx($image);
$height = imagesy($image);

$maxWidth = self::TEMP_FILE_DIMENSION;
$maxHeight = self::TEMP_FILE_DIMENSION;

if ($width > $maxWidth || $height > $maxHeight) {
$aspectRatio = $width / $height;
if ($width > $height) {
$newWidth = $maxWidth;
$newHeight = $maxWidth / $aspectRatio;
} else {
$newHeight = $maxHeight;
$newWidth = $maxHeight * $aspectRatio;
}
$preview = imagescale($image, (int)$newWidth, (int)$newHeight);
}
$imageType = exif_imagetype($path); //To troubleshoot console errors, GD does not support all formats.
if (0 < $imageType && $this->config->getSystemValueString('recognize.preview.gd', 'true') === 'true') {
return $this->generatePrevieWithGD($path);
} else {
$image = $this->previewProvider->getPreview($file, self::TEMP_FILE_DIMENSION, self::TEMP_FILE_DIMENSION);

try {
$preview = $image->read();
} catch (NotPermittedException $e) {
$this->logger->warning('Could not read preview file', ['exception' => $e]);
}

if ($preview === false) {
$this->logger->warning('Could not open preview file');
if (!$this->previewProvider->isAvailable($file)) {
return $path;
}
return $this->generatePreviewWithProvider($file);
}
} catch(\Throwable $e) {
$this->logger->warning('Failed to generate preview of ' . $file->getId() . ' with dimension '.self::TEMP_FILE_DIMENSION . ': ' . $e->getMessage());
return $path;
}
}

$tmpfile = fopen($tmpname, 'wb');

if ($tmpfile === false) {
$this->logger->warning('Could not open tmpfile');
return $path;
}
public function cleanUpTmpFiles():void {
$this->tempManager->clean();
}

if (stream_copy_to_stream($preview, $tmpfile) === false) {
$this->logger->info('Could not copy preview file to temp folder');
fclose($preview);
fclose($tmpfile);
}
fclose($preview);
fclose($tmpfile);
/**
* @param File $file
* @return string
* @throws \OCA\Recognize\Exception\Exception|NotFoundException
*/
public function generatePreviewWithProvider(File $file): string {
$image = $this->previewProvider->getPreview($file, self::TEMP_FILE_DIMENSION, self::TEMP_FILE_DIMENSION);

$imagetype = exif_imagetype($tmpname);
try {
$preview = $image->read();
} catch (NotPermittedException $e) {
throw new \OCA\Recognize\Exception\Exception('Could not read preview file', 0, $e);
}

if (in_array($imagetype, [IMAGETYPE_WEBP, IMAGETYPE_AVIF, false])) { // To troubleshoot if it is a webp or avif.
$preview = imagecreatefromstring(file_get_contents($tmpname));
unlink($tmpname);
} else {
return $tmpname;
}
}
} catch(\Throwable $e) {
$this->logger->info('Failed to generate preview of ' . $file->getId() . ' with dimension '.self::TEMP_FILE_DIMENSION . ': ' . $e->getMessage());
return $path;
if ($preview === false) {
throw new \OCA\Recognize\Exception\Exception('Could not open preview file');
}

// Create a temporary file *with the correct extension*
$tmpname = $this->tempManager->getTemporaryFile('.jpg');

$tmpfile = fopen($tmpname, 'wb');

if ($tmpfile === false) {
$this->logger->warning('Could not open tmpfile');
return $path;
throw new \OCA\Recognize\Exception\Exception('Could not open tmpfile');
}

if ($preview === false) {
$this->logger->warning('Could not open preview file');
return $path;
$copyResult = stream_copy_to_stream($preview, $tmpfile);
fclose($preview);
fclose($tmpfile);

if ($copyResult === false) {
throw new \OCA\Recognize\Exception\Exception('Could not copy preview file to temp folder');
}

$this->logger->debug('Copying ' . $file->getId() . ' preview to tempfolder');
$imagetype = exif_imagetype($tmpname);

if (imagejpeg($preview, $tmpfile, $use_gd_quality) === false) {
$this->logger->warning('Could not copy preview file to temp folder');
fclose($tmpfile);
return $path;
if (in_array($imagetype, [IMAGETYPE_WEBP, IMAGETYPE_AVIF, false])) { // To troubleshoot if it is a webp or avif.
$previewImage = imagecreatefromstring(file_get_contents($tmpname));
$use_gd_quality = (int)$this->config->getSystemValue('recognize.preview.quality', '100');
if (imagejpeg($previewImage, $tmpname, $use_gd_quality) === false) {
throw new \OCA\Recognize\Exception\Exception('Could not copy preview file to temp folder');
}
}
fclose($tmpfile);

return $tmpname;
}

public function cleanUpTmpFiles():void {
$this->tempManager->clean();
/**
* @param string $path
* @return string
* @throws \OCA\Recognize\Exception\Exception
*/
public function generatePrevieWithGD(string $path): string {
$image = imagecreatefromstring(file_get_contents($path));
$width = imagesx($image);
$height = imagesy($image);

$maxWidth = self::TEMP_FILE_DIMENSION;
$maxHeight = self::TEMP_FILE_DIMENSION;

if ($width > $maxWidth || $height > $maxHeight) {
$aspectRatio = $width / $height;
if ($width > $height) {
$newWidth = $maxWidth;
$newHeight = $maxWidth / $aspectRatio;
} else {
$newHeight = $maxHeight;
$newWidth = $maxHeight * $aspectRatio;
}
$previewImage = imagescale($image, (int)$newWidth, (int)$newHeight);
} else {
return $path;
}

// Create a temporary file *with the correct extension*
$tmpname = $this->tempManager->getTemporaryFile('.jpg');

$use_gd_quality = (int)$this->config->getSystemValue('recognize.preview.quality', '100');
if (imagejpeg($previewImage, $tmpname, $use_gd_quality) === false) {
throw new \OCA\Recognize\Exception\Exception('Could not copy preview file to temp folder');
}

return $tmpname;
}
}