Skip to content

Commit

Permalink
Merge pull request #50208 from nextcloud/feat/conversion-adjusting
Browse files Browse the repository at this point in the history
fix(files): conversion api simplification and conflict check
  • Loading branch information
skjnldsv authored Jan 16, 2025
2 parents d9a7124 + 19ce362 commit 35db02c
Show file tree
Hide file tree
Showing 18 changed files with 358 additions and 126 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration-sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ jobs:
- 'federation_features'
- '--tags ~@large files_features'
- 'filesdrop_features'
- 'file_conversions'
- 'openldap_features'
- 'openldap_numerical_features'
- 'ldap_features'
Expand Down
10 changes: 5 additions & 5 deletions apps/files/lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use OC\Files\FilenameValidator;
use OCA\Files\Service\ChunkedUploadConfig;
use OCP\Capabilities\ICapability;
use OCP\Files\Conversion\ConversionMimeTuple;
use OCP\Files\Conversion\ConversionMimeProvider;
use OCP\Files\Conversion\IConversionManager;

class Capabilities implements ICapability {
Expand All @@ -24,7 +24,7 @@ public function __construct(
/**
* Return this classes capabilities
*
* @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: list<array{mime: string, name: string}>}>}}
* @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: string, extension: string, displayName: string}>}}
*/
public function getCapabilities(): array {
return [
Expand All @@ -42,9 +42,9 @@ public function getCapabilities(): array {
'max_parallel_count' => ChunkedUploadConfig::getMaxParallelCount(),
],

'file_conversions' => array_map(function (ConversionMimeTuple $mimeTuple) {
return $mimeTuple->jsonSerialize();
}, $this->fileConversionManager->getMimeTypes()),
'file_conversions' => array_map(function (ConversionMimeProvider $mimeProvider) {
return $mimeProvider->jsonSerialize();
}, $this->fileConversionManager->getProviders()),
],
];
}
Expand Down
27 changes: 10 additions & 17 deletions apps/files/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,22 @@
"type": "object",
"required": [
"from",
"to"
"to",
"extension",
"displayName"
],
"properties": {
"from": {
"type": "string"
},
"to": {
"type": "array",
"items": {
"type": "object",
"required": [
"mime",
"name"
],
"properties": {
"mime": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
"type": "string"
},
"extension": {
"type": "string"
},
"displayName": {
"type": "string"
}
}
}
Expand Down
19 changes: 13 additions & 6 deletions apps/testing/lib/Conversion/ConversionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace OCA\Testing\Conversion;

use OCP\Files\Conversion\ConversionMimeTuple;
use OCP\Files\Conversion\ConversionMimeProvider;
use OCP\Files\Conversion\IConversionProvider;
use OCP\Files\File;
use OCP\IL10N;
Expand All @@ -22,19 +22,26 @@ public function __construct(

public function getSupportedMimeTypes(): array {
return [
new ConversionMimeTuple('image/jpeg', [
['mime' => 'image/png', 'name' => $this->l10n->t('Image (.png)')],
])
new ConversionMimeProvider('image/jpeg', 'image/png', 'png', $this->l10n->t('Image (.png)')),
new ConversionMimeProvider('image/jpeg', 'image/gif', 'gif', $this->l10n->t('Image (.gif)')),
];
}

public function convertFile(File $file, string $targetMimeType): mixed {
$image = imagecreatefromstring($file->getContent());

imagepalettetotruecolor($image);

// Start output buffering
ob_start();
imagepng($image);

// Convert the image to the target format
if ($targetMimeType === 'image/gif') {
imagegif($image);
} else {
imagepng($image);
}

// End and return the output buffer
return ob_get_clean();
}
}
36 changes: 23 additions & 13 deletions build/integration/config/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ default:
- "%paths.base%/../features"
contexts:
- FeatureContext:
baseUrl: http://localhost:8080/ocs/
baseUrl: http://localhost:8080/ocs/
admin:
- admin
- admin
Expand All @@ -39,7 +39,7 @@ default:
- "%paths.base%/../comments_features"
contexts:
- FeatureContext:
baseUrl: http://localhost:8080/ocs/
baseUrl: http://localhost:8080/ocs/
admin:
- admin
- admin
Expand All @@ -62,7 +62,7 @@ default:
- "%paths.base%/../dav_features"
contexts:
- DavFeatureContext:
baseUrl: http://localhost:8080/ocs/
baseUrl: http://localhost:8080/ocs/
admin:
- admin
- admin
Expand All @@ -85,7 +85,7 @@ default:
- "%paths.base%/../federation_features"
contexts:
- FederationContext:
baseUrl: http://localhost:8080/ocs/
baseUrl: http://localhost:8080/ocs/
admin:
- admin
- admin
Expand All @@ -95,7 +95,7 @@ default:
- "%paths.base%/../files_features"
contexts:
- FeatureContext:
baseUrl: http://localhost:8080/ocs/
baseUrl: http://localhost:8080/ocs/
admin:
- admin
- admin
Expand All @@ -113,12 +113,22 @@ default:
- CommandLineContext:
baseUrl: http://localhost:8080
ocPath: ../../
files_conversion:
paths:
- "%paths.base%/../file_conversions"
contexts:
- ConversionsContext:
baseUrl: http://localhost:8080
admin:
- admin
- admin
regular_user_password: 123456
capabilities:
paths:
- "%paths.base%/../capabilities_features"
contexts:
- CapabilitiesContext:
baseUrl: http://localhost:8080/ocs/
baseUrl: http://localhost:8080/ocs/
admin:
- admin
- admin
Expand All @@ -128,7 +138,7 @@ default:
- "%paths.base%/../collaboration_features"
contexts:
- CollaborationContext:
baseUrl: http://localhost:8080/ocs/
baseUrl: http://localhost:8080/ocs/
admin:
- admin
- admin
Expand All @@ -138,7 +148,7 @@ default:
- "%paths.base%/../sharees_features"
contexts:
- ShareesContext:
baseUrl: http://localhost:8080/ocs/
baseUrl: http://localhost:8080/ocs/
admin:
- admin
- admin
Expand All @@ -148,7 +158,7 @@ default:
- "%paths.base%/../sharing_features"
contexts:
- SharingContext:
baseUrl: http://localhost:8080/ocs/
baseUrl: http://localhost:8080/ocs/
admin:
- admin
- admin
Expand All @@ -159,7 +169,7 @@ default:
- "%paths.base%/../videoverification_features"
contexts:
- SharingContext:
baseUrl: http://localhost:8080/ocs/
baseUrl: http://localhost:8080/ocs/
admin:
- admin
- admin
Expand All @@ -170,7 +180,7 @@ default:
- "%paths.base%/../setup_features"
contexts:
- SetupContext:
baseUrl: http://localhost:8080/ocs/
baseUrl: http://localhost:8080/ocs/
admin:
- admin
- admin
Expand Down Expand Up @@ -220,10 +230,10 @@ default:
- "%paths.base%/../remoteapi_features"
contexts:
- FeatureContext:
baseUrl: http://localhost:8080/ocs/
baseUrl: http://localhost:8080/ocs/
admin:
- admin
- admin
regular_user_password: 123456
- RemoteContext:
remote: http://localhost:8080
remote: http://localhost:8080
Binary file added build/integration/data/clouds.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions build/integration/data/clouds.jpg.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: 2019 CHUTTERSNAP <https://unsplash.com/@chuttersnap> <https://unsplash.com/photos/blue-clouds-under-white-sky-9AqIdzEc9pY>"
SPDX-License-Identifier: LicenseRef-Unsplash
3 changes: 3 additions & 0 deletions build/integration/features/bootstrap/BasicStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -170,6 +171,8 @@ public function sendingToWith($verb, $url, $body) {
$this->response = $client->request($verb, $fullUrl, $options);
} catch (ClientException $ex) {
$this->response = $ex->getResponse();
} catch (ServerException $ex) {
$this->response = $ex->getResponse();
}
}

Expand Down
59 changes: 59 additions & 0 deletions build/integration/features/bootstrap/ConversionsContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
require __DIR__ . '/../../vendor/autoload.php';

use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\TableNode;

class ConversionsContext implements Context, SnippetAcceptingContext {
use AppConfiguration;
use BasicStructure;
use WebDav;

/** @BeforeScenario */
public function setUpScenario() {
$this->asAn('admin');
$this->setStatusTestingApp(true);
}

/** @AfterScenario */
public function tearDownScenario() {
$this->asAn('admin');
$this->setStatusTestingApp(false);
}

protected function resetAppConfigs() {
}

/**
* @When /^user "([^"]*)" converts file "([^"]*)" to "([^"]*)"$/
*/
public function userConvertsTheSavedFileId(string $user, string $path, string $mime) {
$this->userConvertsTheSavedFileIdTo($user, $path, $mime, null);
}

/**
* @When /^user "([^"]*)" converts file "([^"]*)" to "([^"]*)" and saves it to "([^"]*)"$/
*/
public function userConvertsTheSavedFileIdTo(string $user, string $path, string $mime, ?string $destination) {
try {
$fileId = $this->getFileIdForPath($user, $path);
} catch (Exception $e) {
// return a fake value to keep going and be able to test the error
$fileId = 0;
}

$data = [['fileId', $fileId], ['targetMimeType', $mime]];
if ($destination !== null) {
$data[] = ['destination', $destination];
}

$this->asAn($user);
$this->sendingToWith('post', '/apps/files/api/v1/convert', new TableNode($data));
}
}
Loading

0 comments on commit 35db02c

Please sign in to comment.