Открывать PDF-файлы напрямую (не через загрузку) по умолчанию

В настоящее время основная обработка PDF-файлов осуществляется через загрузку:

allaboutcats.pdf (42.2 КБ)

Хотя это функционально, для наиболее частого сценария использования — «Я хочу прочитать это прямо сейчас» — особенно на мобильных устройствах, это добавляет несколько лишних шагов.

Необходимость загрузки или сохранения чего-либо является второстепенной. Это требуется иногда, но обычно пользователь просто хочет прочитать файл и продолжить работу.

Тема Inline PDF Previews решает эту проблему, предоставляя встроенный просмотр PDF на настольных компьютерах, и делает это блестяще. Однако она не охватывает следующие распространённые ситуации:

  1. PDF-файлы на мобильных устройствах
  2. Несколько PDF-файлов (из-за визуального шума)
  3. Просмотр PDF-файлов в отдельной вкладке браузера

Стоит отметить, что существуют и другие способы загрузки PDF-файлов. Их можно скачать по прямой ссылке (через правую кнопку мыши / контекстное меню) или из встроенного в браузер просмотрщика PDF, который предоставляет эта тема.

Предложение: по умолчанию открывать PDF-файлы в браузере

Это небольшое изменение изящно решает обе ситуации и просто использует стандартное поведение для других ссылок в Discourse. Таким образом, с предлагаемым изменением:

  1. Ссылки на PDF на мобильных устройствах обычно открываются в том же окне браузера
    — позволяет просматривать одним касанием
  2. Ссылки на PDF на настольных компьютерах (т. е. выше встроенного PDF) открываются в новой вкладке
    — это позволяет просматривать в полной вкладке одним кликом
  3. При намеренном отключении встроенного поведения (путём добавления пробела в тексте ссылки)
    — позволяет просматривать одним кликом

Я объединил всё это в этот PR:

4 лайка

Can anyone of your team maybe have a look at this PR @david ?

I left some comments about the code itself on the PR

When I click this link, it opens the PDF in the browser. No download. Are you seeing something different?

I’m using Chrome on macOS. We’re not running this d-pdf-previews theme component on Meta.

2 лайка

Here in meta.discourse.org, my link above opens in the same tab as this page (just like you describe). This is unchanged in safe-mode.

However, on three of my recently updated sites (with the TC disabled) it attempts a download. This is also the case with safe-mode. I’m not sure why that is. I can’t see any site settings that govern this at all.

Whatever the reason, my fork fixes it for my sites nicely!

This makes no sense to me!!

1 лайк

I’m going to see if we can get this inconsistency fixed up in core. Will update here with progress.

2 лайка

Any joy with this thusfar?

1 лайк

We do have a work-in-progress. Hopefully will be able to land it in the next couple of weeks. It’s a little tricky because there are lots of different situations to verify (local uploads, s3, s3-compatible, with cdn, without cdn, etc).

4 лайка

I just checked a PDF that I uploaded to a topic today on a recently updated site and clicking the pdf link opens the PDF url on the CDN in my browser, fwiw

2 лайка

This should be resolved since SECURITY: Download allowlist for uploaded files · discourse/discourse@9c0642a · GitHub

We now have centralised logic to determine which files should be shown ‘inline’. That means that PDFs are consistently shown inline, and some less safe file types are consistently served as downloads. These changes should work on all types of upload storage (local & S3, with or without CDNs).

5 лайков

This topic was automatically closed after 9 days. New replies are no longer allowed.

David, I’ve just tested this - and while this definitely seems fixed on desktop, it doesn’t seem to be the case for mobile.

When I click on a PDF link in mobile, I get download (which I don’t want). I want it opening in the browser directly (i.e. the same as desktop), regardless of whether the TC is installed or not.

Can you expand on your setup (S3? CDN?), and your mobile device OS?

Clicking the pdf in the OP here on my iPhone seems to open inline correctly.

1 лайк

What about mp4 files? On most of websites, links to mp4 naturally plays them in the browser, but not in discourse, which is rather annoying :slight_smile:

Example: https://unicyclist.com/uploads/default/original/3X/4/f/4fb882b8ca5a0b0e3d75ff932506d57325f5582c.mp4

Expected behavior: https://d.canapin.dev/uploads/default/original/2X/4/4fb882b8ca5a0b0e3d75ff932506d57325f5582c.mp4

In theory, mp4 should be served inline:

which uses:

How did you achieve this? i.e. what’s the difference from unicyclist.com?

Sorry I should have mentioned the distinction. The latter uses a plugin: Discourse Video Inline

On meta, discourse forces mp4 downloads as well: https://d11a6trkgmumsb.cloudfront.net/original/4X/3/f/0/3f09f895d21cf0ae897d90c947abb816830b00a2.mp4

2 лайка

It has changed!!! I’ve just updated again today, and they are opening inline on mobile just fine.

Did you tweak something for us ‘simple self-hosters (no CDN/S3)’? Or was this some dumb caching thing?

No tweaks since the security fix linked above. It could’ve been caching, especially if you did the upgrade via the UI the first time. If you’ve now done a full rebuild, then that will’ve reset the NGINX cache. Glad to hear it’s working now!

I opened a new topic for the mp4 issue

3 лайка

This topic was automatically closed after 3 days. New replies are no longer allowed.