Skip to content

Commit 0fc4fe7

Browse files
committed
feat(api): File conversion API
Signed-off-by: Elizabeth Danzberger <lizzy7128@tutanota.de>
1 parent e686b93 commit 0fc4fe7

File tree

19 files changed

+777
-2
lines changed

19 files changed

+777
-2
lines changed

apps/testing/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
'OCA\\Testing\\Controller\\ConfigController' => $baseDir . '/../lib/Controller/ConfigController.php',
1313
'OCA\\Testing\\Controller\\LockingController' => $baseDir . '/../lib/Controller/LockingController.php',
1414
'OCA\\Testing\\Controller\\RateLimitTestController' => $baseDir . '/../lib/Controller/RateLimitTestController.php',
15+
'OCA\\Testing\\Conversion\\ConversionProvider' => $baseDir . '/../lib/Conversion/ConversionProvider.php',
1516
'OCA\\Testing\\Listener\\GetDeclarativeSettingsValueListener' => $baseDir . '/../lib/Listener/GetDeclarativeSettingsValueListener.php',
1617
'OCA\\Testing\\Listener\\RegisterDeclarativeSettingsListener' => $baseDir . '/../lib/Listener/RegisterDeclarativeSettingsListener.php',
1718
'OCA\\Testing\\Listener\\SetDeclarativeSettingsValueListener' => $baseDir . '/../lib/Listener/SetDeclarativeSettingsValueListener.php',

apps/testing/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ComposerStaticInitTesting
2727
'OCA\\Testing\\Controller\\ConfigController' => __DIR__ . '/..' . '/../lib/Controller/ConfigController.php',
2828
'OCA\\Testing\\Controller\\LockingController' => __DIR__ . '/..' . '/../lib/Controller/LockingController.php',
2929
'OCA\\Testing\\Controller\\RateLimitTestController' => __DIR__ . '/..' . '/../lib/Controller/RateLimitTestController.php',
30+
'OCA\\Testing\\Conversion\\ConversionProvider' => __DIR__ . '/..' . '/../lib/Conversion/ConversionProvider.php',
3031
'OCA\\Testing\\Listener\\GetDeclarativeSettingsValueListener' => __DIR__ . '/..' . '/../lib/Listener/GetDeclarativeSettingsValueListener.php',
3132
'OCA\\Testing\\Listener\\RegisterDeclarativeSettingsListener' => __DIR__ . '/..' . '/../lib/Listener/RegisterDeclarativeSettingsListener.php',
3233
'OCA\\Testing\\Listener\\SetDeclarativeSettingsValueListener' => __DIR__ . '/..' . '/../lib/Listener/SetDeclarativeSettingsValueListener.php',

apps/testing/lib/AppInfo/Application.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace OCA\Testing\AppInfo;
88

99
use OCA\Testing\AlternativeHomeUserBackend;
10+
use OCA\Testing\Conversion\ConversionProvider;
1011
use OCA\Testing\Listener\GetDeclarativeSettingsValueListener;
1112
use OCA\Testing\Listener\RegisterDeclarativeSettingsListener;
1213
use OCA\Testing\Listener\SetDeclarativeSettingsValueListener;
@@ -49,6 +50,8 @@ public function register(IRegistrationContext $context): void {
4950
$context->registerTaskProcessingProvider(FakeTranscribeProvider::class);
5051
$context->registerTaskProcessingProvider(FakeContextWriteProvider::class);
5152

53+
$context->registerConversionProvider(ConversionProvider::class);
54+
5255
$context->registerDeclarativeSettings(DeclarativeSettingsForm::class);
5356
$context->registerEventListener(DeclarativeSettingsRegisterFormEvent::class, RegisterDeclarativeSettingsListener::class);
5457
$context->registerEventListener(DeclarativeSettingsGetValueEvent::class, GetDeclarativeSettingsValueListener::class);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Testing\Conversion;
11+
12+
use OCP\Conversion\ConversionMimeTuple;
13+
use OCP\Conversion\IConversionProvider;
14+
use OCP\Files\File;
15+
16+
class ConversionProvider implements IConversionProvider {
17+
18+
public function getName(): string {
19+
return 'testing';
20+
}
21+
22+
public function getSupportedMimeTypes(): array {
23+
$jpegConversions = new ConversionMimeTuple('image/jpeg', [
24+
'image/png',
25+
]);
26+
27+
return [$jpegConversions];
28+
}
29+
30+
public function convertFile(File $file, string $targetMimeType): mixed {
31+
$image = imagecreatefromstring($file->getContent());
32+
33+
imagepalettetotruecolor($image);
34+
35+
ob_start();
36+
imagepng($image);
37+
return ob_get_clean();
38+
}
39+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
11+
namespace OC\Core\Controller;
12+
13+
use OCP\AppFramework\Http;
14+
use OCP\AppFramework\Http\Attribute\ApiRoute;
15+
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
16+
use OCP\AppFramework\Http\Attribute\UserRateLimit;
17+
use OCP\AppFramework\Http\DataResponse;
18+
use OCP\AppFramework\OCS\OCSException;
19+
use OCP\AppFramework\OCS\OCSNotFoundException;
20+
use OCP\AppFramework\OCSController;
21+
use OCP\Conversion\IConversionManager;
22+
use OCP\Files\File;
23+
use OCP\Files\IRootFolder;
24+
use OCP\IRequest;
25+
26+
class ConversionApiController extends OCSController {
27+
public function __construct(
28+
string $appName,
29+
IRequest $request,
30+
private IConversionManager $conversionManager,
31+
private IRootFolder $rootFolder,
32+
private ?string $userId,
33+
) {
34+
parent::__construct($appName, $request);
35+
}
36+
37+
/**
38+
* Converts a file from one MIME type to another
39+
*
40+
* @param int $fileId ID of the file to be converted
41+
* @param string $targetMimeType The MIME type to which you want to convert the file
42+
* @param string|null $destination The target path of the converted file. Written to a temporary file if left empty
43+
*
44+
* @return DataResponse<Http::STATUS_CREATED, array{path: string}, array{}>
45+
*
46+
* 201: File was converted and written to the destination or temporary file
47+
*
48+
* @throws OCSException The file was unable to be converted
49+
* @throws OCSNotFoundException The file to be converted was not found
50+
*/
51+
#[NoAdminRequired]
52+
#[UserRateLimit(limit: 25, period: 120)]
53+
#[ApiRoute(verb: 'POST', url: '/convert', root: '/conversion')]
54+
public function convert(int $fileId, string $targetMimeType, ?string $destination = null): DataResponse {
55+
$userFolder = $this->rootFolder->getUserFolder($this->userId);
56+
$file = $userFolder->getFirstNodeById($fileId);
57+
58+
if (!($file instanceof File)) {
59+
throw new OCSNotFoundException();
60+
}
61+
62+
try {
63+
if ($destination !== null) {
64+
$destination = $userFolder->getFullpath($destination);
65+
}
66+
67+
$convertedFile = $this->conversionManager->convert($file, $targetMimeType, $destination);
68+
} catch (\Exception $e) {
69+
throw new OCSException($e->getMessage());
70+
}
71+
72+
return new DataResponse([
73+
'path' => $convertedFile,
74+
], Http::STATUS_CREATED);
75+
}
76+
}

core/openapi-full.json

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2535,6 +2535,133 @@
25352535
}
25362536
}
25372537
},
2538+
"/ocs/v2.php/conversion/convert": {
2539+
"post": {
2540+
"operationId": "conversion_api-convert",
2541+
"summary": "Converts a file from one MIME type to another",
2542+
"tags": [
2543+
"conversion_api"
2544+
],
2545+
"security": [
2546+
{
2547+
"bearer_auth": []
2548+
},
2549+
{
2550+
"basic_auth": []
2551+
}
2552+
],
2553+
"requestBody": {
2554+
"required": true,
2555+
"content": {
2556+
"application/json": {
2557+
"schema": {
2558+
"type": "object",
2559+
"required": [
2560+
"fileId",
2561+
"targetMimeType"
2562+
],
2563+
"properties": {
2564+
"fileId": {
2565+
"type": "integer",
2566+
"format": "int64",
2567+
"description": "ID of the file to be converted"
2568+
},
2569+
"targetMimeType": {
2570+
"type": "string",
2571+
"description": "The MIME type to which you want to convert the file"
2572+
},
2573+
"destination": {
2574+
"type": "string",
2575+
"nullable": true,
2576+
"description": "The target path of the converted file. Written to a temporary file if left empty"
2577+
}
2578+
}
2579+
}
2580+
}
2581+
}
2582+
},
2583+
"parameters": [
2584+
{
2585+
"name": "OCS-APIRequest",
2586+
"in": "header",
2587+
"description": "Required to be true for the API request to pass",
2588+
"required": true,
2589+
"schema": {
2590+
"type": "boolean",
2591+
"default": true
2592+
}
2593+
}
2594+
],
2595+
"responses": {
2596+
"201": {
2597+
"description": "File was converted and written to the destination or temporary file",
2598+
"content": {
2599+
"application/json": {
2600+
"schema": {
2601+
"type": "object",
2602+
"required": [
2603+
"ocs"
2604+
],
2605+
"properties": {
2606+
"ocs": {
2607+
"type": "object",
2608+
"required": [
2609+
"meta",
2610+
"data"
2611+
],
2612+
"properties": {
2613+
"meta": {
2614+
"$ref": "#/components/schemas/OCSMeta"
2615+
},
2616+
"data": {
2617+
"type": "object",
2618+
"required": [
2619+
"path"
2620+
],
2621+
"properties": {
2622+
"path": {
2623+
"type": "string"
2624+
}
2625+
}
2626+
}
2627+
}
2628+
}
2629+
}
2630+
}
2631+
}
2632+
}
2633+
},
2634+
"404": {
2635+
"description": "The file to be converted was not found",
2636+
"content": {
2637+
"application/json": {
2638+
"schema": {
2639+
"type": "object",
2640+
"required": [
2641+
"ocs"
2642+
],
2643+
"properties": {
2644+
"ocs": {
2645+
"type": "object",
2646+
"required": [
2647+
"meta",
2648+
"data"
2649+
],
2650+
"properties": {
2651+
"meta": {
2652+
"$ref": "#/components/schemas/OCSMeta"
2653+
},
2654+
"data": {}
2655+
}
2656+
}
2657+
}
2658+
}
2659+
}
2660+
}
2661+
}
2662+
}
2663+
}
2664+
},
25382665
"/ocs/v2.php/hovercard/v1/{userId}": {
25392666
"get": {
25402667
"operationId": "hover_card-get-user",

0 commit comments

Comments
 (0)