Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/file_signature.rb

Class Method Summary collapse

Class Method Details

.magic_number_type(file_name) ⇒ Object

Detect the file’s data type by opening the file then using IO#magic_number_type to read the first few bytes.

Return a magic number type symbol, e.g. :bitmap, :jpg, etc. Returns nil if the data type is unknown

Example:

File.magic_number_type("test.ps")
=> :postscript

See

- IO::MagicNumberTypeMap
- IO::MimeTypeMap
- IO#magic_number_type
- IO#mime_type
- File.mime_type


319
320
321
# File 'lib/file_signature.rb', line 319

def self.magic_number_type(file_name)
  File.open(file_name,"rb"){|f| f.magic_number_type }
end

.mime_type(file_name) ⇒ Object

Detect the file’s data type by opening the file then using IO#magic_number_type to read the first few bytes.

Return the MIME type of the file. Returns ‘application/octet-stream’ for unknown types.

Example:

File.mime_type("test.ps")
=> "application/postscript"

See

- IO::MagicNumberTypeMap
- IO::MimeTypeMap
- IO#magic_number_type
- IO#mime_type
- File.magic_number_type


340
341
342
# File 'lib/file_signature.rb', line 340

def self.mime_type(file_name)
  File.open(file_name,"rb"){|f| f.mime_type }
end