14
14
use OCP \IURLGenerator ;
15
15
use OCA \Cookbook \Service \DbCacheService ;
16
16
use OCA \Cookbook \Exception \RecipeExistsException ;
17
+ use OCA \Cookbook \Helper \AcceptHeaderParsingHelper ;
17
18
use OCA \Cookbook \Helper \RestParameterParser ;
18
19
use OCP \AppFramework \Http \JSONResponse ;
20
+ use OCP \IL10N ;
19
21
20
22
class RecipeController extends Controller {
21
23
/**
@@ -37,13 +39,34 @@ class RecipeController extends Controller {
37
39
*/
38
40
private $ restParser ;
39
41
40
- public function __construct ($ AppName , IRequest $ request , IURLGenerator $ urlGenerator , RecipeService $ recipeService , DbCacheService $ dbCacheService , RestParameterParser $ restParser ) {
42
+ /**
43
+ * @var AcceptHeaderParsingHelper
44
+ */
45
+ private $ acceptHeaderParser ;
46
+
47
+ /**
48
+ * @var IL10N
49
+ */
50
+ private $ l ;
51
+
52
+ public function __construct (
53
+ $ AppName ,
54
+ IRequest $ request ,
55
+ IURLGenerator $ urlGenerator ,
56
+ RecipeService $ recipeService ,
57
+ DbCacheService $ dbCacheService ,
58
+ RestParameterParser $ restParser ,
59
+ AcceptHeaderParsingHelper $ acceptHeaderParser ,
60
+ IL10N $ l
61
+ ) {
41
62
parent ::__construct ($ AppName , $ request );
42
63
43
64
$ this ->service = $ recipeService ;
44
65
$ this ->urlGenerator = $ urlGenerator ;
45
66
$ this ->dbCacheService = $ dbCacheService ;
46
67
$ this ->restParser = $ restParser ;
68
+ $ this ->acceptHeaderParser = $ acceptHeaderParser ;
69
+ $ this ->l = $ l ;
47
70
}
48
71
49
72
/**
@@ -178,16 +201,28 @@ public function destroy($id) {
178
201
public function image ($ id ) {
179
202
$ this ->dbCacheService ->triggerCheck ();
180
203
204
+ $ acceptHeader = $ this ->request ->getHeader ('Accept ' );
205
+ $ acceptedExtensions = $ this ->acceptHeaderParser ->parseHeader ($ acceptHeader );
206
+
181
207
$ size = isset ($ _GET ['size ' ]) ? $ _GET ['size ' ] : null ;
182
208
183
209
try {
184
210
$ file = $ this ->service ->getRecipeImageFileByFolderId ($ id , $ size );
185
211
186
212
return new FileDisplayResponse ($ file , Http::STATUS_OK , ['Content-Type ' => 'image/jpeg ' , 'Cache-Control ' => 'public, max-age=604800 ' ]);
187
213
} catch (\Exception $ e ) {
188
- $ file = file_get_contents (dirname (__FILE__ ) . '/../../img/recipe.svg ' );
189
-
190
- return new DataDisplayResponse ($ file , Http::STATUS_OK , ['Content-Type ' => 'image/svg+xml ' ]);
214
+ if (array_search ('svg ' , $ acceptedExtensions , true ) === false ) {
215
+ // We may not serve a SVG image. Tell the client about the missing image.
216
+ $ json = [
217
+ 'msg ' => $ this ->l ->t ('No image with the matching mime type was found on the server. ' ),
218
+ ];
219
+ return new JSONResponse ($ json , Http::STATUS_NOT_ACCEPTABLE );
220
+ } else {
221
+ // The client accepts the SVG file. Send it.
222
+ $ file = file_get_contents (dirname (__FILE__ ) . '/../../img/recipe.svg ' );
223
+
224
+ return new DataDisplayResponse ($ file , Http::STATUS_OK , ['Content-Type ' => 'image/svg+xml ' ]);
225
+ }
191
226
}
192
227
}
193
228
}
0 commit comments