2022-10-29 18:05:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2022 Varun Patil <radialapps@gmail.com>
|
|
|
|
* @author Varun Patil <radialapps@gmail.com>
|
|
|
|
* @license AGPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Memories\Controller;
|
|
|
|
|
2022-11-10 06:43:58 +00:00
|
|
|
use OCA\Memories\AppInfo\Application;
|
2023-03-23 20:32:23 +00:00
|
|
|
use OCA\Memories\Exceptions;
|
2022-10-29 18:05:05 +00:00
|
|
|
use OCA\Memories\Exif;
|
2023-04-16 06:51:05 +00:00
|
|
|
use OCA\Memories\Service;
|
2023-03-23 20:32:23 +00:00
|
|
|
use OCA\Memories\Util;
|
2022-10-29 18:05:05 +00:00
|
|
|
use OCP\AppFramework\Http;
|
2022-12-03 05:04:31 +00:00
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
2022-10-29 18:05:05 +00:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2022-12-07 23:31:59 +00:00
|
|
|
use OCP\Files\IRootFolder;
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-03-22 18:40:56 +00:00
|
|
|
class ImageController extends GenericApiController
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
2022-12-03 05:04:31 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* Get preview of image
|
|
|
|
*/
|
|
|
|
public function preview(
|
|
|
|
int $id,
|
|
|
|
int $x = 32,
|
|
|
|
int $y = 32,
|
|
|
|
bool $a = false,
|
2023-10-15 01:59:00 +00:00
|
|
|
string $mode = 'fill',
|
2023-10-14 08:25:50 +00:00
|
|
|
): Http\Response {
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($id, $x, $y, $a, $mode) {
|
|
|
|
if (-1 === $id || 0 === $x || 0 === $y) {
|
|
|
|
throw Exceptions::MissingParameter('id, x, y');
|
|
|
|
}
|
2022-12-03 05:04:31 +00:00
|
|
|
|
2023-03-23 21:45:56 +00:00
|
|
|
$file = $this->fs->getUserFile($id);
|
2022-12-04 17:57:31 +00:00
|
|
|
$preview = \OC::$server->get(\OCP\IPreview::class)->getPreview($file, $x, $y, !$a, $mode);
|
2022-12-04 17:40:58 +00:00
|
|
|
$response = new FileDisplayResponse($preview, Http::STATUS_OK, [
|
|
|
|
'Content-Type' => $preview->getMimeType(),
|
2022-12-03 05:04:31 +00:00
|
|
|
]);
|
|
|
|
$response->cacheFor(3600 * 24, false, true);
|
|
|
|
|
|
|
|
return $response;
|
2023-03-23 20:32:23 +00:00
|
|
|
});
|
2022-12-03 05:04:31 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 23:31:59 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* Get preview of many images
|
|
|
|
*/
|
2023-11-01 04:29:43 +00:00
|
|
|
public function multipreview(array $files): Http\Response
|
2022-12-07 23:46:52 +00:00
|
|
|
{
|
2023-11-01 04:29:43 +00:00
|
|
|
return Util::guardExDirect(function (Http\IOutput $out) use ($files) {
|
2023-04-02 06:05:29 +00:00
|
|
|
// Filter files with valid parameters
|
2023-10-15 19:46:35 +00:00
|
|
|
$files = array_filter($files, static function (array $file) {
|
2023-04-02 06:05:29 +00:00
|
|
|
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
|
2023-10-15 19:46:35 +00:00
|
|
|
usort($files, static function (array $a, array $b) {
|
2023-04-02 06:05:29 +00:00
|
|
|
$aArea = (int) $a['x'] * (int) $a['y'];
|
|
|
|
$bArea = (int) $b['x'] * (int) $b['y'];
|
|
|
|
|
|
|
|
return $aArea <=> $bArea;
|
|
|
|
});
|
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
/** @var \OCP\IPreview */
|
2023-03-23 20:32:23 +00:00
|
|
|
$previewManager = \OC::$server->get(\OCP\IPreview::class);
|
2022-12-07 23:31:59 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// For checking max previews
|
|
|
|
$previewRoot = new \OC\Preview\Storage\Root(
|
|
|
|
\OC::$server->get(IRootFolder::class),
|
|
|
|
\OC::$server->get(\OC\SystemConfig::class),
|
|
|
|
);
|
2022-12-07 23:31:59 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// stream the response
|
2023-04-20 20:02:23 +00:00
|
|
|
$out->setHeader('Content-Type: application/octet-stream');
|
2022-12-07 23:31:59 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
foreach ($files as $bodyFile) {
|
|
|
|
$reqid = $bodyFile['reqid'];
|
|
|
|
$fileid = (int) $bodyFile['fileid'];
|
|
|
|
$x = (int) $bodyFile['x'];
|
|
|
|
$y = (int) $bodyFile['y'];
|
|
|
|
$a = '1' === $bodyFile['a'];
|
2022-12-07 23:31:59 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
try {
|
|
|
|
// Make sure max preview exists
|
2023-03-24 00:19:05 +00:00
|
|
|
$file = $this->fs->getUserFile($fileid);
|
2023-03-23 20:32:23 +00:00
|
|
|
$fileId = (string) $file->getId();
|
|
|
|
$folder = $previewRoot->getFolder($fileId);
|
|
|
|
$hasMax = false;
|
|
|
|
foreach ($folder->getDirectoryListing() as $preview) {
|
|
|
|
$name = $preview->getName();
|
|
|
|
if (str_contains($name, '-max')) {
|
|
|
|
$hasMax = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$hasMax) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add this preview to the response
|
|
|
|
$preview = $previewManager->getPreview($file, $x, $y, !$a, \OCP\IPreview::MODE_FILL);
|
|
|
|
$content = $preview->getContent();
|
|
|
|
if (empty($content)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ob_start();
|
2023-04-02 05:35:11 +00:00
|
|
|
// Encode parameters
|
|
|
|
$json = json_encode([
|
2023-03-23 20:32:23 +00:00
|
|
|
'reqid' => $reqid,
|
2023-04-02 05:35:11 +00:00
|
|
|
'len' => \strlen($content),
|
|
|
|
'type' => $preview->getMimeType(),
|
2023-03-23 20:32:23 +00:00
|
|
|
]);
|
2023-04-02 05:35:11 +00:00
|
|
|
|
|
|
|
// Send the length of the json as a single byte
|
2023-04-20 20:02:23 +00:00
|
|
|
$out->setOutput(\chr(\strlen($json)));
|
|
|
|
$out->setOutput($json);
|
2023-04-02 05:35:11 +00:00
|
|
|
|
|
|
|
// Send the image
|
2023-04-20 20:02:23 +00:00
|
|
|
$out->setOutput($content);
|
2023-03-23 20:32:23 +00:00
|
|
|
ob_end_flush();
|
|
|
|
} catch (\OCP\Files\NotFoundException $e) {
|
|
|
|
continue;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-12-07 23:31:59 +00:00
|
|
|
}
|
2023-03-23 20:32:23 +00:00
|
|
|
});
|
2022-12-07 23:31:59 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 18:05:05 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
2022-12-03 04:07:06 +00:00
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* Get EXIF info for an image with file id
|
2022-10-29 18:05:05 +00:00
|
|
|
*
|
|
|
|
* @param string fileid
|
|
|
|
*/
|
2022-12-03 04:44:24 +00:00
|
|
|
public function info(
|
2023-08-04 01:57:23 +00:00
|
|
|
int $id,
|
2022-12-03 04:44:24 +00:00
|
|
|
bool $basic = false,
|
2023-03-08 03:12:43 +00:00
|
|
|
bool $current = false,
|
2023-08-04 02:43:57 +00:00
|
|
|
bool $tags = false,
|
2023-10-15 01:59:00 +00:00
|
|
|
string $clusters = '',
|
2023-03-19 03:38:37 +00:00
|
|
|
): Http\Response {
|
2023-08-26 02:38:33 +00:00
|
|
|
return Util::guardEx(function () use ($id, $basic, $current, $tags, $clusters) {
|
2023-08-04 01:57:23 +00:00
|
|
|
$file = $this->fs->getUserFile($id);
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Get the image info
|
2023-10-14 06:51:12 +00:00
|
|
|
$info = $this->tq->getInfoById($id, $basic);
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-04-16 18:13:58 +00:00
|
|
|
// Add fileid and etag
|
|
|
|
$info['fileid'] = $file->getId();
|
|
|
|
$info['etag'] = $file->getEtag();
|
|
|
|
|
2023-08-04 02:43:57 +00:00
|
|
|
// Inject permissions and convert to string
|
2023-10-22 20:47:12 +00:00
|
|
|
$info['permissions'] = Util::permissionsToStr($file->getPermissions());
|
2023-08-04 02:43:57 +00:00
|
|
|
|
|
|
|
// Inject other file parameters that are cheap to get now
|
|
|
|
$info['mimetype'] = $file->getMimeType();
|
|
|
|
$info['size'] = $file->getSize();
|
|
|
|
$info['basename'] = $file->getName();
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Allow these ony for logged in users
|
2023-10-15 19:46:35 +00:00
|
|
|
if ($user = $this->userSession->getUser()) {
|
2023-08-26 02:55:04 +00:00
|
|
|
// Get the path of the file relative to current user
|
|
|
|
// "/admin/files/Photos/Camera/20230821_135017.jpg" => "/Photos/..."
|
|
|
|
$parts = explode('/', $file->getPath());
|
|
|
|
if (\count($parts) > 3 && 'files' === $parts[2] && $parts[1] === $user->getUID()) {
|
|
|
|
$info['filename'] = '/'.implode('/', \array_slice($parts, 3));
|
2023-08-26 02:38:33 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Get list of tags for this file
|
|
|
|
if ($tags) {
|
2023-08-04 02:43:57 +00:00
|
|
|
$info['tags'] = $this->getTags($id);
|
2023-03-23 20:32:23 +00:00
|
|
|
}
|
2023-03-07 22:38:37 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Get latest exif data if requested
|
|
|
|
if ($current) {
|
|
|
|
$info['current'] = Exif::getExifFromFile($file);
|
|
|
|
}
|
2023-03-10 00:39:26 +00:00
|
|
|
|
2023-08-04 02:43:57 +00:00
|
|
|
// Get clusters for this file
|
|
|
|
if ($clusters) {
|
|
|
|
$clist = [];
|
|
|
|
foreach (explode(',', $clusters) as $type) {
|
|
|
|
$backend = \OC::$server->get(\OCA\Memories\ClustersBackend\Manager::class)->get($type);
|
|
|
|
if ($backend->isEnabled()) {
|
|
|
|
$clist[$type] = $backend->getClusters($id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$info['clusters'] = $clist;
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 16:58:43 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
return new JSONResponse($info, Http::STATUS_OK);
|
|
|
|
});
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-10 06:24:38 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
2022-11-10 06:19:44 +00:00
|
|
|
* Set the exif data for a file.
|
2022-10-29 18:05:05 +00:00
|
|
|
*/
|
2023-08-04 05:15:27 +00:00
|
|
|
public function setExif(int $id, array $raw): Http\Response
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($id, $raw) {
|
2023-08-04 05:15:27 +00:00
|
|
|
$file = $this->fs->getUserFile($id);
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Check if user has permissions
|
|
|
|
if (!$file->isUpdateable() || Util::isEncryptionEnabled()) {
|
|
|
|
throw Exceptions::ForbiddenFileUpdate($file->getName());
|
|
|
|
}
|
2022-11-21 10:13:43 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Check if allowed to edit file
|
|
|
|
$mime = $file->getMimeType();
|
|
|
|
if (!\in_array($mime, Exif::allowedEditMimetypes(), true)) {
|
|
|
|
$name = $file->getName();
|
2023-03-17 07:12:06 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
throw Exceptions::Forbidden("Cannot edit file {$name} (blacklisted type {$mime})");
|
|
|
|
}
|
2023-03-17 07:12:06 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Set the exif data
|
2023-03-17 08:11:13 +00:00
|
|
|
Exif::setFileExif($file, $raw);
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_OK);
|
|
|
|
});
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
2022-11-10 05:39:13 +00:00
|
|
|
|
|
|
|
/**
|
2022-11-10 06:43:58 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
2023-03-11 01:30:42 +00:00
|
|
|
* @PublicPage
|
|
|
|
*
|
2023-03-15 19:28:33 +00:00
|
|
|
* Get a full resolution decodable image for editing from a file.
|
2023-03-15 21:06:30 +00:00
|
|
|
* The returned image may be png / webp / jpeg / gif.
|
2023-03-15 19:28:33 +00:00
|
|
|
* These formats are supported by all browsers.
|
2022-11-10 05:39:13 +00:00
|
|
|
*/
|
2023-03-19 03:38:37 +00:00
|
|
|
public function decodable(string $id): Http\Response
|
2022-11-10 05:39:13 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($id) {
|
2023-03-23 21:45:56 +00:00
|
|
|
$file = $this->fs->getUserFile((int) $id);
|
2022-11-10 05:39:13 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Check if valid image
|
|
|
|
$mimetype = $file->getMimeType();
|
|
|
|
if (!\in_array($mimetype, Application::IMAGE_MIMES, true)) {
|
|
|
|
throw Exceptions::Forbidden('Not an image');
|
|
|
|
}
|
2022-11-10 05:39:13 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
/** @var string Blob of image */
|
|
|
|
$blob = $file->getContent();
|
|
|
|
|
|
|
|
// Convert image to JPEG if required
|
|
|
|
if (!\in_array($mimetype, ['image/png', 'image/webp', 'image/jpeg', 'image/gif'], true)) {
|
2023-03-29 22:05:05 +00:00
|
|
|
[$blob, $mimetype] = $this->getImageJPEG($blob, $mimetype);
|
2023-03-23 20:32:23 +00:00
|
|
|
}
|
2022-11-10 05:39:13 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Return the image
|
|
|
|
$response = new Http\DataDisplayResponse($blob, Http::STATUS_OK, ['Content-Type' => $mimetype]);
|
|
|
|
$response->cacheFor(3600 * 24, false, false);
|
2022-11-10 05:39:13 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
return $response;
|
|
|
|
});
|
2022-11-10 05:39:13 +00:00
|
|
|
}
|
2023-03-07 22:38:37 +00:00
|
|
|
|
2023-04-16 06:51:05 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
|
|
|
public function editImage(
|
|
|
|
int $id,
|
|
|
|
string $name,
|
|
|
|
int $width,
|
|
|
|
int $height,
|
2023-05-28 07:58:45 +00:00
|
|
|
?float $quality,
|
2023-04-16 06:51:05 +00:00
|
|
|
string $extension,
|
2023-10-15 01:59:00 +00:00
|
|
|
array $state,
|
2023-04-16 06:51:05 +00:00
|
|
|
): Http\Response {
|
2023-04-16 06:59:29 +00:00
|
|
|
return Util::guardEx(function () use ($id, $name, $width, $height, $quality, $extension, $state) {
|
2023-04-16 06:51:05 +00:00
|
|
|
// Get the file
|
|
|
|
$file = $this->fs->getUserFile($id);
|
|
|
|
|
|
|
|
// Check if creating a copy
|
|
|
|
$copy = $name !== $file->getName();
|
|
|
|
|
|
|
|
// Check if user has permissions to do this
|
|
|
|
if (!$file->isUpdateable() || ($copy && !$file->getParent()->isCreatable())) {
|
|
|
|
throw Exceptions::ForbiddenFileUpdate($file->getName());
|
|
|
|
}
|
|
|
|
|
2023-04-16 07:05:41 +00:00
|
|
|
// Check if target copy file exists
|
|
|
|
if ($copy && $file->getParent()->nodeExists($name)) {
|
|
|
|
throw Exceptions::ForbiddenFileUpdate($name);
|
|
|
|
}
|
|
|
|
|
2023-04-16 06:51:05 +00:00
|
|
|
// Check if we have imagick
|
|
|
|
if (!class_exists('Imagick')) {
|
|
|
|
throw Exceptions::Forbidden('Imagick extension is not available');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the image
|
|
|
|
$image = new \Imagick();
|
|
|
|
$image->readImageBlob($file->getContent());
|
|
|
|
|
2023-08-22 17:46:53 +00:00
|
|
|
// Due to a bug in filerobot, the provided width and height may be swapped
|
|
|
|
// 1. If the user does not rotate the image, we're fine
|
|
|
|
// 2. If image is rotated and user doesn't change the save resolution,
|
|
|
|
// the wxh corresponds to the original image, not the rotated one
|
|
|
|
// 3. If image is rotated and user changes the save resolution,
|
|
|
|
// the wxh corresponds to the rotated image.
|
|
|
|
$iw = $image->getImageWidth();
|
|
|
|
$ih = $image->getImageHeight();
|
|
|
|
$shouldResize = $width !== $iw || $height !== $ih;
|
|
|
|
|
2023-04-16 06:51:05 +00:00
|
|
|
// Apply the edits
|
|
|
|
(new Service\FileRobotMagick($image, $state))->apply();
|
|
|
|
|
2023-04-16 06:59:29 +00:00
|
|
|
// Resize the image
|
2023-08-22 17:46:53 +00:00
|
|
|
$iw = $image->getImageWidth();
|
|
|
|
$ih = $image->getImageHeight();
|
|
|
|
if ($shouldResize && $width && $height && ($iw !== $width || $ih !== $height)) {
|
2023-04-16 06:59:29 +00:00
|
|
|
$image->resizeImage($width, $height, \Imagick::FILTER_LANCZOS, 1, true);
|
|
|
|
}
|
|
|
|
|
2023-05-28 07:58:45 +00:00
|
|
|
// Set image format
|
2023-04-16 06:51:05 +00:00
|
|
|
$image->setImageFormat($extension);
|
2023-05-28 07:58:45 +00:00
|
|
|
|
|
|
|
// Set quality if specified
|
|
|
|
if (null !== $quality && $quality >= 0 && $quality <= 1) {
|
|
|
|
$image->setImageCompressionQuality((int) round(100 * $quality));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the image
|
2023-04-16 06:51:05 +00:00
|
|
|
$blob = $image->getImageBlob();
|
|
|
|
|
|
|
|
// Save the file
|
|
|
|
if ($copy) {
|
|
|
|
$file = $file->getParent()->newFile($name, $blob);
|
|
|
|
} else {
|
|
|
|
$file->putContent($blob);
|
|
|
|
}
|
|
|
|
|
2023-04-16 17:52:54 +00:00
|
|
|
// Make sure the preview is updated
|
|
|
|
\OC::$server->get(\OCP\IPreview::class)->getPreview($file);
|
|
|
|
|
2023-08-04 01:57:23 +00:00
|
|
|
return $this->info($file->getId(), true);
|
2023-04-16 06:51:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-29 22:05:05 +00:00
|
|
|
/**
|
|
|
|
* Given a blob of image data, return a JPEG blob.
|
|
|
|
*
|
2023-03-29 23:19:31 +00:00
|
|
|
* @param string $blob Blob of image data in any format
|
2023-03-29 22:05:05 +00:00
|
|
|
* @param string $mimetype Mimetype of image data
|
|
|
|
*
|
2023-10-15 19:46:35 +00:00
|
|
|
* @return string[] [blob, mimetype]
|
|
|
|
*
|
|
|
|
* @psalm-return list{string, string}
|
2023-03-29 22:05:05 +00:00
|
|
|
*/
|
2023-03-29 23:19:31 +00:00
|
|
|
private function getImageJPEG($blob, $mimetype): array
|
|
|
|
{
|
2023-03-29 22:05:05 +00:00
|
|
|
// TODO: Use imaginary if available
|
|
|
|
|
|
|
|
// Check if Imagick is available
|
2023-03-29 23:19:31 +00:00
|
|
|
if (!class_exists('Imagick')) {
|
2023-03-29 22:05:05 +00:00
|
|
|
throw Exceptions::Forbidden('Imagick extension is not available');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read original image
|
|
|
|
try {
|
|
|
|
$image = new \Imagick();
|
|
|
|
$image->readImageBlob($blob);
|
|
|
|
} catch (\ImagickException $e) {
|
2023-03-29 23:19:31 +00:00
|
|
|
throw Exceptions::Forbidden('Imagick failed to read image: '.$e->getMessage());
|
2023-03-29 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to JPEG
|
|
|
|
try {
|
2023-03-29 22:08:44 +00:00
|
|
|
$image->autoOrient();
|
2023-03-29 22:05:05 +00:00
|
|
|
$image->setImageFormat('jpeg');
|
2023-05-18 00:36:05 +00:00
|
|
|
$image->setImageCompressionQuality(85);
|
2023-03-29 22:05:05 +00:00
|
|
|
$blob = $image->getImageBlob();
|
|
|
|
$mimetype = $image->getImageMimeType();
|
|
|
|
} catch (\ImagickException $e) {
|
2023-03-29 23:19:31 +00:00
|
|
|
throw Exceptions::Forbidden('Imagick failed to convert image: '.$e->getMessage());
|
2023-07-23 22:26:41 +00:00
|
|
|
} finally {
|
|
|
|
$image->clear();
|
2023-03-29 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return [$blob, $mimetype];
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:38:37 +00:00
|
|
|
/**
|
|
|
|
* Get the tags for a file.
|
2023-10-15 19:46:35 +00:00
|
|
|
*
|
|
|
|
* @return string[]
|
2023-03-07 22:38:37 +00:00
|
|
|
*/
|
|
|
|
private function getTags(int $fileId): array
|
|
|
|
{
|
|
|
|
// Make sure tags are enabled
|
2023-03-23 20:49:26 +00:00
|
|
|
if (!Util::tagsIsEnabled()) {
|
2023-03-07 22:38:37 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the tag ids for this file
|
|
|
|
$objectMapper = \OC::$server->get(\OCP\SystemTag\ISystemTagObjectMapper::class);
|
|
|
|
$tagIds = $objectMapper->getTagIdsForObjects([$fileId], 'files')[(string) $fileId];
|
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
// Get all matching tag objects
|
|
|
|
$tags = \OC::$server->get(\OCP\SystemTag\ISystemTagManager::class)->getTagsByIds($tagIds);
|
2023-03-08 02:49:21 +00:00
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
// Filter out the tags that are not user visible
|
2023-08-30 18:10:08 +00:00
|
|
|
$visible = array_filter($tags, static fn ($t) => $t->isUserVisible());
|
2023-03-23 20:32:23 +00:00
|
|
|
|
|
|
|
// Get the tag names
|
2023-08-30 18:10:08 +00:00
|
|
|
return array_map(static fn ($t) => $t->getName(), $visible);
|
2023-03-07 22:38:37 +00:00
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|