From cccc9798c04430f66a62dfaeb45c7ea32d3d3055 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sat, 1 Apr 2023 23:05:29 -0700 Subject: [PATCH] image: sort multipreview priority by size Signed-off-by: Varun Patil --- lib/Controller/ImageController.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/Controller/ImageController.php b/lib/Controller/ImageController.php index fa26b0e1..dc6cecf0 100644 --- a/lib/Controller/ImageController.php +++ b/lib/Controller/ImageController.php @@ -82,6 +82,22 @@ class ImageController extends GenericApiController $body = file_get_contents('php://input'); $files = json_decode($body, true); + // Filter files with valid parameters + $files = array_filter($files, function ($file) { + return isset($file['reqid'], $file['fileid'], $file['x'], $file['y'], $file['a']) + && (int) $file['fileid'] > 0 + && (int) $file['x'] > 0 + && (int) $file['y'] > 0; + }); + + // Sort files by size, ascending + usort($files, function ($a, $b) { + $aArea = (int) $a['x'] * (int) $a['y']; + $bArea = (int) $b['x'] * (int) $b['y']; + + return $aArea <=> $bArea; + }); + /** @var \OCP\IPreview $previewManager */ $previewManager = \OC::$server->get(\OCP\IPreview::class); @@ -95,17 +111,11 @@ class ImageController extends GenericApiController header('Content-Type: application/octet-stream'); foreach ($files as $bodyFile) { - if (!isset($bodyFile['reqid']) || !isset($bodyFile['fileid']) || !isset($bodyFile['x']) || !isset($bodyFile['y']) || !isset($bodyFile['a'])) { - continue; - } $reqid = $bodyFile['reqid']; $fileid = (int) $bodyFile['fileid']; $x = (int) $bodyFile['x']; $y = (int) $bodyFile['y']; $a = '1' === $bodyFile['a']; - if ($fileid <= 0 || $x <= 0 || $y <= 0) { - continue; - } try { // Make sure max preview exists