88
99use OCA \Theming \AppInfo \Application ;
1010use OCA \Theming \Service \BackgroundService ;
11+ use OCP \AppFramework \Services \IAppConfig ;
1112use OCP \Files \IAppData ;
1213use OCP \Files \NotFoundException ;
1314use OCP \Files \NotPermittedException ;
@@ -30,6 +31,7 @@ public function __construct(
3031 private LoggerInterface $ logger ,
3132 private ITempManager $ tempManager ,
3233 private BackgroundService $ backgroundService ,
34+ private IAppConfig $ appConfig ,
3335 ) {
3436 }
3537
@@ -40,7 +42,7 @@ public function __construct(
4042 * @return string the image url
4143 */
4244 public function getImageUrl (string $ key ): string {
43- $ cacheBusterCounter = $ this ->config -> getAppValue (Application:: APP_ID , ' cachebuster ' , ' 0 ' );
45+ $ cacheBusterCounter = ( string ) $ this ->appConfig -> getAppValueInt (ConfigLexicon:: CACHE_BUSTER );
4446 if ($ this ->hasImage ($ key )) {
4547 return $ this ->urlGenerator ->linkToRoute ('theming.Theming.getImage ' , [ 'key ' => $ key ]) . '?v= ' . $ cacheBusterCounter ;
4648 } elseif ($ key === 'backgroundDark ' && $ this ->hasImage ('background ' )) {
@@ -85,31 +87,14 @@ public function getImageUrlAbsolute(string $key): string {
8587 public function getImage (string $ key , bool $ useSvg = true ): ISimpleFile {
8688 $ mime = $ this ->config ->getAppValue ('theming ' , $ key . 'Mime ' , '' );
8789 $ folder = $ this ->getRootFolder ()->getFolder ('images ' );
88- $ useSvg = $ useSvg && $ this ->canConvert ('SVG ' );
8990
9091 if ($ mime === '' || !$ folder ->fileExists ($ key )) {
9192 throw new NotFoundException ();
9293 }
93- // if SVG was requested and is supported
94- if ($ useSvg ) {
95- if (!$ folder ->fileExists ($ key . '.svg ' )) {
96- try {
97- $ finalIconFile = new \Imagick ();
98- $ finalIconFile ->setBackgroundColor ('none ' );
99- $ finalIconFile ->readImageBlob ($ folder ->getFile ($ key )->getContent ());
100- $ finalIconFile ->setImageFormat ('SVG ' );
101- $ svgFile = $ folder ->newFile ($ key . '.svg ' );
102- $ svgFile ->putContent ($ finalIconFile ->getImageBlob ());
103- return $ svgFile ;
104- } catch (\ImagickException $ e ) {
105- $ this ->logger ->info ('The image was requested to be no SVG file, but converting it to SVG failed: ' . $ e ->getMessage ());
106- }
107- } else {
108- return $ folder ->getFile ($ key . '.svg ' );
109- }
110- }
111- // if SVG was not requested, but PNG is supported
112- if (!$ useSvg && $ this ->canConvert ('PNG ' )) {
94+ // only convert SVG originals to PNG when SVG output is not desired;
95+ // converting raster images to SVG produces broken output and is not useful
96+ $ isOriginalSvg = ($ mime === 'image/svg+xml ' || $ mime === 'image/svg ' );
97+ if ($ isOriginalSvg && !$ useSvg && $ this ->canConvert ('SVG ' ) && $ this ->canConvert ('PNG ' )) {
11398 if (!$ folder ->fileExists ($ key . '.png ' )) {
11499 try {
115100 $ finalIconFile = new \Imagick ();
@@ -120,13 +105,12 @@ public function getImage(string $key, bool $useSvg = true): ISimpleFile {
120105 $ pngFile ->putContent ($ finalIconFile ->getImageBlob ());
121106 return $ pngFile ;
122107 } catch (\ImagickException $ e ) {
123- $ this ->logger ->info ('The image was requested to be no SVG file, but converting it to PNG failed: ' . $ e ->getMessage ());
108+ $ this ->logger ->info ('Converting SVG to PNG failed: ' . $ e ->getMessage ());
124109 }
125110 } else {
126111 return $ folder ->getFile ($ key . '.png ' );
127112 }
128113 }
129- // fallback to the original file
130114 return $ folder ->getFile ($ key );
131115 }
132116
@@ -157,7 +141,7 @@ public function getCustomImages(): array {
157141 * @throws NotPermittedException
158142 */
159143 public function getCacheFolder (): ISimpleFolder {
160- $ cacheBusterValue = $ this ->config -> getAppValue ( ' theming ' , ' cachebuster ' , ' 0 ' );
144+ $ cacheBusterValue = ( string ) $ this ->appConfig -> getAppValueInt (ConfigLexicon:: CACHE_BUSTER );
161145 try {
162146 $ folder = $ this ->getRootFolder ()->getFolder ($ cacheBusterValue );
163147 } catch (NotFoundException $ e ) {
@@ -214,6 +198,12 @@ public function delete(string $key): void {
214198 } catch (NotFoundException $ e ) {
215199 } catch (NotPermittedException $ e ) {
216200 }
201+ try {
202+ $ file = $ this ->getRootFolder ()->getFolder ('images ' )->getFile ($ key . '.svg ' );
203+ $ file ->delete ();
204+ } catch (NotFoundException $ e ) {
205+ } catch (NotPermittedException $ e ) {
206+ }
217207
218208 if ($ key === 'logo ' ) {
219209 $ this ->config ->deleteAppValue ('theming ' , 'logoDimensions ' );
0 commit comments