[GH-ISSUE #458] Add a feature to serve any file as text/plain in the browser #249

Closed
opened 2026-04-08 16:51:28 +03:00 by zhus · 3 comments
Owner

Originally created by @tkapias on GitHub (Oct 1, 2024).
Original GitHub issue: https://github.com/sigoden/dufs/issues/458

Specific Demand

Can you add a button in the web interface, like the edit button, which would force to display any file as text/plain in a new browser tab?

  • It need to override browser internal mimetype features.
  • It should be disabled for binary files.
  • Would be nice if it could be called directly like https://dufs.example.com/path/file.ext?plain.

Context

Before Dufs, I had too many file servers (seafile, privatebin, syncthing, kopia...). I got ride of seafile and I would like to replace privatebin's usage by dufs too.

The main issue is that sometime I would like to display some content in the browser with syntax highlighting.

For plain files, it 's pretty easy:

  • Files with a mimetype text/plain are displayed as text in Firefox.
  • I use the addon Enlight to apply coloration.

But, there are 2 issues:

  • Until recently Firefox did not display all text/* types, some of them where pushed for download. Even markdown's .md were like that.

Apparently, Firefox's release 128 changed that: bugzilla-1319262, bugzilla-196078. But it will take some time to get this on Firefox ESR.
I tried to force mimetypes in nginx, but apparently Firefox ultimately parse the extension.

types        { text/plain txt md log; }
default_type text/plain;
  • Even if all text/* mime types are considered as plain in Firefox, there are other filetypes that a user would like to view, and there are also other browsers with the same issue.

Implement Suggestion

Not sure.

Maybe use js to serve the file with a fake extension and mimetype by appending .plain to it?

But testing binary files probably needs to be done server-side.

Originally created by @tkapias on GitHub (Oct 1, 2024). Original GitHub issue: https://github.com/sigoden/dufs/issues/458 ## Specific Demand Can you add a button in the web interface, like the edit button, which would force to display any file as text/plain in a new browser tab? - It need to override browser internal mimetype features. - It should be disabled for binary files. - Would be nice if it could be called directly like `https://dufs.example.com/path/file.ext?plain`. ### Context Before Dufs, I had too many file servers (seafile, privatebin, syncthing, kopia...). I got ride of seafile and I would like to replace privatebin's usage by dufs too. The main issue is that sometime I would like to display some content in the browser with syntax highlighting. For plain files, it 's pretty easy: - Files with a mimetype text/plain are displayed as text in Firefox. - I use the addon [Enlight](https://github.com/Qeole/Enlight) to apply coloration. But, there are 2 issues: - Until recently Firefox did not display all `text/*` types, some of them where pushed for download. Even markdown's `.md` were like that. Apparently, Firefox's release 128 changed that: [bugzilla-1319262](https://bugzilla.mozilla.org/show_bug.cgi?id=1319262), [bugzilla-196078](https://bugzilla.mozilla.org/show_bug.cgi?id=196078). But it will take some time to get this on Firefox ESR. I tried to force mimetypes in nginx, but apparently Firefox ultimately parse the extension. ```nginx types { text/plain txt md log; } default_type text/plain; ``` - Even if all `text/*` mime types are considered as plain in Firefox, there are other filetypes that a user would like to view, and there are also other browsers with the same issue. ## Implement Suggestion Not sure. Maybe use js to serve the file with a fake extension and mimetype by appending `.plain` to it? But testing binary files probably needs to be done server-side.
zhus closed this issue 2026-04-08 16:51:28 +03:00
Author
Owner

@tkapias commented on GitHub (Oct 1, 2024):

I just saw that you can get the editor to display the file content with ?view (without edition toolbar) and that it does check for binaries or large files.

It's really close to what I requested.

New Suggestion

  • Create a new parameter ?plain.

Differences with ?view:

  • Open in a new tab.

  • Make class="editor-page" the only element in the body.

  • Maybe disable the editing feature for this view.

  • Add 2 new buttons for "View file" and "View plain file", near the "Edit file" in the index.

<!-- gh-comment-id:2385383145 --> @tkapias commented on GitHub (Oct 1, 2024): I just saw that you can get the editor to display the file content with `?view` (without edition toolbar) and that it does check for binaries or large files. It's really close to what I requested. ## New Suggestion - Create a new parameter `?plain`. Differences with `?view`: - Open in a new tab. - Make `class="editor-page"` the only element in the body. - Maybe disable the editing feature for this view. - Add 2 new buttons for "View file" and "View plain file", near the "Edit file" in the index.
Author
Owner

@sigoden commented on GitHub (Oct 2, 2024):

We will not support this feature. This demand is too niche.

To display the file in the browser with syntax highlighting, you should use browser extensions.

<!-- gh-comment-id:2387267903 --> @sigoden commented on GitHub (Oct 2, 2024): We will not support this feature. This demand is too niche. To display the file in the browser with syntax highlighting, you should use browser extensions.
Author
Owner

@tkapias commented on GitHub (Oct 2, 2024):

You were right not to work on it, after your message I found afew external solutions.

I just leave the comment for other users.


  • 3 solutions to force some mimetypes to be displayed inline in the browser:
    • You can force the conversion of any content-type to text/plain with the Content-Type Fixer extension.
    • Firefox 128 introduced more text/* content-types to those opening inline, but Firefox ESR will not get this feature for a long time.
    • If you use nginx as a proxy, you can force a rewrite of the header Content-Type for a selected file extension with proxy_hide_header and add_header:
location / {
    proxy_pass              http://127.0.0.1:8099;
    proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto $scheme;
    proxy_http_version      1.1;
    proxy_request_buffering off;
    proxy_connect_timeout   150s;
    proxy_send_timeout      36000s;
    proxy_read_timeout      1200s;
    send_timeout            36000s;
    client_max_body_size    0;

    location ~ \.md$ {
        proxy_pass              http://127.0.0.1:8099;
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;
        proxy_http_version      1.1;
        proxy_request_buffering off;
        proxy_connect_timeout   150s;
        proxy_send_timeout      36000s;
        proxy_read_timeout      1200s;
        send_timeout            36000s;
        client_max_body_size    0;
        proxy_hide_header       Content-Type;
        add_header              Content-Type "text/plain; charset=UTF-8";
    }
}
  • Then, you can use the Enlight extension to add syntax highlighting to the inline text.

Thanks for Dufs, it's a good tool.

<!-- gh-comment-id:2387603093 --> @tkapias commented on GitHub (Oct 2, 2024): You were right not to work on it, after your message I found afew external solutions. I just leave the comment for other users. --- - 3 solutions to force some mimetypes to be displayed inline in the browser: - You can force the conversion of any content-type to text/plain with the [Content-Type Fixer](https://addons.mozilla.org/en-US/firefox/addon/content-type-fixer/?utm_source=addons.mozilla.org&utm_medium=referral&utm_content=search) extension. - Firefox 128 introduced more `text/*` content-types to those opening inline, but Firefox ESR will not get this feature for a long time. - If you use nginx as a proxy, you can force a rewrite of the header Content-Type for a selected file extension with `proxy_hide_header` and `add_header`: ```nginx location / { proxy_pass http://127.0.0.1:8099; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_request_buffering off; proxy_connect_timeout 150s; proxy_send_timeout 36000s; proxy_read_timeout 1200s; send_timeout 36000s; client_max_body_size 0; location ~ \.md$ { proxy_pass http://127.0.0.1:8099; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_request_buffering off; proxy_connect_timeout 150s; proxy_send_timeout 36000s; proxy_read_timeout 1200s; send_timeout 36000s; client_max_body_size 0; proxy_hide_header Content-Type; add_header Content-Type "text/plain; charset=UTF-8"; } } ``` - Then, you can use the [Enlight](https://addons.mozilla.org/en-US/firefox/addon/enlight/) extension to add syntax highlighting to the inline text. Thanks for Dufs, it's a good tool.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sigoden/dufs#249