3232use OCP \AppFramework \Http ;
3333use OCP \AppFramework \Http \DataResponse ;
3434use OCP \AppFramework \Http \FileDisplayResponse ;
35+ use OCP \AppFramework \Http \RedirectResponse ;
3536use OCP \Files \File ;
3637use OCP \Files \IRootFolder ;
3738use OCP \Files \Node ;
3839use OCP \Files \NotFoundException ;
3940use OCP \IPreview ;
4041use OCP \IRequest ;
42+ use OCP \Preview \IMimeIconProvider ;
4143
4244class PreviewController extends Controller {
4345 public function __construct (
@@ -46,6 +48,7 @@ public function __construct(
4648 private IPreview $ preview ,
4749 private IRootFolder $ root ,
4850 private ?string $ userId ,
51+ private IMimeIconProvider $ mimeIconProvider ,
4952 ) {
5053 parent ::__construct ($ appName , $ request );
5154 }
@@ -62,6 +65,7 @@ public function __construct(
6265 * @param bool $a Whether to not crop the preview
6366 * @param bool $forceIcon Force returning an icon
6467 * @param string $mode How to crop the image
68+ * @param bool $mimeFallback Whether to fallback to the mime icon if no preview is available
6569 * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>
6670 *
6771 * 200: Preview returned
@@ -75,7 +79,8 @@ public function getPreview(
7579 int $ y = 32 ,
7680 bool $ a = false ,
7781 bool $ forceIcon = true ,
78- string $ mode = 'fill ' ): Http \Response {
82+ string $ mode = 'fill ' ,
83+ bool $ mimeFallback ): Http \Response {
7984 if ($ file === '' || $ x === 0 || $ y === 0 ) {
8085 return new DataResponse ([], Http::STATUS_BAD_REQUEST );
8186 }
@@ -87,7 +92,7 @@ public function getPreview(
8792 return new DataResponse ([], Http::STATUS_NOT_FOUND );
8893 }
8994
90- return $ this ->fetchPreview ($ node , $ x , $ y , $ a , $ forceIcon , $ mode );
95+ return $ this ->fetchPreview ($ node , $ x , $ y , $ a , $ forceIcon , $ mode, $ mimeFallback );
9196 }
9297
9398 /**
@@ -102,6 +107,7 @@ public function getPreview(
102107 * @param bool $a Whether to not crop the preview
103108 * @param bool $forceIcon Force returning an icon
104109 * @param string $mode How to crop the image
110+ * @param bool $mimeFallback Whether to fallback to the mime icon if no preview is available
105111 * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>
106112 *
107113 * 200: Preview returned
@@ -115,7 +121,8 @@ public function getPreviewByFileId(
115121 int $ y = 32 ,
116122 bool $ a = false ,
117123 bool $ forceIcon = true ,
118- string $ mode = 'fill ' ) {
124+ string $ mode = 'fill ' ,
125+ bool $ mimeFallback = false ) {
119126 if ($ fileId === -1 || $ x === 0 || $ y === 0 ) {
120127 return new DataResponse ([], Http::STATUS_BAD_REQUEST );
121128 }
@@ -129,19 +136,20 @@ public function getPreviewByFileId(
129136
130137 $ node = array_pop ($ nodes );
131138
132- return $ this ->fetchPreview ($ node , $ x , $ y , $ a , $ forceIcon , $ mode );
139+ return $ this ->fetchPreview ($ node , $ x , $ y , $ a , $ forceIcon , $ mode, $ mimeFallback );
133140 }
134141
135142 /**
136- * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>
143+ * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>|RedirectResponse<string>
137144 */
138145 private function fetchPreview (
139146 Node $ node ,
140147 int $ x ,
141148 int $ y ,
142149 bool $ a ,
143150 bool $ forceIcon ,
144- string $ mode ) : Http \Response {
151+ string $ mode ,
152+ bool $ mimeFallback = false ) : Http \Response {
145153 if (!($ node instanceof File) || (!$ forceIcon && !$ this ->preview ->isAvailable ($ node ))) {
146154 return new DataResponse ([], Http::STATUS_NOT_FOUND );
147155 }
@@ -167,6 +175,13 @@ private function fetchPreview(
167175 $ response ->cacheFor (3600 * 24 , false , true );
168176 return $ response ;
169177 } catch (NotFoundException $ e ) {
178+ // If we have no preview enabled, we can redirect to the mime icon if any
179+ if ($ mimeFallback ) {
180+ if ($ url = $ this ->mimeIconProvider ->getMimeIconUrl ($ node ->getMimeType ())) {
181+ return new RedirectResponse ($ url );
182+ }
183+ }
184+
170185 return new DataResponse ([], Http::STATUS_NOT_FOUND );
171186 } catch (\InvalidArgumentException $ e ) {
172187 return new DataResponse ([], Http::STATUS_BAD_REQUEST );
0 commit comments