Skip to content

Commit 9c4b94d

Browse files
Merge pull request #4811 from LibreSign/fix/check-if-preview-is-available
fix: check if preview is available
2 parents d918db1 + ee3b605 commit 9c4b94d

3 files changed

Lines changed: 30 additions & 8 deletions

File tree

lib/Controller/AccountController.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
use OCP\AppFramework\Http\Attribute\UseSession;
3434
use OCP\AppFramework\Http\DataResponse;
3535
use OCP\IL10N;
36-
use OCP\IPreview;
3736
use OCP\IRequest;
3837
use OCP\IURLGenerator;
3938
use OCP\IUserSession;
@@ -62,7 +61,6 @@ public function __construct(
6261
private LoggerInterface $logger,
6362
protected IUserSession $userSession,
6463
protected SessionService $sessionService,
65-
private IPreview $preview,
6664
private ValidateHelper $validateHelper,
6765
) {
6866
parent::__construct(Application::APP_ID, $request);

lib/Controller/SignatureElementsController.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
use OCP\AppFramework\Http\Attribute\PublicPage;
2626
use OCP\AppFramework\Http\DataResponse;
2727
use OCP\AppFramework\Http\FileDisplayResponse;
28+
use OCP\Files\SimpleFS\InMemoryFile;
2829
use OCP\IL10N;
2930
use OCP\IPreview;
3031
use OCP\IRequest;
32+
use OCP\IURLGenerator;
3133
use OCP\IUser;
3234
use OCP\IUserSession;
35+
use OCP\Preview\IMimeIconProvider;
3336

3437
/**
3538
* @psalm-import-type LibresignUserElement from ResponseDefinitions
@@ -46,6 +49,8 @@ public function __construct(
4649
protected SessionService $sessionService,
4750
protected SignFileService $signFileService,
4851
private IPreview $preview,
52+
protected IMimeIconProvider $mimeIconProvider,
53+
protected IURLGenerator $urlGenerator,
4954
private ValidateHelper $validateHelper,
5055
) {
5156
parent::__construct(Application::APP_ID, $request);
@@ -157,14 +162,30 @@ public function getSignatureElementPreview(int $nodeId) {
157162
$nodeId,
158163
$this->sessionService->getSessionId()
159164
);
165+
if ($this->preview->isAvailable($node)) {
166+
$preview = $this->preview->getPreview(
167+
file: $node,
168+
width: SignerElementsService::ELEMENT_SIGN_WIDTH,
169+
height: SignerElementsService::ELEMENT_SIGN_HEIGHT,
170+
);
171+
} else {
172+
// When the preview is disabled, use the icon image of mimetype
173+
// as fallback
174+
$url = $this->mimeIconProvider->getMimeIconUrl($node->getMimeType());
175+
$baseUrl = $this->urlGenerator->getBaseUrl();
176+
if (!str_starts_with($url, $baseUrl)) {
177+
throw new DoesNotExistException('Preview disabled');
178+
}
179+
$path = \OC::$SERVERROOT . str_replace($baseUrl, '', $url);
180+
if (!file_exists($path)) {
181+
throw new DoesNotExistException('Preview disabled');
182+
}
183+
$extension = pathinfo($path, PATHINFO_EXTENSION);
184+
$preview = new InMemoryFile(implode('.', ['signature', $extension]), file_get_contents($path));
185+
}
160186
} catch (DoesNotExistException $th) {
161187
return new DataResponse([], Http::STATUS_NOT_FOUND);
162188
}
163-
$preview = $this->preview->getPreview(
164-
file: $node,
165-
width: SignerElementsService::ELEMENT_SIGN_WIDTH,
166-
height: SignerElementsService::ELEMENT_SIGN_HEIGHT,
167-
);
168189
$response = new FileDisplayResponse($preview, Http::STATUS_OK, [
169190
'Content-Type' => $preview->getMimeType(),
170191
]);

src/Components/PreviewSignature/PreviewSignature.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default {
6767
await axios(config)
6868
.then(response => {
6969
const buffer = Buffer.from(response.data, 'binary').toString('base64')
70-
this.imageData = 'data:application/pdf;base64,' + buffer
70+
this.imageData = 'data:' + response.headers['content-type'] + ';base64,' + buffer
7171
this.onImageLoad(true)
7272
})
7373
.catch(() => this.onImageLoad(false))
@@ -89,5 +89,8 @@ export default {
8989
margin-bottom: 10px;
9090
min-width: 350px;
9191
min-height: 95px;
92+
display: flex;
93+
align-items: center;
94+
justify-content: center;
9295
}
9396
</style>

0 commit comments

Comments
 (0)