mimetype Go Package

repository·master·Indexed 24 days ago

https://github.com/gabriel-vasile/mimetype

A Go package for detecting MIME types and file extensions using magic numbers. It features a hierarchical detection structure for performance, is goroutine-safe, and has no external dependencies. The library provides functions to detect types from byte slices, io.Reader, or file paths, and allows for custom format extensions via the Extend method.

Tokens
1.4K
Snippets
3
Records
14
Agent score
84%

What's inside mimetype

  1. How mimetype detection works

    master

    The library uses a hierarchical detection structure to optimize performance. This approach is particularly useful for container formats; for example, once a file is identified as a ZIP archive, the library skips generic checks (like text vs. binary) and instead checks if it matches specific container formats like Microsoft Office files.

    To maintain efficiency and avoid loading entire files into memory, mimetype.DetectReader and mimetype.DetectFile only read the header of the input by default.

  2. Increase detection limit for specific file formats

    master

    Some file formats (such as Microsoft Office documents) store their magic number signatures towards the end of the file rather than the beginning. If a supported file is not being detected correctly, you can increase the number of bytes used for detection using mimetype.SetLimit().

    • mimetype.SetLimit(1024*1024): Sets the detection limit to 1MB.
    • mimetype.SetLimit(0): Disables the limit, using the entire file content for detection.
    mimetype.SetLimit(1024*1024) // Set limit to 1MB.
    // or
    mimetype.SetLimit(0) // No limit, whole file content used.
    
    mimetype.DetectFile("file.doc")
  3. Detect MIME types and extensions

    master

    The mimetype package provides three primary ways to detect a file's MIME type and extension based on magic numbers. Once detected, you can use the .String() method to get the MIME type string and .Extension() to get the file extension.

    Note: Content type detection using magic numbers should be a last resort, as it is slower and less accurate than using protocol metadata like the Content-Type header in HTTP or SMTP.

  4. Detect MIME type from a byte slice with Detect()

    master
    Use Detect(in []byte) to identify the MIME type of a provided byte slice. The function uses magic number signatures to perform detection. If identification fails, it returns application/octet-stream. The detection is limited by the value set via SetLimit() (defaulting to 4096 bytes).
  5. Detect MIME type from an io.Reader with DetectReader()

    master

    Use DetectReader(r io.Reader) to identify the MIME type of a stream.

    Important Considerations:

    • Offset: The reader must be at the start of the data. If using an io.ReadSeeker that has already been read, you must rewind it using reader.Seek(0, io.SeekStart) before calling DetectReader.
    • Error Handling: If identification fails, it returns application/octet-stream. Any returned error is specifically related to the reading process from the input reader.
    • Limit: If SetLimit is greater than 0, only up to that many bytes are read. If SetLimit is 0, the entire reader is consumed.
  6. Configure the detection byte limit with SetLimit()

    master

    Use SetLimit(limit uint32) to control the maximum number of bytes read from an input during detection.

    • Default: 4096 bytes.
    • Increasing the limit: Useful for file formats that store magic numbers towards the end of the file (e.g., docx, pptx, xlsx).
    • Setting to 0: Tells the library to use the entire input file/stream for detection.
    • Concurrency: This is safe to call from different goroutines.
  7. Extend detection for custom file formats with Extend()

    master

    Use Extend(detector func(raw []byte, limit uint32) bool, mime, extension string, aliases ...string) to add support for new file formats. This adds the new format to the root application/octet-stream MIME type.

    The detector function should return true if the provided raw bytes match the format.

  8. Detect MIME type from a file path with DetectFile()

    master
    Use DetectFile(path string) to identify the MIME type of a file on disk. This function handles opening and closing the file automatically. If identification fails or an error occurs during opening/reading, it returns application/octet-stream and the associated error.
  9. Compare MIME types with Is()

    master

    The Is(expectedMIME string) method checks if the current MIME type matches the expectedMIME string.

    It is robust because it:

    • Checks both the primary MIME type and any registered aliases.
    • Performs equality tests on the type/subtype section only.
    • Ignores optional MIME parameters (e.g., ; charset=utf-8).
    • Ignores leading/trailing whitespace.
    • Is case-insensitive.
    func (m *MIME) Is(expectedMIME string) bool
  10. Extend MIME detection with custom formats

    master

    You can add support for new sub-formats to an existing MIME type using the Extend method.

    To extend a format, you must provide:

    1. A detector function: func(raw []byte, limit uint32) bool. This function returns true if the input matches the signature.
    2. The mime type string.
    3. The extension (including the leading dot, e.g., ".ext").
    4. Optional aliases.

    Note: A sub-format will only be detected if all detectors in its parent chain also return true.

  11. Lookup a MIME object with Lookup()

    master
    Use Lookup(m string) *MIME to find a *MIME object by its string representation. The input string can be the main MIME type or one of its aliases. The function automatically parses the input to strip optional parameters before searching.