2525use OCP \Files \Storage \ISharedStorage ;
2626use OCP \IPreview ;
2727use OCP \IRequest ;
28+ use OCP \IURLGenerator ;
2829use OCP \Preview \IMimeIconProvider ;
2930
3031class PreviewController extends Controller {
@@ -35,6 +36,7 @@ public function __construct(
3536 private IRootFolder $ root ,
3637 private ?string $ userId ,
3738 private IMimeIconProvider $ mimeIconProvider ,
39+ private IURLGenerator $ urlGenerator ,
3840 ) {
3941 parent ::__construct ($ appName , $ request );
4042 }
@@ -48,13 +50,11 @@ public function __construct(
4850 * @param int $x Width of the preview. A width of -1 will use the original image width.
4951 * @param int $y Height of the preview. A height of -1 will use the original image height.
5052 * @param bool $a Preserve the aspect ratio
51- * @param bool $forceIcon Force returning an icon
5253 * @param 'fill'|'cover' $mode How to crop the image
53- * @param bool $mimeFallback Whether to fallback to the mime icon if no preview is available
5454 * @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{}>
5555 *
5656 * 200: Preview returned
57- * 303: Redirect to the mime icon url if mimeFallback is true
57+ * 303: Redirect to the mime icon url
5858 * 400: Getting preview is not possible
5959 * 403: Getting preview is not allowed
6060 * 404: Preview not found
@@ -68,9 +68,8 @@ public function getPreview(
6868 int $ x = 32 ,
6969 int $ y = 32 ,
7070 bool $ a = false ,
71- bool $ forceIcon = true ,
7271 string $ mode = 'fill ' ,
73- bool $ mimeFallback = false ): Http \Response {
72+ ): Http \Response {
7473 if ($ file === '' || $ x === 0 || $ y === 0 ) {
7574 return new DataResponse ([], Http::STATUS_BAD_REQUEST );
7675 }
@@ -82,7 +81,7 @@ public function getPreview(
8281 return new DataResponse ([], Http::STATUS_NOT_FOUND );
8382 }
8483
85- return $ this ->fetchPreview ($ node , $ x , $ y , $ a , $ forceIcon , $ mode, $ mimeFallback );
84+ return $ this ->fetchPreview ($ node , $ x , $ y , $ a , $ mode );
8685 }
8786
8887 /**
@@ -92,13 +91,11 @@ public function getPreview(
9291 * @param int $x Width of the preview. A width of -1 will use the original image width.
9392 * @param int $y Height of the preview. A height of -1 will use the original image height.
9493 * @param bool $a Preserve the aspect ratio
95- * @param bool $forceIcon Force returning an icon
9694 * @param 'fill'|'cover' $mode How to crop the image
97- * @param bool $mimeFallback Whether to fallback to the mime icon if no preview is available
9895 * @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{}>
9996 *
10097 * 200: Preview returned
101- * 303: Redirect to the mime icon url if mimeFallback is true
98+ * 303: Redirect to the mime icon url
10299 * 400: Getting preview is not possible
103100 * 403: Getting preview is not allowed
104101 * 404: Preview not found
@@ -112,9 +109,8 @@ public function getPreviewByFileId(
112109 int $ x = 32 ,
113110 int $ y = 32 ,
114111 bool $ a = false ,
115- bool $ forceIcon = true ,
116112 string $ mode = 'fill ' ,
117- bool $ mimeFallback = false ) {
113+ ) {
118114 if ($ fileId === -1 || $ x === 0 || $ y === 0 ) {
119115 return new DataResponse ([], Http::STATUS_BAD_REQUEST );
120116 }
@@ -126,7 +122,7 @@ public function getPreviewByFileId(
126122 return new DataResponse ([], Http::STATUS_NOT_FOUND );
127123 }
128124
129- return $ this ->fetchPreview ($ node , $ x , $ y , $ a , $ forceIcon , $ mode, $ mimeFallback );
125+ return $ this ->fetchPreview ($ node , $ x , $ y , $ a , $ mode );
130126 }
131127
132128 /**
@@ -137,11 +133,10 @@ private function fetchPreview(
137133 int $ x ,
138134 int $ y ,
139135 bool $ a ,
140- bool $ forceIcon ,
141136 string $ mode ,
142- bool $ mimeFallback = false ) : Http \Response {
143- if (!($ node instanceof File) || (! $ forceIcon && ! $ this -> preview -> isAvailable ( $ node )) ) {
144- return new DataResponse ([], Http:: STATUS_NOT_FOUND );
137+ ) : Http \Response {
138+ if (!($ node instanceof File)) {
139+ return new RedirectResponse ( $ this -> urlGenerator -> getAbsoluteURL ( $ this -> urlGenerator -> imagePath ( ' core ' , ' filetypes/folder.svg ' )) );
145140 }
146141 if (!$ node ->isReadable ()) {
147142 return new DataResponse ([], Http::STATUS_FORBIDDEN );
@@ -175,13 +170,11 @@ private function fetchPreview(
175170 return $ response ;
176171 } catch (NotFoundException $ e ) {
177172 // If we have no preview enabled, we can redirect to the mime icon if any
178- if ($ mimeFallback ) {
179- if ($ url = $ this ->mimeIconProvider ->getMimeIconUrl ($ node ->getMimeType ())) {
180- return new RedirectResponse ($ url );
181- }
173+ if ($ url = $ this ->mimeIconProvider ->getMimeIconUrl ($ node ->getMimeType ())) {
174+ return new RedirectResponse ($ url );
182175 }
183176
184- return new DataResponse ([], Http:: STATUS_NOT_FOUND );
177+ return new RedirectResponse ( $ this -> urlGenerator -> getAbsoluteURL ( $ this -> urlGenerator -> imagePath ( ' core ' , ' filetypes/file.svg ' )) );
185178 } catch (\InvalidArgumentException $ e ) {
186179 return new DataResponse ([], Http::STATUS_BAD_REQUEST );
187180 }
0 commit comments