memories/lib/Controller/ImageController.php

133 lines
3.9 KiB
PHP
Raw Normal View History

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;
2022-10-29 18:05:05 +00:00
use OCA\Memories\Exif;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
class ImageController extends ApiBase
{
/**
* @NoAdminRequired
*
* Get image info for one file
*
* @param string fileid
*/
public function info(string $id): JSONResponse
{
2022-11-10 06:19:44 +00:00
$file = $this->getUserFile((int) $id);
if (!$file) {
2022-10-29 18:05:05 +00:00
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
// Get the image info
2022-11-07 21:25:52 +00:00
$basic = false !== $this->request->getParam('basic', false);
$info = $this->timelineQuery->getInfoById($file->getId(), $basic);
2022-10-29 18:05:05 +00:00
// Get latest exif data if requested
if ($this->request->getParam('current', false)) {
2022-11-10 06:19:44 +00:00
$info['current'] = Exif::getExifFromFile($file);
}
2022-10-29 18:05:05 +00:00
return new JSONResponse($info, Http::STATUS_OK);
}
/**
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-11-10 06:24:38 +00:00
*
* @param string fileid
2022-10-29 18:05:05 +00:00
*/
2022-11-10 06:19:44 +00:00
public function setExif(string $id): JSONResponse
2022-10-29 18:05:05 +00:00
{
2022-11-10 06:19:44 +00:00
$file = $this->getUserFile((int) $id);
if (!$file) {
2022-10-29 18:05:05 +00:00
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
// Check if user has permissions
if (!$file->isUpdateable()) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
2022-11-10 06:19:44 +00:00
// Get original file from body
$exif = $this->request->getParam('raw');
$path = $file->getStorage()->getLocalFile($file->getInternalPath());
2022-10-29 18:05:05 +00:00
try {
2022-11-10 06:19:44 +00:00
Exif::setExif($path, $exif);
2022-10-29 18:05:05 +00:00
} catch (\Exception $e) {
2022-11-10 06:19:44 +00:00
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
2022-10-29 18:05:05 +00:00
}
2022-11-10 06:19:44 +00:00
// Update remote file if not local
if (!$file->getStorage()->isLocal()) {
$file->putContent(fopen($path, 'r')); // closes the handler
2022-10-29 18:05:05 +00:00
}
// Reprocess the file
$this->timelineWrite->processFile($file, true);
2022-11-10 06:19:44 +00:00
return new JSONResponse([], Http::STATUS_OK);
2022-10-29 18:05:05 +00:00
}
/**
2022-11-10 06:43:58 +00:00
* @NoAdminRequired
*
* @NoCSRFRequired
*
* Get a full resolution JPEG for editing from a file.
*/
2022-11-10 06:43:58 +00:00
public function jpeg(string $id)
{
2022-11-10 06:19:44 +00:00
$file = $this->getUserFile((int) $id);
if (!$file) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
2022-11-10 06:43:58 +00:00
// check if valid image
$mimetype = $file->getMimeType();
if (!\in_array($mimetype, Application::IMAGE_MIMES, true)) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
2022-11-10 06:19:44 +00:00
// Get the image
$path = $file->getStorage()->getLocalFile($file->getInternalPath());
2022-11-10 06:43:58 +00:00
$image = new \Imagick($path);
$image->setImageFormat('jpeg');
$image->setImageCompressionQuality(95);
$blob = $image->getImageBlob();
2022-11-10 06:19:44 +00:00
// Return the image
2022-11-10 06:43:58 +00:00
$response = new Http\DataDisplayResponse($blob, Http::STATUS_OK, ['Content-Type' => $image->getImageMimeType()]);
$response->cacheFor(3600 * 24, false, false);
2022-11-10 06:19:44 +00:00
return $response;
}
2022-10-29 18:05:05 +00:00
}