setting mime type for image files before they are encrypted with Shrine and Rails
Simply said: I think you're not quite doing this in the way Shrine intends and there's multiple ways to remedy this. I'll rank them from (in my opinion, based on complexity/appropriateness) best to worst:
- Encryption could/should be treated as a part of Shrine's processing/derivatives concept. So you'd want to perform the processing there and set the metadata accordingly. The Shrine author himself has outlined solutions to process the original file in a thread here: https://discourse.shrinerb.com/t/keep-original-file-after-processing/50.
- You can override metadata manually: https://shrinerb.com/docs/metadata#controlling-extraction. This would probably mean not using attribute based assignment but instead calling the attacher directly (see below).
- You can define your own MIME type determining logic as set out here: https://shrinerb.com/docs/plugins/determine_mime_type.
- You're using
Shrine.mime_type
already. Use the value you get from this and store it in a separate database columnmime_type
on yourid_docs
model and then use it when you're reading from the database. Note that this probably implicitly means that the value in this column and the value you get from the file's metadata's mime type are out of sync.
Using the attacher directly:
mime_type = Shrine.mime_type(image)
# ...
@id_doc = IdDoc.create(user_id: current_user.id) do |id_doc|
id_doc.image_attacher.model_assign(encrypted_image, metadata: { mime_type: mime_type })
end
Rails: MIME type issues with .m4v files
You need to add the following lines to your config/initializers/mime_types.rb
file:
# register MIME type with Rails
Mime::Type.register "video/mp4", :m4v
# register MIME type with MIME::Type gem
MIME::Types.add(MIME::Type.from_array("video/mp4", %(m4v)))
Now in the console you can test the results
MIME::Types.type_for("abc.m4v").to_s
#=> "video/mp4"
Setting MIME types using the ASP.NET Development Server
The built-in development web server in Visual Studio (Cassini) has no knowledge of <system.webServer>
, only IIS7.x or IIS7.5 Express will consume these settings.
Also the static file content types in Visual Studio's development web server are hard coded.
From Microsoft.VisualStudio.WebHost.Connection
(disassembled using .NET Reflector):
private static string MakeContentTypeHeader(string fileName)
{
string str = null;
FileInfo info = new FileInfo(fileName);
switch (info.Extension.ToLowerInvariant())
{
case ".bmp":
str = "image/bmp";
break;
case ".css":
str = "text/css";
break;
case ".gif":
str = "image/gif";
break;
case ".ico":
str = "image/x-icon";
break;
case ".htm":
case ".html":
str = "text/html";
break;
case ".jpe":
case ".jpeg":
case ".jpg":
str = "image/jpeg";
break;
case ".js":
str = "application/x-javascript";
break;
}
if (str == null)
{
return null;
}
return ("Content-Type: " + str + "\r\n");
}
To be honest, with the advent of IIS7.5 Express I can't see why you'd want to use the built-in web server. Cassini can be the cause of so much confusion when it comes to deployment time on a production server because it's nothing like the real deal (security, configuration etc) whereas if you can get your site running on IIS7.5 Express then there's a fairly high probability that deployment onto a production IIS7.5 server will "just work".
I wouldn't be surprised if Microsoft yanked the Cassini server from the next version of Visual Studio given how easy it is to run with IIS7.5 Express.
Paperclip uploads for office files (docx,pptx) are being downloaded as zip files?
It turns out, as Marc B first hinted at - that all Office documents that end in x
are indeed zipped XML files. Anything that uses normal mimetypes will assume that it's a zipped file.
To get around this, you have to register the Office mimetypes with your server. So, for your .pptx files, you put
Mime::Type.register "application/vnd.openxmlformats-officedocument.presentationml.presentation", :pptx
in your config/initializers/mime_types.rb file.
Alternatively, you can use the Rack::Mime::MIME_TYPES.merge!()
method, which is seen in action in this Stackoverflow answer, if you have to support all of the Office 2007 files.
Related Topics
Cannot Load Such File -- Rack/Handler/Puma
Rspec 'Eq' VS 'Eql' in 'Expect' Tests
How to Convert Activerecord Table Name to Model Class Name
Stack Level Too Deep When Using Carrierwave Versions
Using Ruby to Generate Sha512 Crypt-Style Hashes Formatted for /Etc/Shadow
Why Is Uri.Escape() Marked as Obsolete and Where Is This Regexp::Unsafe Constant
Require': Cannot Load Such File -- Spec_Helper (Loaderror)
How to Parse a CSV File Located in a Amazon S3 Bucket
Rails Script Segmentation Fault with Rvm
Extract All Urls Inside a String in Ruby
In Ruby, How to Find Out If a String Is Not in an Array
Is It Necessary to Close Stringio in Ruby
Ssl_Connect Syscall Returned=5 Errno=0 State=Sslv2/V3 Read Server Hello A
Activerecords Select(:Id).Collect VS. Pluck(:Id) Methods: Why Is Pure Ar "Pluck" Slower
How to Inherit from Nilclass or How to Simulate Similar Function