1313use OCP \AppFramework \Http \Attribute \OpenAPI ;
1414use OCP \AppFramework \Http \DataResponse ;
1515use OCP \AppFramework \Http \FileDisplayResponse ;
16+ use OCP \AppFramework \Http \RedirectResponse ;
1617use OCP \Files \IRootFolder ;
1718use OCP \Files \NotFoundException ;
1819use OCP \IPreview ;
1920use OCP \IRequest ;
2021use OCP \IUserSession ;
22+ use OCP \Preview \IMimeIconProvider ;
2123
2224#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT )]
2325class PreviewController extends Controller {
@@ -29,6 +31,7 @@ public function __construct(
2931 private IUserSession $ userSession ,
3032 private IVersionManager $ versionManager ,
3133 private IPreview $ previewManager ,
34+ private IMimeIconProvider $ mimeIconProvider ,
3235 ) {
3336 parent ::__construct ($ appName , $ request );
3437 }
@@ -40,9 +43,11 @@ public function __construct(
4043 * @param int $x Width of the preview
4144 * @param int $y Height of the preview
4245 * @param string $version Version of the file to get the preview for
43- * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, list<empty>, array{}>
46+ * @param bool $mimeFallback Whether to fallback to the mime icon if no preview is available
47+ * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, list<empty>, array{}>|RedirectResponse<Http::STATUS_SEE_OTHER, array{}>
4448 *
4549 * 200: Preview returned
50+ * 303: Redirect to the mime icon url if mimeFallback is true
4651 * 400: Getting preview is not possible
4752 * 404: Preview not found
4853 */
@@ -53,19 +58,31 @@ public function getPreview(
5358 int $ x = 44 ,
5459 int $ y = 44 ,
5560 string $ version = '' ,
61+ bool $ mimeFallback = false ,
5662 ) {
5763 if ($ file === '' || $ version === '' || $ x === 0 || $ y === 0 ) {
5864 return new DataResponse ([], Http::STATUS_BAD_REQUEST );
5965 }
6066
67+ $ versionFile = null ;
6168 try {
6269 $ user = $ this ->userSession ->getUser ();
6370 $ userFolder = $ this ->rootFolder ->getUserFolder ($ user ->getUID ());
6471 $ file = $ userFolder ->get ($ file );
6572 $ versionFile = $ this ->versionManager ->getVersionFile ($ user , $ file , $ version );
6673 $ preview = $ this ->previewManager ->getPreview ($ versionFile , $ x , $ y , true , IPreview::MODE_FILL , $ versionFile ->getMimetype ());
67- return new FileDisplayResponse ($ preview , Http::STATUS_OK , ['Content-Type ' => $ preview ->getMimeType ()]);
74+ $ response = new FileDisplayResponse ($ preview , Http::STATUS_OK , ['Content-Type ' => $ preview ->getMimeType ()]);
75+ $ response ->cacheFor (3600 * 24 , false , true );
76+ return $ response ;
6877 } catch (NotFoundException $ e ) {
78+ // If we have no preview enabled, we can redirect to the mime icon if any
79+ if ($ mimeFallback && $ versionFile !== null ) {
80+ $ url = $ this ->mimeIconProvider ->getMimeIconUrl ($ versionFile ->getMimeType ());
81+ if ($ url !== null ) {
82+ return new RedirectResponse ($ url );
83+ }
84+ }
85+
6986 return new DataResponse ([], Http::STATUS_NOT_FOUND );
7087 } catch (\InvalidArgumentException $ e ) {
7188 return new DataResponse ([], Http::STATUS_BAD_REQUEST );
0 commit comments