Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MIME::Types#type_for doesn't find an extension added with MIME::Type#add_extensions #84

Closed
Ddam opened this issue Nov 24, 2014 · 11 comments · Fixed by #101 or #111
Closed

MIME::Types#type_for doesn't find an extension added with MIME::Type#add_extensions #84

Ddam opened this issue Nov 24, 2014 · 11 comments · Fixed by #101 or #111
Assignees
Labels
Milestone

Comments

@Ddam
Copy link

Ddam commented Nov 24, 2014

I need to add an extension to the existing mime type application/octet-stream.
I tried doing this:

MIME::Types["application/octet-stream"].first.add_extensions("mo")
MIME::Types.of('filename.mo').first.to_s # => returns ""

When looking at the code, it seems that the variable @extension_index is not refreshed.
I also tried calling index_extensions after adding the new extension: it works, but is deprecated.

@halostatue halostatue added the Bug label Nov 24, 2014
@halostatue halostatue self-assigned this Nov 24, 2014
@halostatue
Copy link
Member

I need to think about how to do this, but it is a bug.

@mehulkar
Copy link

I see in the History.rdoc that this deprecation happened in 2013, but I don't see the reasoning behind the API change. Any insight on why it was deprecated?

@halostatue
Copy link
Member

@mehulkar it’s something that should not be worried about by the consumer of the library. I just need to figure out how to hook this in. I don’t think I can fix this until 3.x when a feature introduced in an upcoming release becomes default behaviour.

@mehulkar
Copy link

I see, thanks for the response @halostatue.

I use index_extensions to add custom file extensions for mime types. If I don't have these extensions added to the mime types, then paperclip (ruby library for image uploading/processing) fails spoof detection and I have to disable. I'm not clear on if there's a different way I should be matching up file types and extensions now?

@halostatue
Copy link
Member

What I’m saying is that #index_extensions is not something you should have to call. Adding a new mime type, or updating a mime type in a registry should reindex the extensions for that registry. That is:

MIME::Types["application/octet-stream"].first.add_extensions("mo")
MIME::Types.index_extensions
MIME::Types.of('filename.mo').first.to_s # => returns "application/octet-stream"

should be:

MIME::Types["application/octet-stream"].first.add_extensions("mo")
MIME::Types.of('filename.mo').first.to_s # => returns "application/octet-stream"

That it isn’t is a bug. Right now, MIME::Types don’t known anything about the container(s) that they belong to. With the columnar store implementation we are creating is that knowledge, but I’m not hooking this particular behaviour in place quite yet (because extensions are loaded first).

I prefer not leaving this bug in place, but I don’t have a good way to do this that doesn’t potentially result in cyclical dependencies or use a potentially unsafe weak reference or add an external dependency. I will figure it out for 3.0.

@halostatue halostatue added this to the 3.0 milestone May 20, 2015
@halostatue halostatue mentioned this issue May 25, 2015
halostatue added a commit that referenced this issue May 25, 2015
Release 2.6

Fixes #83 (Decrease Memory Usage).
Fixes #84 (Requiring mime/types accounts for 2% of all application RAM).
Based on and Fixes #96.
@halostatue halostatue reopened this May 28, 2015
@halostatue
Copy link
Member

Didn’t mean to close this one with the release.

@aptinio
Copy link

aptinio commented May 28, 2015

Do you think we can add a new public method to MIME::Types for this? Something like:

class MIME::Types
  def add_type_extensions(type_id, *extensions)
    type = self[type_id]
    type.add_extensions(*extensions)
    index_extensions!(type)
  end
end

I'd be happy to write the tests and submit a PR.

@aptinio
Copy link

aptinio commented May 28, 2015

We could also possibly make a MIME::Type keep track of all the MIME::Types it's been added to and call index_extensions on each one of those when adding extensions to a type.

@aptinio
Copy link

aptinio commented May 28, 2015

We could also have a configure method that accepts a block wherein types and extensions can be added and have all the caches updated after executing the block.

@aptinio
Copy link

aptinio commented May 28, 2015

Just throwing out these ideas here. :-) BTW, thanks for all your work on this gem!

@halostatue
Copy link
Member

I don’t see it happening in the 2.x release, and that API isn’t what I would want for this. Basically, that has the problem that MIME::Types['application/word'] returns two items…and that the more I look at it, #priority_sort is sorting on the wrong thing first (it uses the simplified type—another design problem now that the x- prefix has been retired)…

Basically, there will be one of two ways that this works:

  1. add_type Just Works for MIME::Types that are put into a registry. This is preferred, but will require that I understand how WeakRef works and/or add a dependency on ref to get this right.
  2. Alternatively, there is a #change_type method that encapsulates the workflow, outlined in your proposed mechanism, but does does it through some sort of temporary proxy object (e.g., #change_type(type) { |type| type.add_extensions(…) }).

Those are the main methods I’m considering, but they are all bigger changes than I am comfortable with for 2.x. One of the nice things that I will have is that I’m probably going to drop Ruby 1.9 support with mime-types 3.

halostatue added a commit that referenced this issue Nov 21, 2015
- Require Ruby 2.0 or later. Resolves #97.
- Remove deprecated methods.
- Update known registries when a MIME type extension changes. Resolves #84.
- Relicensed mime-types 3.0 as MIT only. Resolves #95.
- Extracted data from this gem to mime-types-data; removed deprecated data.
- Rewrote tests to better understand what is being tested—some of the tests
  were almost ten years old and didn’t make a lot of sense with this version. I
  have switched to minitest/spec with assertions.
- Columnar data is now the default registry store.
halostatue added a commit that referenced this issue Nov 21, 2015
- Require Ruby 2.0 or later. Resolves #97.
- Remove deprecated methods.
- Update known registries when a MIME type extension changes. Resolves #84.
- Relicensed mime-types 3.0 as MIT only. Resolves #95.
- Extracted data from this gem to mime-types-data; removed deprecated data.
- Rewrote tests to better understand what is being tested—some of the tests
  were almost ten years old and didn’t make a lot of sense with this version. I
  have switched to minitest/spec with assertions.
- Columnar data is now the default registry store. Because JSON is not required
  by default, this change resolves #85.
- MIME::Types containers are now implemented with Set instead of Array to
  prevent data duplication. Resolves #79.
halostatue added a commit that referenced this issue Nov 21, 2015
- Require Ruby 2.0 or later. Resolves #97.
- Remove deprecated methods.
- Update known registries when a MIME type extension changes. Resolves #84.
- Relicensed mime-types 3.0 as MIT only. Resolves #95.
- Extracted data from this gem to mime-types-data; removed deprecated data.
- Rewrote tests to better understand what is being tested—some of the tests
  were almost ten years old and didn’t make a lot of sense with this version. I
  have switched to minitest/spec with assertions.
- Columnar data is now the default registry store. Because JSON is not required
  by default, this change resolves #85.
- MIME::Types containers are now implemented with Set instead of Array to
  prevent data duplication. Resolves #79.
jsonn pushed a commit to jsonn/pkgsrc that referenced this issue Dec 14, 2015
== 3.0 / 2015-11-21

* 2 governance changes

  * This project and the related mime-types-data project are now exclusively
    MIT licensed. Resolves
    {#95}[mime-types/ruby-mime-types#95].

  * All projects under the mime-types organization now have a standard code of
    conduct adapted from the {Contributor
    Covenant}[http://contributor-covenant.org]. This text can be found in the
    {Code-of-Conduct.rdoc}[Code-of-Conduct_rdoc.html] file.

* 3 major changes

  * All methods deprecated in mime-types 2.x have been removed.
  * mime-types now requires Ruby 2.0 compatibility or later. Resolves
    {#97}[mime-types/ruby-mime-types#97].
  * The registry data has been removed from mime-types and put into
    mime-types-data, maintained and released separately. It can be found at
    {mime-types-data}[https://github.com/mime-types/mime-types-data].

* 17 minor changes:

  * MIME::Type changes:

    * Changed the way that simplified types representations are creatd to
      reflect the fact that +x-+ prefixes are no longer considered special
      according to IANA. A simplified MIME type is case-folded to lowercase. A
      new keyword parameter, +remove_x_prefix+, can be provided to remove +x-+
      prefixes.
    * Improved initialization with an Array works so that extensions do not
      need to be wrapped in another array. This means that <tt>%w(text/yaml
      yaml yml)</tt> works in the same way that <tt>['text/yaml', %w(yaml
      yml)]</tt> did (and still does).
    * Changed +priority_compare+ to conform with attributes that no longer
      exist.
    * Changed the internal implementation of extensions to use a frozen Set.
    * When extensions are set or modified with +add_extensions+, the primary
      registry will be informed of a need to reindex extensions. Resolves
      {#84}[mime-types/ruby-mime-types#84].
    * The preferred extension can be set explicitly. If not set, it will be the
      first extension. If the preferred extension is not in the extension list,
      it will be added.
    * Improved how xref URLs are generated.
    * Converted +obsolete+, +registered+ and +signature+ to attr_accessors.

  * MIME::Types changes:

    * Modified MIME::Types.new to track instances of MIME::Types so that they
      can be told to reindex the extensions as necessary.
    * Removed +data_version+ attribute.
    * Changed #[] so that the +complete+ and +registered+ flags are keywords
      instead of a generic options parameter.
    * Extracted the class methods to a separate file.
    * Changed the container implementation to use a Set instead of an Array to
      prevent data duplication. Resolves
      {#79}[mime-types/ruby-mime-types#79].

  * MIME::Types::Cache changes:

    * Caching is now based on the data gem version instead of the mime-types
      version.
    * Caching is compatible with columnar registry stores.

  * MIME::Types::Loader changes:

    * MIME::Types::Loader::PATH has been removed and replaced with
      MIME::Types::Data::PATH from the mime-types-data gem. The environment
      variable RUBY_MIME_TYPES_DATA is still used.
    * Support for the long-deprecated mime-types v1 format has been removed.
    * The registry is default loaded from the columnar store by default. The
      internal format of the columnar store has changed; many of the boolean
      flags are now loaded from a single file. Resolves
      {#85}[https://github.com/mime-types/ruby-mime-types/85].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants