1212
1313use OCP \AppFramework \Http ;
1414use OCP \AppFramework \Http \Attribute \ApiRoute ;
15+ use OCP \AppFramework \Http \Attribute \NoAdminRequired ;
1516use OCP \AppFramework \Http \Attribute \UserRateLimit ;
1617use OCP \AppFramework \Http \DataResponse ;
18+ use OCP \AppFramework \OCS \OCSException ;
19+ use OCP \AppFramework \OCS \OCSNotFoundException ;
1720use OCP \AppFramework \OCSController ;
1821use OCP \Conversion \IConversionManager ;
1922use OCP \Files \File ;
@@ -33,25 +36,36 @@ public function __construct(
3336 parent ::__construct ($ appName , $ request );
3437 }
3538
39+ /**
40+ * Converts a file from one MIME type to another
41+ *
42+ * @param int $fileId ID of the file to be converted
43+ * @param string $targetMimeType The MIME type to which you want to convert the file
44+ * @param string|null $destination The target path of the converted file. Written to a temporary file if left empty
45+ *
46+ * @return DataResponse<Http::STATUS_CREATED, array{path: string}, array{}>
47+ *
48+ * 201: File was converted and written to the destination or temporary file
49+ *
50+ * @throws OCSException The file was unable to be converted
51+ * @throws OCSNotFoundException The file to be converted was not found
52+ */
53+ #[NoAdminRequired]
3654 #[UserRateLimit(limit: 25 , period: 120 )]
3755 #[ApiRoute(verb: 'POST ' , url: '/convert ' , root: '/conversion ' )]
3856 public function convert (int $ fileId , string $ targetMimeType , ?string $ destination = null ): DataResponse {
3957 $ userFolder = $ this ->rootFolder ->getUserFolder ($ this ->userId );
4058 $ file = $ userFolder ->getFirstNodeById ($ fileId );
4159
4260 if (!($ file instanceof File)) {
43- return new DataResponse ([
44- 'message ' => $ this ->l10n ->t ('File not found ' ),
45- ], Http::STATUS_NOT_FOUND );
61+ throw new OCSNotFoundException ();
4662 }
4763
4864 try {
4965 $ destination = $ userFolder ->getFullpath ($ destination );
5066 $ convertedFile = $ this ->conversionManager ->convert ($ file , $ targetMimeType , $ destination );
5167 } catch (\Exception $ e ) {
52- return new DataResponse ([
53- 'message ' => $ e ->getMessage (),
54- ], Http::STATUS_INTERNAL_SERVER_ERROR );
68+ throw new OCSException ($ e ->getMessage ());
5569 }
5670
5771 return new DataResponse ([
0 commit comments