1111
1212use OC \AppFramework \Bootstrap \Coordinator ;
1313use OC \SystemConfig ;
14- use OCP \Files \Conversion \ConversionMimeTuple ;
14+ use OCP \Files \Conversion \ConversionMimeProvider ;
1515use OCP \Files \Conversion \IConversionManager ;
1616use OCP \Files \Conversion \IConversionProvider ;
1717use OCP \Files \File ;
18+ use OCP \Files \Folder ;
1819use OCP \Files \GenericFileException ;
1920use OCP \Files \IRootFolder ;
2021use OCP \ITempManager ;
@@ -53,16 +54,28 @@ public function hasProviders(): bool {
5354 return !empty ($ context ->getFileConversionProviders ());
5455 }
5556
56- public function getMimeTypes (): array {
57- $ mimeTypes = [];
58-
59- foreach ($ this ->getProviders () as $ provider ) {
60- $ mimeTypes [] = $ provider ->getSupportedMimetypes ();
57+ public function getProviders (): array {
58+ $ providers = [];
59+ foreach ($ this ->getRegisteredProviders () as $ provider ) {
60+ $ providers = array_merge ($ providers , $ provider ->getSupportedMimeTypes ());
6161 }
62+ return $ providers ;
63+ }
64+
65+ /**
66+ * @param string $mime
67+ * @return list<ConversionMimeProvider>
68+ */
69+ private function getProvidersForMime (string $ mime ): array {
70+ $ mimeTypes = $ this ->getProviders ();
71+ $ filtered = array_filter (
72+ $ mimeTypes ,
73+ function (ConversionMimeProvider $ mimeProvider ) use ($ mime ) {
74+ return $ mimeProvider ->getFrom () === $ mime ;
75+ }
76+ );
6277
63- /** @var list<ConversionMimeTuple> */
64- $ mimeTypes = array_merge (...$ mimeTypes );
65- return $ mimeTypes ;
78+ return array_values ($ filtered );
6679 }
6780
6881 public function convert (File $ file , string $ targetMimeType , ?string $ destination = null ): string {
@@ -80,24 +93,36 @@ public function convert(File $file, string $targetMimeType, ?string $destination
8093 $ fileMimeType = $ file ->getMimetype ();
8194 $ validProvider = $ this ->getValidProvider ($ fileMimeType , $ targetMimeType );
8295
96+ $ targetExtension = '' ;
97+ foreach ($ this ->getProvidersForMime ($ fileMimeType ) as $ mimeProvider ) {
98+ if ($ mimeProvider ->getTo () === $ targetMimeType ) {
99+ $ targetExtension = $ mimeProvider ->getExtension ();
100+ break ;
101+ }
102+ }
103+
83104 if ($ validProvider !== null ) {
84105 $ convertedFile = $ validProvider ->convertFile ($ file , $ targetMimeType );
85106
86- if ($ destination !== null ) {
87- $ convertedFile = $ this ->writeToDestination ($ destination , $ convertedFile );
88- return $ convertedFile ->getPath ();
107+ // If destination not provided, we use the same path
108+ // as the original file, but with the new extension
109+ if ($ destination === null ) {
110+ $ basename = pathinfo ($ file ->getPath (), PATHINFO_FILENAME );
111+ $ parent = $ file ->getParent ();
112+ $ destination = $ parent ->getFullPath ($ basename . '. ' . $ targetExtension );
89113 }
90114
91- $ tmp = $ this ->tempManager ->getTemporaryFile ();
92- file_put_contents ($ tmp , $ convertedFile );
93-
94- return $ tmp ;
115+ $ convertedFile = $ this ->writeToDestination ($ destination , $ convertedFile );
116+ return $ convertedFile ->getPath ();
95117 }
96118
97119 throw new RuntimeException ('Could not convert file ' );
98120 }
99121
100- public function getProviders (): array {
122+ /**
123+ * @return IConversionProvider[]
124+ */
125+ private function getRegisteredProviders (): array {
101126 if (count ($ this ->providers ) > 0 ) {
102127 return $ this ->providers ;
103128 }
@@ -125,28 +150,29 @@ public function getProviders(): array {
125150 }
126151
127152 private function writeToDestination (string $ destination , mixed $ content ): File {
153+ if ($ this ->rootFolder ->nodeExists ($ destination )) {
154+ $ file = $ this ->rootFolder ->get ($ destination );
155+ $ parent = $ file ->getParent ();
156+ if (!$ parent ->isCreatable ()) {
157+ throw new GenericFileException ('Destination is not creatable ' );
158+ }
159+
160+ $ newName = $ parent ->getNonExistingName (basename ($ destination ));
161+ $ destination = $ parent ->getFullPath ($ newName );
162+ }
163+
128164 return $ this ->rootFolder ->newFile ($ destination , $ content );
129165 }
130166
131167 private function getValidProvider (string $ fileMimeType , string $ targetMimeType ): ?IConversionProvider {
132- $ validProvider = null ;
133- foreach ($ this ->getProviders () as $ provider ) {
134- $ suitableMimeTypes = array_filter (
135- $ provider ->getSupportedMimeTypes (),
136- function (ConversionMimeTuple $ mimeTuple ) use ($ fileMimeType , $ targetMimeType ) {
137- ['from ' => $ from , 'to ' => $ to ] = $ mimeTuple ->jsonSerialize ();
138-
139- $ supportsTargetMimeType = in_array ($ targetMimeType , array_column ($ to , 'mime ' ));
140- return ($ from === $ fileMimeType ) && $ supportsTargetMimeType ;
168+ foreach ($ this ->getRegisteredProviders () as $ provider ) {
169+ foreach ($ provider ->getSupportedMimeTypes () as $ mimeProvider ) {
170+ if ($ mimeProvider ->getFrom () === $ fileMimeType && $ mimeProvider ->getTo () === $ targetMimeType ) {
171+ return $ provider ;
141172 }
142- );
143-
144- if (!empty ($ suitableMimeTypes )) {
145- $ validProvider = $ provider ;
146- break ;
147173 }
148174 }
149-
150- return $ validProvider ;
175+
176+ return null ;
151177 }
152178}
0 commit comments