Skip to content

Commit 23e4606

Browse files
committed
feat: add preferred apps/providers and use them first
Signed-off-by: Elizabeth Danzberger <[email protected]>
1 parent 8c524cc commit 23e4606

9 files changed

Lines changed: 66 additions & 49 deletions

File tree

apps/files/lib/Capabilities.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OC\Files\FilenameValidator;
1111
use OCA\Files\Service\ChunkedUploadConfig;
1212
use OCP\Capabilities\ICapability;
13+
use OCP\Files\Conversion\ConversionMimeTuple;
1314
use OCP\Files\Conversion\IConversionManager;
1415

1516
class Capabilities implements ICapability {
@@ -23,7 +24,7 @@ public function __construct(
2324
/**
2425
* Return this classes capabilities
2526
*
26-
* @return array{files: array{'$comment': ?string, bigfilechunking: bool, blacklisted_files: list<mixed>, forbidden_filenames: list<string>, forbidden_filename_basenames: list<string>, forbidden_filename_characters: list<string>, forbidden_filename_extensions: list<string>, chunked_upload: array{max_size: int, max_parallel_count: int}, conversions: array<string, mixed>}}
27+
* @return array{files: array{'$comment': ?string, bigfilechunking: bool, blacklisted_files: list<mixed>, forbidden_filenames: list<string>, forbidden_filename_basenames: list<string>, forbidden_filename_characters: list<string>, forbidden_filename_extensions: list<string>, chunked_upload: array{max_size: int, max_parallel_count: int}, file_conversions: list<array{from: string, to: array<string, string>}>}}
2728
*/
2829
public function getCapabilities(): array {
2930
return [
@@ -41,7 +42,9 @@ public function getCapabilities(): array {
4142
'max_parallel_count' => ChunkedUploadConfig::getMaxParallelCount(),
4243
],
4344

44-
'conversions' => $this->fileConversionManager->getMimeTypes(),
45+
'file_conversions' => array_map(function (ConversionMimeTuple $mimeTuple) {
46+
return $mimeTuple->jsonSerialize();
47+
}, $this->fileConversionManager->getMimeTypes())
4548
],
4649
];
4750
}

apps/files/lib/Controller/ConversionApiController.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use OCP\AppFramework\Http;
1414
use OCP\AppFramework\Http\Attribute\ApiRoute;
1515
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
16-
use OCP\AppFramework\Http\Attribute\OpenAPI;
1716
use OCP\AppFramework\Http\Attribute\UserRateLimit;
1817
use OCP\AppFramework\Http\DataResponse;
1918
use OCP\AppFramework\OCS\OCSException;
@@ -22,6 +21,7 @@
2221
use OCP\AppFramework\OCSController;
2322
use OCP\Files\Conversion\IConversionManager;
2423
use OCP\Files\File;
24+
use OCP\Files\InvalidPathException;
2525
use OCP\Files\IRootFolder;
2626
use OCP\IL10N;
2727
use OCP\IRequest;
@@ -55,7 +55,6 @@ public function __construct(
5555
#[NoAdminRequired]
5656
#[UserRateLimit(limit: 25, period: 120)]
5757
#[ApiRoute(verb: 'POST', url: '/api/v1/convert')]
58-
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
5958
public function convert(int $fileId, string $targetMimeType, ?string $destination = null): DataResponse {
6059
$userFolder = $this->rootFolder->getUserFolder($this->userId);
6160
$file = $userFolder->getFirstNodeById($fileId);
@@ -81,12 +80,17 @@ public function convert(int $fileId, string $targetMimeType, ?string $destinatio
8180

8281
try {
8382
$convertedFile = $this->fileConversionManager->convert($file, $targetMimeType, $destination);
83+
$convertedFileRelativePath = PathHelper::getRelativePath($userFolder->getInternalPath(), $convertedFile);
84+
85+
if ($convertedFileRelativePath === null) {
86+
throw new InvalidPathException($this->l10n->t('Could not get relative path to converted file'));
87+
}
8488
} catch (\Exception $e) {
8589
throw new OCSException($e->getMessage());
8690
}
8791

8892
return new DataResponse([
89-
'path' => PathHelper::getRelativePath($userFolder->getInternalPath(), $convertedFile) ?? $convertedFile,
93+
'path' => $convertedFileRelativePath,
9094
], Http::STATUS_CREATED);
9195
}
9296
}

apps/files/openapi.json

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"forbidden_filename_characters",
3838
"forbidden_filename_extensions",
3939
"chunked_upload",
40-
"conversions",
40+
"file_conversions",
4141
"directEditing"
4242
],
4343
"properties": {
@@ -95,10 +95,25 @@
9595
}
9696
}
9797
},
98-
"conversions": {
99-
"type": "object",
100-
"additionalProperties": {
101-
"type": "object"
98+
"file_conversions": {
99+
"type": "array",
100+
"items": {
101+
"type": "object",
102+
"required": [
103+
"from",
104+
"to"
105+
],
106+
"properties": {
107+
"from": {
108+
"type": "string"
109+
},
110+
"to": {
111+
"type": "object",
112+
"additionalProperties": {
113+
"type": "string"
114+
}
115+
}
116+
}
102117
}
103118
},
104119
"directEditing": {

apps/files/tests/Controller/ConversionApiControllerTest.php

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,14 @@
2525
* @package OCA\Files\Controller
2626
*/
2727
class ConversionApiControllerTest extends TestCase {
28-
/** @var ConversionApiController */
29-
private $conversionApiController;
30-
31-
/** @var string */
32-
private $appName = 'files';
33-
34-
/** @var IRequest|MockObject */
35-
private $request;
36-
37-
/** @var IConversionManager|MockObject */
38-
private $fileConversionManager;
39-
40-
/** @var IRootFolder|MockObject */
41-
private $rootFolder;
42-
43-
/** @var File|MockObject */
44-
private $file;
45-
46-
/** @var Folder|MockObject */
47-
private $userFolder;
48-
49-
/** @var string */
50-
private $user;
28+
private string $appName = 'files';
29+
private ConversionApiController $conversionApiController;
30+
private IRequest&MockObject $request;
31+
private IConversionManager&MockObject $fileConversionManager;
32+
private IRootFolder&MockObject $rootFolder;
33+
private File&MockObject $file;
34+
private Folder&MockObject $userFolder;
35+
private string $user;
5136

5237
protected function setUp(): void {
5338
parent::setUp();

config/config.sample.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@
14341434
*
14351435
* Default: 100 MiB
14361436
*/
1437-
'max_conversion_filesize' => 100,
1437+
'max_file_conversion_filesize' => 100,
14381438

14391439
/**
14401440
* LDAP

lib/private/Files/Conversion/ConversionManager.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@
2626
use Throwable;
2727

2828
class ConversionManager implements IConversionManager {
29-
/** @var ?IConversionProvider[] */
30-
private ?array $providers = null;
29+
/** @var string[] */
30+
private array $preferredApps = [
31+
'richdocuments',
32+
];
33+
34+
/** @var IConversionProvider[] */
35+
private array $preferredProviders = [];
36+
37+
/** @var IConversionProvider[] */
38+
private array $providers = [];
3139

3240
public function __construct(
3341
private Coordinator $coordinator,
@@ -48,10 +56,10 @@ public function getMimeTypes(): array {
4856
$mimeTypes = [];
4957

5058
foreach ($this->getProviders() as $provider) {
51-
array_push($mimeTypes, $provider->getSupportedMimeType());
59+
$mimeTypes[] = $provider->getSupportedMimeType();
5260
}
5361

54-
return array_merge([], $mimeTypes);
62+
return $mimeTypes;
5563
}
5664

5765
public function convert(File $file, string $targetMimeType, ?string $destination = null): string {
@@ -61,7 +69,7 @@ public function convert(File $file, string $targetMimeType, ?string $destination
6169

6270
// Operate in mebibytes
6371
$fileSize = $file->getSize() / (1024 * 1024);
64-
$threshold = $this->config->getValue('max_conversion_filesize', 100);
72+
$threshold = $this->config->getValue('max_file_conversion_filesize', 100);
6573
if ($fileSize > $threshold) {
6674
throw new GenericFileException('File is too large to convert');
6775
}
@@ -96,17 +104,21 @@ public function convert(File $file, string $targetMimeType, ?string $destination
96104
}
97105

98106
public function getProviders(): array {
99-
if ($this->providers !== null) {
107+
if (count($this->providers) > 0) {
100108
return $this->providers;
101109
}
102110

103111
$context = $this->coordinator->getRegistrationContext();
104-
$this->providers = [];
105-
106112
foreach ($context->getFileConversionProviders() as $providerRegistration) {
107113
$class = $providerRegistration->getService();
114+
$appId = $providerRegistration->getAppId();
108115

109116
try {
117+
if (in_array($appId, $this->preferredApps)) {
118+
$this->preferredProviders[$class] = $this->serverContainer->get($class);
119+
continue;
120+
}
121+
110122
$this->providers[$class] = $this->serverContainer->get($class);
111123
} catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable $e) {
112124
$this->logger->error('Failed to load file conversion provider ' . $class, [
@@ -115,7 +127,7 @@ public function getProviders(): array {
115127
}
116128
}
117129

118-
return $this->providers;
130+
return array_merge([], $this->preferredProviders, $this->providers);
119131
}
120132

121133
private function writeToDestination(string $destination, mixed $content): File {

lib/public/Files/Conversion/ConversionMimeTuple.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
class ConversionMimeTuple implements JsonSerializable {
2121
/**
2222
* @param string $from The original MIME type of a file
23-
* @param array<string,string> $to The desired MIME type for the file mapped to its translated name
23+
* @param array<string, string> $to The desired MIME type for the file mapped to its translated name
2424
*
2525
* @since 31.0.0
2626
*/
@@ -31,6 +31,8 @@ public function __construct(
3131
}
3232

3333
/**
34+
* @return array{from: string, to: array<string, string>}
35+
*
3436
* @since 31.0.0
3537
*/
3638
public function jsonSerialize(): array {

lib/public/Files/Conversion/IConversionManager.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ interface IConversionManager {
1818
/**
1919
* Determines whether or not conversion providers are available
2020
*
21-
* @return bool
22-
*
2321
* @since 31.0.0
2422
*/
2523
public function hasProviders(): bool;
2624

2725
/**
2826
* Gets all supported MIME type conversions
2927
*
30-
* @return array<ConversionMimeTuple>
28+
* @return list<ConversionMimeTuple>
3129
*
3230
* @since 31.0.0
3331
*/

lib/public/Files/Conversion/IConversionProvider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ interface IConversionProvider {
2121
/**
2222
* Get the MIME type tuple this conversion provider supports
2323
*
24-
* @return ConversionMimeTuple
25-
*
2624
* @since 31.0.0
2725
*/
2826
public function getSupportedMimeType(): ConversionMimeTuple;

0 commit comments

Comments
 (0)