Caddy2: how to set MIME type and Content-disposition for a file extension? - mime-types

What is the way to configure/override how certain MIME types are handled?
For example, with the default configuration a python file (.py) is served with Content-disposition": attachment which opens a file save dialog rather than displaying inline plain text in the browser.
I've identified one possible approach:
#py {
path *.py
}
header #py Content-Type text/plain
While this works, it looks more of a hack rather than adding/modifying a mime type, given that it's simply setting a header based on a substring match. Is this the only possible approach?
What defines that a specific MIME type should be served inline or have "Content-disposition": attachment? Does caddy expose a straightforward way to configure how MIME types are handled?
The correct mime type for .py files is application/x-python-code or text/x-python. How can I configure caddy to treat these types as inline plain text, and not add the Content-disposition": attachment header? Forcing text/plain works as a workaround, but I am assuming there is a cleaner way to do it.

Related

Apex File Browse... Mime type restriction text/csv

I want to restrict the files seen by the Apex (5.1) File Browse to only CSV files. I am scratching my head on this one as when I set the mime type attribute to mime/csv I get all files. I have tried other mime types text/*, image/* they work as intended, I only see files these types of files. When I do upload CSV files Oracle sets the mime_type field in apex_application_temp_files to text/csv so this is internally recognised. I also have the multi-file select option set.
What am I doing wrong? If this is actually is a bug is there a work around?
Thanks
Bob
Oracle 12c DB, Tomcat, Chrome
Try this:
<!-- (IE 10+, Edge, Chrome, Firefox 42+) -->
<input type="file" accept=".xls,.xlsx" />
Fill the field "Custom Attributes"
Reference: Limit file format when using <input type="file">?
If you need to restrict the upload only for one type of files, so, create a constraint in your table that check if the mimetype is of this type.
Restriction e.g. to Excel (XLSX)
Under "Setting" set "File Types" with the following content to restrict file browse to XLSX only.
accept=".xls,.xlsx, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel
#See source: https://w3c.github.io/html/sec-forms.html#element-attrdef-input-accept

go swagger how to add support for all mime type

Swagger by default supports application/json as Content type. And if I want to add other Content types then "produces" or "consumes" fields needs to updated in the swagger JSON file. Client may send any type of Data. Is there a way to support any Content type (remove the validation for Content type or regex like *) in swagger
As far as I have worked on go-swagger, you will have to mention in the swagger-spec file and generate the code again in order to do that. You can use / to describe that the endpoint can generate all type of data. But you can not modify it after you have generated the code using go-swagger.
You can directly modify the generated files but it's not recommended.

Change default content type for previewing site with "nanoc view"

I generate HTML files without any filetype extension with the following route in nanoc’s Rules file:
route '/blog/*/' do
item.identifier.chop
end
So the file /content/blog/hello-world.html gets generated as /output/blog/hello-world, leading to the URL path /blog/hello-world.
When using nanoc’s built-in preview server (nanoc view, which uses adsf and WEBrick, as far as I understand), these extension-less HTML files get interpreted as plain text.
For previewing with nanoc view, how can I set the default content type (i.e. text/html) for files without filetype extensions?
(When publishing this site, I set the correct content type in Apache’s .htaccess file so that the files get interpreted as HTML, but WEBrick/adsf don’t seem to support .htaccess.)
You should be writing those to /output/blog/hello-world/index.html instead. That makes them work seamlessly in nanoc view, Apache (with no .htaccess), even by just opening the site files in your browser (assuming you're using relativized links).

Override a default mime-type when using the Sling MimeTypeService

I am using the Sling MimeTypeService in order to get an appropriate extension for a given file & trying to override a default extension with my own.
E.g. if I had a method:
#Reference
MimeTypeService mimeTypeService;
public void getPlainTextAsDiff() {
mimeTypeService.registerMimeType("text/plain", ".diff");
mimeTypeService.getExtension("text/plain"); //returns ".txt"
}
This returns ".txt" rather than ".diff", ignoring the first line of the method.
This also seems to be inline with the documentation:
A MIME type may be mapped to multiple extensions (e.g. text/plain to
txt, log, ...). This method is expected to returned one of those
extensions. It is up to the implementation to select an appropriate
extension if multiple mappings exist for a single MIME type.
[My emphasis]
I'm wondering if it's possible to circumvent this somehow, e.g. deregistering a mime-type or ranking those available, so that in the case above "diff" would guaranteed to be returned?
You can extend mime type mappings by providing MimeTypeProvider services, but looking at the MimeTypeServiceImpl code it looks like those are not sorted by service ranking. If I'm correct, you won't have a way to prioritize you MimeTypeProviders. You might want to experiment with this and submit an enhancement request or patch.

Is there a "default" MIME type?

Is there what could be considered a "default" mimetype?
I've seen "unknown/unknown" and "application/binary". But is there a default to revert to when no other MIME type is found?
The least specific official MIME type is application/octet-stream. Without any additional information, it says "here is a bunch of bytes, hopefully there is an application over on your end which knows what to do with them". Sometimes there is a file name which helps convey to the recipient what to do with the data.
"unknown" doesn't really add anything over this, except to confuse clients who don't support random unofficial MIME types. Ditto for application/binary; it's just a non-standard way of restating "octet-stream".
This is the answer to "What can I put in the Content-Type: header if I can't find an existing content type which adequately describes my data?" which is how I have interpreted this question. The proposed duplicate Unknown file type MIME? has a lengthy answer which discusses "How is my data interpreted if I don't put a valid Content-Type: header?" specifically in an HTTP context; the answer to that is protocol-specific (in email, for example, the default implied Content-Type: for MIME body parts which do not contain this header is text/plain; charset="us-ascii").

Resources