Skip to content

Commit

Permalink
Merge pull request #2 from LibreCodeCoop/feat/make-possible-use-multi…
Browse files Browse the repository at this point in the history
…ple-image-types

feat: make possible use multiple types
  • Loading branch information
vitormattos authored Oct 3, 2024
2 parents 2c97721 + 78f8666 commit b793584
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
17 changes: 4 additions & 13 deletions lib/Middleware/InjectionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Middleware;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IRequest;
Expand Down Expand Up @@ -61,25 +63,14 @@ private function getImageFromDomain(Response $response, string $type): Response
return $response;
}

if ($type === 'logo') {
$file = $this->companyService->getThemeFile('core/img/logo.png');
$mime = 'image/png';
} elseif ($type === 'favicon') {
$file = $this->companyService->getThemeFile('core/img/favicon.png');
$mime = 'image/png';
} elseif ($type === 'background') {
$file = $this->companyService->getThemeFile('core/img/background.png');
$mime = 'image/png';
} else {
return new NotFoundResponse();
}
$file = $this->companyService->getThemeFile('core/img/' . $type);

if ($response instanceof NotFoundResponse) {
$response = new FileDisplayResponse($file);
$csp = new ContentSecurityPolicy();
$csp->allowInlineStyle();
$response->cacheFor(3600);
$response->addHeader('Content-Type', $mime);
$response->addHeader('Content-Type', $file->getMimeType());

Check failure on line 73 in lib/Middleware/InjectionMiddleware.php

View workflow job for this annotation

GitHub Actions / Nextcloud

InvalidTemplateParam

lib/Middleware/InjectionMiddleware.php:73:4: InvalidTemplateParam: Extended template param S of OCP\AppFramework\Http\FileDisplayResponse<mixed, array<never, never>>&static expects type int, type mixed given (see https://psalm.dev/183)
$response->addHeader('Content-Disposition', 'attachment; filename="' . $type . '"');
$response->setContentSecurityPolicy($csp);
} else {
Expand Down
14 changes: 8 additions & 6 deletions lib/Service/CompanyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,15 @@ public function getCompanyCode(): string {

public function getThemeFile($name): ISimpleFile {
$folder = $this->getThemeFolder($this->getCompanyCode());
try {
$file = $folder->getFile($name);
} catch (NotFoundException $e) {
$folder = $this->getThemeFolder('default');
$file = $folder->getFile($name);
foreach (['png', 'jpg', 'svg'] as $extension) {
try {
return $folder->getFile($name . '.' . $extension);
} catch (NotFoundException $e) {
continue;
}
}
return $file;
$folder = $this->getThemeFolder('default');
return $folder->getFile($name);
}

private function getThemeFolder(string $folderName): ISimpleFolder {
Expand Down

0 comments on commit b793584

Please sign in to comment.