Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Preview of csv file downloads file instead of showing a preview
### Description

I archive some csv file for my business with financial data and I just noticed that the preview function while hovering over the eye button downloads those files instead of showing the content just like for a plain txt file.

This is quite annoying if I move the mouse over the screen and 5+ downloads will be started.

### Steps to reproduce

1. Upload a csv into paperless (if the consumer complains about an unsupported mime-type just add the returned type to ```paperless-ngx/src/paperless_text/signals.py```)
2. Hover over the eye icon of the uploaded file
3. The file will be downloaded by the browser

### Webserver logs

_No response_

### Paperless-ngx version

1.9.2

### Host OS

Debian bullseye

### Installation method

Bare metal

### Browser

Chrome

### Configuration changes

_No response_

### Other

_No response_


Discussion:
Hmm, I cant reproduce this myself (locally, on demo site). See below is a screenshot of the preview of a random csv file I found. I wonder if theres more to it. Speaking of mime types, what does your paperless show under Metadata for this/these files?

<img width="567" alt="Screen Shot 2022-10-02 at 5 56 37 PM" src="https://user-images.githubusercontent.com/4887959/193484772-dead8599-9681-46bb-a9c9-311c72fedf09.png">

Metadata looks like this:
![image](https://user-images.githubusercontent.com/12427722/193514699-7f033a3a-5cac-4d9a-aef3-608f84cd983f.png)
I bet your system uses a newer version of the file command so that CSVs are recognized with a "text/csv" mime-type. Try downgrading to file 5.39.
OR
Your mime-type will be recogized as "text/plain" that was the case on my paperless when I used the content you used in your screenshot. Try using a CSV with column information like this:
```
name,age
foo,30
bar,35
```
Ok I managed to reproduce this with [this file](https://github.com/paperless-ngx/paperless-ngx/files/9695328/2022-10-03.Sample100.csv) I found. The thing is, Im not sure of the best way to solve it.

- Displaying csv files in native html isnt supported, so I think the 'right' thing to do here would be to disable the preview button for these.
- Problem is we dont get the file type without making a second call to the `metadata` endpoint, obviously we cant do that on the document list page with dozens of docs.

Possible ideas, would love some thoughts on this:

- Have the `api/documents` endpoint include this e.g. by passing the file type with the document
- The frontend tries to parse the type from the filename, obviously potential issues with that
- Any other ideas?
I mean a csv file also just contains plain text.
Couldn't you overwrite the saved mime-type with "text/plain" here if disposition is set to "inline" like in the preview call?: https://github.com/paperless-ngx/paperless-ngx/blob/8feada6907a616c16956646810342dca11e18a02/src/documents/views.py#L263

Or would that be too ugly a workaround?

If not we need this for "application/csv" and "text/csv" as I fixed it in #1726 

Resolving patch:
diff --git a/src/documents/views.py b/src/documents/views.py
--- a/src/documents/views.py
+++ b/src/documents/views.py
@@ -261,6 +261,9 @@ def file_response(self, pk, request, disposition):
             file_handle = doc.source_file
             filename = doc.get_public_filename()
             mime_type = doc.mime_type
+            # Support browser previewing csv files by using text mime type
+            if mime_type in {"application/csv", "text/csv"} and disposition == "inline":
+                mime_type = "text/plain"
 
         if doc.storage_type == Document.STORAGE_TYPE_GPG:
             file_handle = GnuPG.decrypted(file_handle)


Test patch: