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 }
@@ -63,9 +66,11 @@ protected function isPasswordProtected(): bool {
6366 * @param int $x Width of the preview
6467 * @param int $y Height of the preview
6568 * @param bool $a Whether to not crop the preview
66- * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, list<empty>, array{}>
69+ * @param bool $mimeFallback Whether to fallback to the mime icon if no preview is available
70+ * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, list<empty>, array{}>|RedirectResponse<Http::STATUS_SEE_OTHER, array{}>
6771 *
6872 * 200: Preview returned
73+ * 303: Redirect to the mime icon url if mimeFallback is true
6974 * 400: Getting preview is not possible
7075 * 403: Getting preview is not allowed
7176 * 404: Share or preview not found
@@ -79,6 +84,7 @@ public function getPreview(
7984 int $ x = 32 ,
8085 int $ y = 32 ,
8186 $ a = false ,
87+ bool $ mimeFallback = false ,
8288 ) {
8389 $ cacheForSeconds = 60 * 60 * 24 ; // 1 day
8490
@@ -124,6 +130,12 @@ public function getPreview(
124130 $ response ->cacheFor ($ cacheForSeconds );
125131 return $ response ;
126132 } catch (NotFoundException $ e ) {
133+ // If we have no preview enabled, we can redirect to the mime icon if any
134+ if ($ mimeFallback ) {
135+ if ($ url = $ this ->mimeIconProvider ->getMimeIconUrl ($ file ->getMimeType ())) {
136+ return new RedirectResponse ($ url );
137+ }
138+ }
127139 return new DataResponse ([], Http::STATUS_NOT_FOUND );
128140 } catch (\InvalidArgumentException $ e ) {
129141 return new DataResponse ([], Http::STATUS_BAD_REQUEST );
0 commit comments