1111use OCP \AppFramework \Http \Attribute \PublicPage ;
1212use OCP \AppFramework \Http \DataResponse ;
1313use OCP \AppFramework \Http \FileDisplayResponse ;
14+ use OCP \AppFramework \Http \RedirectResponse ;
1415use OCP \AppFramework \PublicShareController ;
1516use OCP \Constants ;
1617use OCP \Files \Folder ;
1718use OCP \Files \NotFoundException ;
1819use OCP \IPreview ;
1920use OCP \IRequest ;
2021use OCP \ISession ;
22+ use OCP \Preview \IMimeIconProvider ;
2123use OCP \Share \Exceptions \ShareNotFound ;
2224use OCP \Share \IManager as ShareManager ;
2325use OCP \Share \IShare ;
@@ -33,6 +35,7 @@ public function __construct(
3335 private ShareManager $ shareManager ,
3436 ISession $ session ,
3537 private IPreview $ previewManager ,
38+ private IMimeIconProvider $ mimeIconProvider ,
3639 ) {
3740 parent ::__construct ($ appName , $ request , $ session );
3841 }
@@ -79,6 +82,7 @@ public function getPreview(
7982 int $ x = 32 ,
8083 int $ y = 32 ,
8184 $ a = false ,
85+ bool $ mimeFallback = false ,
8286 ) {
8387 $ cacheForSeconds = 60 * 60 * 24 ; // 1 day
8488
@@ -124,6 +128,12 @@ public function getPreview(
124128 $ response ->cacheFor ($ cacheForSeconds );
125129 return $ response ;
126130 } catch (NotFoundException $ e ) {
131+ // If we have no preview enabled, we can redirect to the mime icon if any
132+ if ($ mimeFallback ) {
133+ if ($ url = $ this ->mimeIconProvider ->getMimeIconUrl ($ file ->getMimeType ())) {
134+ return new RedirectResponse ($ url );
135+ }
136+ }
127137 return new DataResponse ([], Http::STATUS_NOT_FOUND );
128138 } catch (\InvalidArgumentException $ e ) {
129139 return new DataResponse ([], Http::STATUS_BAD_REQUEST );
0 commit comments