XWiki PDF Handling: Encrypting Downloads While Allowing Previews

Hello everyone,
I am using XWiki(Xwiki 15.6) on Kubernetes and I have a specific requirement. I want to be able to preview PDF files via the web tab, but when downloading, I need to encrypt the files.
Work completed so far:

  • some PDF attachment exist.
  • I have written an encryption program (called Crypt-Gateway).
  • I am using Istio’s Envoy Filter as a request controller (it can access request headers and parameters to perform redirection or allow requests).

However, I am currently facing two problems:

  • I cannot distinguish between a download and preview from request headers and parameters. Therefore, I cannot decide whether to encrypt the file.
  • I noticed that when previewing the interface and directly downloading my file, the file is downloaded without any network request.(No further requests are made to my XWiki, so if someone clicks preview first and then downloads, my encryption program cannot encrypt the file.)

I would like to know:

  • Does XWiki already have similar functionality? I understand that the Attachment section in XWiki mentions forced download (force-download) and whitelist-related content, but I believe that does not meet my needs of both PDF preview and encryption.
  • Is there something wrong with my approach or logic?

I am not a native English Speaker. If there are any issues with my expression, please let me know so I can make corrections and additions at any time.
Any advice or tips will be appreciated.