Skip to content
Merged
4 changes: 4 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
'routes' => [
['name' => 'Image#insertImageLink', 'url' => '/image/link', 'verb' => 'POST'],
['name' => 'Image#uploadImage', 'url' => '/image/upload', 'verb' => 'POST'],
['name' => 'Image#getImage', 'url' => '/image', 'verb' => 'GET'],
['name' => 'Image#insertImageLinkPublic', 'url' => '/public/image/link', 'verb' => 'POST'],
['name' => 'Image#uploadImagePublic', 'url' => '/public/image/upload', 'verb' => 'POST'],
['name' => 'Image#getImagePublic', 'url' => '/public/image', 'verb' => 'GET'],

['name' => 'Session#create', 'url' => '/session/create', 'verb' => 'PUT'],
['name' => 'Session#fetch', 'url' => '/session/fetch', 'verb' => 'POST'],
Expand Down
156 changes: 146 additions & 10 deletions lib/Controller/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,29 @@
namespace OCA\Text\Controller;

use Exception;
use OCA\Text\AppInfo\Application;
use OCP\AppFramework\Http;
use OCA\Text\Service\ImageService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
use Psr\Log\LoggerInterface;

class ImageController extends Controller {

public const IMAGE_MIME_TYPES = [
'image/png',
'image/jpeg',
'image/jpg',
'image/gif',
'image/x-xbitmap',
'image/x-ms-bmp',
'image/bmp',
'image/svg+xml',
'image/webp',
];

/**
* @var string|null
*/
Expand All @@ -42,45 +57,166 @@ class ImageController extends Controller {
* @var ImageService
*/
private $imageService;
/**
* @var LoggerInterface
*/
private $logger;

public function __construct(string $appName,
IRequest $request,
LoggerInterface $logger,
ImageService $imageService,
?string $userId) {
parent::__construct($appName, $request);
$this->userId = $userId;
$this->imageService = $imageService;
$this->request = $request;
$this->logger = $logger;
}

/**
* @NoAdminRequired
*
* @param int $textFileId
* @param string $link
* @return DataResponse
*/
public function insertImageLink(string $link): DataResponse {
$downloadResult = $this->imageService->insertImageLink($link, $this->userId);
if (isset($downloadResult['error'])) {
return new DataResponse($downloadResult, Http::STATUS_BAD_REQUEST);
} else {
return new DataResponse($downloadResult);
public function insertImageLink(int $textFileId, string $link): DataResponse {
try {
$downloadResult = $this->imageService->insertImageLink($textFileId, $link, $this->userId);
if (isset($downloadResult['error'])) {
return new DataResponse($downloadResult, Http::STATUS_BAD_REQUEST);
} else {
return new DataResponse($downloadResult);
}
} catch (Exception $e) {
return new DataResponse(['error' => 'Link insertion error: ' . $e->getMessage()], Http::STATUS_BAD_REQUEST);
}
}

/**
* @NoAdminRequired
* @PublicPage
*
* @param int|null $textFileId can be null with public file share
* @param string $link
* @param string $shareToken
* @return DataResponse
*/
public function insertImageLinkPublic(?int $textFileId, string $link, string $shareToken): DataResponse {
try {
$downloadResult = $this->imageService->insertImageLinkPublic($textFileId, $link, $shareToken);
if (isset($downloadResult['error'])) {
return new DataResponse($downloadResult, Http::STATUS_BAD_REQUEST);
} else {
return new DataResponse($downloadResult);
}
} catch (Exception $e) {
return new DataResponse(['error' => 'Link insertion error: ' . $e->getMessage()], Http::STATUS_BAD_REQUEST);
}
}

/**
* @NoAdminRequired
*
* @param int $textFileId
* @return DataResponse
*/
public function uploadImage(string $textFilePath): DataResponse {
public function uploadImage(int $textFileId): DataResponse {
try {
$file = $this->request->getUploadedFile('image');
if ($file !== null && isset($file['tmp_name'], $file['name'])) {
if ($file !== null && isset($file['tmp_name'], $file['name'], $file['type'])) {
if (!in_array($file['type'], self::IMAGE_MIME_TYPES)) {
return new DataResponse(['error' => 'Image type not supported'], Http::STATUS_BAD_REQUEST);
}
$newFileContent = file_get_contents($file['tmp_name']);
$newFileName = $file['name'];
$uploadResult = $this->imageService->uploadImage($textFilePath, $newFileName, $newFileContent, $this->userId);
return new DataResponse($uploadResult);
$uploadResult = $this->imageService->uploadImage($textFileId, $newFileName, $newFileContent, $this->userId);
if (isset($uploadResult['error'])) {
return new DataResponse($uploadResult, Http::STATUS_BAD_REQUEST);
} else {
return new DataResponse($uploadResult);
}
} else {
return new DataResponse(['error' => 'No uploaded file'], Http::STATUS_BAD_REQUEST);
}
} catch (Exception $e) {
$this->logger->error('Upload error: ' . $e->getMessage(), ['app' => Application::APP_NAME]);
return new DataResponse(['error' => 'Upload error'], Http::STATUS_BAD_REQUEST);
}
}

/**
* @NoAdminRequired
* @PublicPage
*
* @param int|null $textFileId can be null with public file share
* @param string $shareToken
* @return DataResponse
*/
public function uploadImagePublic(?int $textFileId, string $shareToken): DataResponse {
try {
$file = $this->request->getUploadedFile('image');
if ($file !== null && isset($file['tmp_name'], $file['name'], $file['type'])) {
if (!in_array($file['type'], self::IMAGE_MIME_TYPES)) {
return new DataResponse(['error' => 'Image type not supported'], Http::STATUS_BAD_REQUEST);
}
$newFileContent = file_get_contents($file['tmp_name']);
$newFileName = $file['name'];
$uploadResult = $this->imageService->uploadImagePublic($textFileId, $newFileName, $newFileContent, $shareToken);
if (isset($uploadResult['error'])) {
return new DataResponse($uploadResult, Http::STATUS_BAD_REQUEST);
} else {
return new DataResponse($uploadResult);
}
} else {
return new DataResponse(['error' => 'No uploaded file'], Http::STATUS_BAD_REQUEST);
}
} catch (Exception $e) {
$this->logger->error('Upload error: ' . $e->getMessage(), ['app' => Application::APP_NAME]);
return new DataResponse(['error' => 'Upload error'], Http::STATUS_BAD_REQUEST);
}
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*
* Serve the images in the editor
* @param int $textFileId
* @param string $imageFileName
* @return DataDisplayResponse
* @throws \OCP\Files\InvalidPathException
* @throws \OCP\Files\NotFoundException
* @throws \OCP\Files\NotPermittedException
* @throws \OCP\Lock\LockedException
* @throws \OC\User\NoUserException
*/
public function getImage(int $textFileId, string $imageFileName): DataDisplayResponse {
$imageFile = $this->imageService->getImage($textFileId, $imageFileName, $this->userId);
if ($imageFile !== null) {
return new DataDisplayResponse($imageFile->getContent(), Http::STATUS_OK, ['Content-Type' => $imageFile->getMimeType()]);
} else {
return new DataDisplayResponse('', Http::STATUS_NOT_FOUND);
}
}

/**
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
*
* @param int $textFileId
* @param string $imageFileName
* @param string $shareToken
* @return DataDisplayResponse
*/
public function getImagePublic(int $textFileId, string $imageFileName, string $shareToken): DataDisplayResponse {
$imageFile = $this->imageService->getImagePublic($textFileId, $imageFileName, $shareToken);
if ($imageFile !== null) {
return new DataDisplayResponse($imageFile->getContent(), Http::STATUS_OK, ['Content-Type' => $imageFile->getMimeType()]);
} else {
return new DataDisplayResponse('', Http::STATUS_NOT_FOUND);
}
}
}
Loading