Support custom Content-Type
Closed this issue · 2 comments
Middleman 3.1.1 adds support for proxies with custom content-types: bhollis/middleman@7a4aa10
In our case we have an extensionless proxy that points to an html file:
proxy 'faq', 'faq.html'
When going to that URL on s3 the browser downloads the file because the default content type for files without an extension is application/octet=stream
. I realized that this has to do with mime-type lookup coming from asset_sync:
https://github.com/rumblelabs/asset_sync/blob/master/lib/asset_sync/multi_mime.rb
We considered setting the content-type manually on s3 for the files in question, but that's not sustainable.
Since middleman uses Rack::Mime
our workaround is adding a mime-type for files without an extension (which also works on middleman 3.1):
Rack::Mime::MIME_TYPES.merge!('.' => 'text/html')
@damrbaby Thanks for the detailed issue. I'm currently re-writing middleman-sync to use multi_sync ( my multi-threaded re-write of asset_sync ). Your use case is something I've got in the pipeline as making easier and hopefully would look like something below ( the multi_sync bit, I would then abstract this into middleman-sync )
MultiSync.run do
source :build {
:type => :local, # :local is the source's type, current options are :local
:source_dir => "/build",
:targets => :www # an array of target name(s) that this source should sync against.
}
source : faq {
:type => :local,
:targets => :www
:source_dir => "/build",
:include => "faq.html",
:content_type => "text/html" # something like this, where this source's files are forced to be a specific content_type
}
target :www {
:type => :aws, # :aws is the target's type, current options are :aws
:target_dir => "your_aws_bucket",
:destination_dir => "an_optional_directory_inside_your_aws_bucket",
:credentials => {
:region => "us-east-1",
:aws_access_key_id => "super_secret",
:aws_secret_access_key => "super_secret"
}
}
end
Anyways, I'll keep this issue open and work to build in to middleman-sync / multi_sync and multi_mime this edge case.
Thanks again.
s3_sync
is dependent on the mime-types
gem for taking a resource path and determining the content type. I've got a project I was needing to change the content type of extensionless files, and until the next version is out I've managed to hack the content type by adding the following to my config.rb
:
mime_type_html = MIME::Types["text/html"].first
mime_type_html.add_extensions(["extensionlessfilename1", "extensionlessfilename1", etc)
MIME::Types.index_extensions(mime_type_html)
For extensionless paths the mime-types
gem will match the filename to a mime type instead. I already know what all my extensionless files will be as they are pregenerated and I have them in an array, so I just pass it into the add_extensions
function.
Note: This isn't ideal, I'm only doing this until the next version of s3_sync
comes out that has the new facility for controlling content type.