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-12-04 17:40:58 +00:00
|
|
|
use OCA\Memories\Db\TimelineWrite;
|
2022-10-29 18:05:05 +00:00
|
|
|
use OCA\Memories\Exif;
|
|
|
|
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
|
|
|
|
|
|
|
class ImageController extends ApiBase
|
|
|
|
{
|
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,
|
|
|
|
string $mode = 'fill'
|
|
|
|
) {
|
|
|
|
if (-1 === $id || 0 === $x || 0 === $y) {
|
|
|
|
return new JSONResponse([
|
|
|
|
'message' => 'Invalid parameters',
|
|
|
|
], Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = $this->getUserFile($id);
|
|
|
|
if (!$file) {
|
|
|
|
return new JSONResponse([
|
|
|
|
'message' => 'File not found',
|
|
|
|
], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
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;
|
|
|
|
} catch (\OCP\Files\NotFoundException $e) {
|
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 23:31:59 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* Get preview of many images
|
|
|
|
*/
|
2022-12-07 23:46:52 +00:00
|
|
|
public function multipreview()
|
|
|
|
{
|
2022-12-07 23:31:59 +00:00
|
|
|
// read body to array
|
|
|
|
try {
|
|
|
|
$body = file_get_contents('php://input');
|
|
|
|
$files = json_decode($body, true);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var \OCP\IPreview $previewManager */
|
|
|
|
$previewManager = \OC::$server->get(\OCP\IPreview::class);
|
|
|
|
|
|
|
|
// For checking max previews
|
|
|
|
$previewRoot = new \OC\Preview\Storage\Root(
|
|
|
|
\OC::$server->get(IRootFolder::class),
|
|
|
|
\OC::$server->getSystemConfig(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// stream the response
|
|
|
|
header('Content-Type: application/octet-stream');
|
2022-12-07 23:46:52 +00:00
|
|
|
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 7 * 3600 * 24));
|
|
|
|
header('Cache-Control: max-age='. 7 * 3600 * 24 .', private');
|
2022-12-07 23:31:59 +00:00
|
|
|
|
|
|
|
foreach ($files as $bodyFile) {
|
2022-12-08 21:37:55 +00:00
|
|
|
if (!isset($bodyFile['reqid']) || !isset($bodyFile['fileid']) || !isset($bodyFile['x']) || !isset($bodyFile['y']) || !isset($bodyFile['a'])) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-12-07 23:31:59 +00:00
|
|
|
$reqid = $bodyFile['reqid'];
|
|
|
|
$fileid = (int) $bodyFile['fileid'];
|
|
|
|
$x = (int) $bodyFile['x'];
|
|
|
|
$y = (int) $bodyFile['y'];
|
2022-12-07 23:46:52 +00:00
|
|
|
$a = '1' === $bodyFile['a'];
|
2022-12-08 21:37:55 +00:00
|
|
|
if ($fileid <= 0 || $x <= 0 || $y <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-12-07 23:31:59 +00:00
|
|
|
|
|
|
|
$file = $this->getUserFile($fileid);
|
|
|
|
if (!$file) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-12-08 21:37:55 +00:00
|
|
|
try {
|
|
|
|
// Make sure max preview exists
|
|
|
|
$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;
|
2022-12-07 23:31:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 21:37:55 +00:00
|
|
|
// Add this preview to the response
|
2022-12-07 23:31:59 +00:00
|
|
|
$preview = $previewManager->getPreview($file, $x, $y, !$a, 'fill');
|
|
|
|
$content = $preview->getContent();
|
|
|
|
if (empty($content)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo json_encode([
|
|
|
|
'reqid' => $reqid,
|
|
|
|
'Content-Length' => \strlen($content),
|
|
|
|
'Content-Type' => $preview->getMimeType(),
|
|
|
|
]);
|
|
|
|
echo "\n";
|
|
|
|
echo $content;
|
|
|
|
flush();
|
2022-12-08 21:37:55 +00:00
|
|
|
} catch (\OCP\Files\NotFoundException $e) {
|
|
|
|
continue;
|
2022-12-07 23:31:59 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
string $id,
|
|
|
|
bool $basic = false,
|
|
|
|
bool $current = false
|
|
|
|
): 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
|
|
|
$info = $this->timelineQuery->getInfoById($file->getId(), $basic);
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2022-11-10 05:39:13 +00:00
|
|
|
// Get latest exif data if requested
|
2022-12-03 04:07:06 +00:00
|
|
|
// Allow this ony for logged in users
|
2022-12-03 04:44:24 +00:00
|
|
|
if ($current && null !== $this->userSession->getUser()) {
|
2022-11-10 06:19:44 +00:00
|
|
|
$info['current'] = Exif::getExifFromFile($file);
|
2022-11-10 05:39:13 +00:00
|
|
|
}
|
|
|
|
|
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
|
2022-11-15 13:40:46 +00:00
|
|
|
if (!$file->isUpdateable() || !($file->getPermissions() & \OCP\Constants::PERMISSION_UPDATE)) {
|
2022-10-29 18:05:05 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
|
|
|
|
2022-11-21 10:13:43 +00:00
|
|
|
// Check for end-to-end encryption
|
2022-12-04 17:33:20 +00:00
|
|
|
if (\OCA\Memories\Util::isEncryptionEnabled()) {
|
2022-11-21 10:13:43 +00:00
|
|
|
return new JSONResponse(['message' => 'Cannot change encrypted file'], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
|
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
|
2022-12-04 17:40:58 +00:00
|
|
|
$timelineWrite = new TimelineWrite($this->connection);
|
|
|
|
$timelineWrite->processFile($file, true);
|
2022-10-29 18:05:05 +00:00
|
|
|
|
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 05:39:13 +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 05:39:13 +00:00
|
|
|
*/
|
2022-11-10 06:43:58 +00:00
|
|
|
public function jpeg(string $id)
|
2022-11-10 05:39:13 +00:00
|
|
|
{
|
2022-11-10 06:19:44 +00:00
|
|
|
$file = $this->getUserFile((int) $id);
|
|
|
|
if (!$file) {
|
2022-11-10 05:39:13 +00:00
|
|
|
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 05:39:13 +00:00
|
|
|
|
2022-11-10 06:19:44 +00:00
|
|
|
// Get the image
|
2022-11-10 05:39:13 +00:00
|
|
|
$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 05:39:13 +00:00
|
|
|
|
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 05:39:13 +00:00
|
|
|
|
2022-11-10 06:19:44 +00:00
|
|
|
return $response;
|
2022-11-10 05:39:13 +00:00
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|