Separate registry content type and content disposition

This commit is contained in:
Joshua Peek
2011-06-02 12:14:19 -05:00
parent 8fc89d1d4f
commit bcc2042768
8 changed files with 298 additions and 21 deletions

View File

@@ -2,7 +2,6 @@ require 'linguist/language'
require 'linguist/mime'
require 'linguist/pathname'
require 'escape_utils'
require 'yaml'
module Linguist
@@ -63,12 +62,7 @@ module Linguist
#
# Returns a content disposition String.
def disposition
case content_type
when 'application/octet-stream', 'application/java-archive'
"attachment; filename=#{EscapeUtils.escape_url(pathname.basename)}"
else
'inline'
end
pathname.disposition
end
# Public: Is the blob text?

View File

@@ -1,7 +1,11 @@
# MIME types and extensions to override when the raw blob is served
# mime types
application/javascript: text/plain
application/perl: text/plain
application/python: text/plain
application/rdf+xml: text/plain
application/ruby: text/plain
application/sh: text/plain
application/tex: text/plain
application/xhtml+xml: text/plain

View File

@@ -1,11 +1,42 @@
require 'mime/types'
require 'yaml'
module MIME
class Type
attr_accessor :binary
undef_method :binary?
def binary?
if defined? @binary
@binary
else
@encoding == 'base64'
end
end
attr_accessor :attachment
def attachment?
if defined? @attachment
@attachment
else
binary?
end
end
end
end
# Register additional mime type extensions
mime_extensions = YAML.load_file(File.expand_path("../mimes.yml", __FILE__))
mime_extensions.each do |mime_type, exts|
mime_extensions.each do |mime_type, options|
mime = MIME::Types[mime_type].first || MIME::Type.new(mime_type)
exts.each { |ext| mime.extensions << ext }
(options['extensions'] || []).each { |ext| mime.extensions << ext }
mime.binary = options['binary'] if options.key?('binary')
mime.attachment = options['attachment'] if options.key?('attachment')
MIME::Types.add_type_variant(mime)
MIME::Types.index_extensions(mime)
end
@@ -59,5 +90,49 @@ module Linguist
type
end
# Internal: Determine if extension or mime type is binary.
#
# ext_or_mime_type - A file extension ".txt" or mime type "text/plain".
#
# Returns true or false
def self.binary?(ext_or_mime_type)
mime_type = lookup_mime_type_for(ext_or_mime_type)
mime_type.nil? || mime_type.binary?
end
# Internal: Determine if extension or mime type is an attachment.
#
# ext_or_mime_type - A file extension ".txt" or mime type "text/plain".
#
# Attachments are files that should be downloaded rather than be
# displayed in the browser.
#
# This is used to set our Content-Disposition headers.
#
# Attachment files should generally binary files but non-
# attachments do not imply plain text. For an example Images are
# not treated as attachments.
#
# Returns true or false
def self.attachment?(ext_or_mime_type)
mime_type = lookup_mime_type_for(ext_or_mime_type)
mime_type.nil? || mime_type.attachment?
end
# Internal: Lookup mime type for extension or mime type
#
# Returns a MIME::Type
def self.lookup_mime_type_for(ext_or_mime_type)
ext_or_mime_type ||= ''
if ext_or_mime_type =~ /\w+\/\w+/
guesses = ::MIME::Types[ext_or_mime_type]
else
guesses = ::MIME::Types.type_for(ext_or_mime_type)
end
guesses.first
end
end
end

View File

@@ -2,13 +2,75 @@
#
# Review this list if we ever upgrade from mime-types 1.15 to 1.16
application/atom+xml:
binary: false
application/javascript:
binary: false
extensions:
- js
application/json:
binary: false
extensions:
- json
application/rdf+xml:
binary: false
application/sh:
binary: false
application/x-troff-ms:
binary: false
application/netcdf:
binary: false
application/x-perl:
binary: false
extensions:
- pl
- pm
application/x-python:
binary: false
extensions:
- py
application/x-ruby:
binary: false
extensions:
- rb
application/x-wais-source:
binary: false
application/vnd.mozilla.xul+xml:
binary: false
application/octet-stream:
- dmg
- dll
binary: true
extensions:
- dmg
- dll
application/java-archive:
- ear
- war
binary: true
extensions:
- ear
- war
application/x-shockwave-flash:
- swf
binary: true
extensions:
- swf
image/gif:
attachment: false
image/jpeg:
attachment: false
image/png:
attachment: false

View File

@@ -1,6 +1,8 @@
require 'linguist/language'
require 'linguist/mime'
require 'escape_utils'
module Linguist
# Similar to ::Pathname, Linguist::Pathname wraps a path string and
# provides helpful query methods. Its useful when you only have a
@@ -99,6 +101,30 @@ module Linguist
@content_type ||= Mime.content_type_for(extname)
end
# Public: Determine if the Pathname should be served as an
# attachment.
#
# Returns true or false.
def attachment?
@attachment ||= Mime.attachment?(extname)
end
# Public: Get the Content-Disposition header value
#
# This value is used when serving raw blobs.
#
# # => "attachment; filename=file.tar"
# # => "inline"
#
# Returns a content disposition String.
def disposition
if attachment?
"attachment; filename=#{EscapeUtils.escape_url(basename)}"
else
'inline'
end
end
def to_s
@path.dup
end