2525use OCP \AppFramework \Http \Attribute \PublicPage ;
2626use OCP \AppFramework \Http \DataResponse ;
2727use OCP \AppFramework \Http \FileDisplayResponse ;
28+ use OCP \Files \SimpleFS \InMemoryFile ;
2829use OCP \IL10N ;
2930use OCP \IPreview ;
3031use OCP \IRequest ;
32+ use OCP \IURLGenerator ;
3133use OCP \IUser ;
3234use 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 ]);
0 commit comments