aboutsummaryrefslogtreecommitdiffstats
path: root/lib/public/Files/Conversion
diff options
context:
space:
mode:
Diffstat (limited to 'lib/public/Files/Conversion')
-rw-r--r--lib/public/Files/Conversion/ConversionMimeProvider.php66
-rw-r--r--lib/public/Files/Conversion/IConversionManager.php46
-rw-r--r--lib/public/Files/Conversion/IConversionProvider.php41
3 files changed, 153 insertions, 0 deletions
diff --git a/lib/public/Files/Conversion/ConversionMimeProvider.php b/lib/public/Files/Conversion/ConversionMimeProvider.php
new file mode 100644
index 00000000000..0daf4a10648
--- /dev/null
+++ b/lib/public/Files/Conversion/ConversionMimeProvider.php
@@ -0,0 +1,66 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCP\Files\Conversion;
+
+use JsonSerializable;
+
+/**
+ * A tuple-like object representing both an original and target
+ * MIME type for a file conversion
+ *
+ * @since 31.0.0
+ */
+class ConversionMimeProvider implements JsonSerializable {
+ /**
+ * @param string $from The source MIME type of a file
+ * @param string $to The target MIME type for the file
+ * @param string $extension The file extension for the target MIME type (e.g. 'png')
+ * @param string $displayName The human-readable name of the target MIME type (e.g. 'Image (.png)')
+ *
+ * @since 31.0.0
+ */
+ public function __construct(
+ private string $from,
+ private string $to,
+ private string $extension,
+ private string $displayName,
+ ) {
+ }
+
+ public function getFrom(): string {
+ return $this->from;
+ }
+
+ public function getTo(): string {
+ return $this->to;
+ }
+
+ public function getExtension(): string {
+ return $this->extension;
+ }
+
+ public function getDisplayName(): string {
+ return $this->displayName;
+ }
+
+ /**
+ * @return array{from: string, to: string, extension: string, displayName: string}
+ *
+ * @since 31.0.0
+ */
+ public function jsonSerialize(): array {
+ return [
+ 'from' => $this->from,
+ 'to' => $this->to,
+ 'extension' => $this->extension,
+ 'displayName' => $this->displayName,
+ ];
+ }
+}
diff --git a/lib/public/Files/Conversion/IConversionManager.php b/lib/public/Files/Conversion/IConversionManager.php
new file mode 100644
index 00000000000..ed418129d3b
--- /dev/null
+++ b/lib/public/Files/Conversion/IConversionManager.php
@@ -0,0 +1,46 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCP\Files\Conversion;
+
+use OCP\Files\File;
+
+/**
+ * @since 31.0.0
+ */
+interface IConversionManager {
+ /**
+ * Determines whether or not conversion providers are available
+ *
+ * @since 31.0.0
+ */
+ public function hasProviders(): bool;
+
+ /**
+ * Gets all supported MIME type conversions
+ *
+ * @return list<ConversionMimeProvider>
+ *
+ * @since 31.0.0
+ */
+ public function getProviders(): array;
+
+ /**
+ * Convert a file to a given MIME type
+ *
+ * @param File $file The file to be converted
+ * @param string $targetMimeType The MIME type to convert the file to
+ * @param ?string $destination The destination to save the converted file
+ *
+ * @return string Path to the converted file
+ *
+ * @since 31.0.0
+ */
+ public function convert(File $file, string $targetMimeType, ?string $destination = null): string;
+}
diff --git a/lib/public/Files/Conversion/IConversionProvider.php b/lib/public/Files/Conversion/IConversionProvider.php
new file mode 100644
index 00000000000..3b5c5945c99
--- /dev/null
+++ b/lib/public/Files/Conversion/IConversionProvider.php
@@ -0,0 +1,41 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCP\Files\Conversion;
+
+use OCP\Files\File;
+
+/**
+ * This interface is implemented by apps that provide
+ * a file conversion provider
+ *
+ * @since 31.0.0
+ */
+interface IConversionProvider {
+ /**
+ * Get an array of MIME type tuples this conversion provider supports
+ *
+ * @return list<ConversionMimeProvider>
+ *
+ * @since 31.0.0
+ */
+ public function getSupportedMimeTypes(): array;
+
+ /**
+ * Convert a file to a given MIME type
+ *
+ * @param File $file The file to be converted
+ * @param string $targetMimeType The MIME type to convert the file to
+ *
+ * @return resource|string Resource or string content of the file
+ *
+ * @since 31.0.0
+ */
+ public function convertFile(File $file, string $targetMimeType): mixed;
+}