Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Mime Db Laravel Package

jshttp/mime-db

The canonical MIME type database from jshttp, published as JSON. It maps file extensions to media types and includes metadata such as compression and charset, powering Node.js HTTP libraries like mime-types and Express.

Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require jshttp/mime-db
    

    The package is dependency-free and requires no configuration—just autoload it via Composer.

  2. First Use Case: Detecting MIME Types

    use JSHttp\MimeDb\MimeDb;
    
    $mimeDb = new MimeDb();
    $mimeType = $mimeDb->getMimeType('application/json'); // Returns 'application/json'
    $extension = $mimeDb->getExtension('application/json'); // Returns 'json'
    
  3. Where to Look First

    • Core Class: JSHttp\MimeDb\MimeDb (main interface for lookups).
    • Data Source: The package includes a built-in database of MIME types (no external files needed).
    • Documentation: Check the GitHub repo for edge cases (e.g., custom type handling).

Implementation Patterns

Common Workflows

  1. MIME Type Lookup

    $mimeDb = new MimeDb();
    $type = $mimeDb->getMimeType('txt'); // 'text/plain'
    
    • Useful for file uploads, API responses, or content negotiation.
  2. Extension-to-MIME Mapping

    $extension = $mimeDb->getExtension('application/pdf'); // 'pdf'
    
    • Ideal for generating Content-Disposition headers or validating file types.
  3. Reverse Lookup (MIME → Extension)

    $mimeDb->getExtension('image/png'); // 'png'
    
    • Helps in dynamic file naming or storage systems.
  4. Integration with Laravel Filesystem

    use Illuminate\Support\Facades\Storage;
    
    $path = 'example.pdf';
    $mime = $mimeDb->getMimeType(pathinfo($path, PATHINFO_EXTENSION));
    Storage::disk('s3')->put($path, file_get_contents($path), ['mime_type' => $mime]);
    
  5. Custom MIME Types

    $mimeDb->addType('application/vnd.myapp+json', ['json']);
    $mimeDb->addExtension('myjson', 'application/vnd.myapp+json');
    

Best Practices

  • Cache Results: Wrap lookups in a cache layer (e.g., Laravel’s Cache::remember) for performance.
    $mime = Cache::remember("mime_{$extension}", 3600, fn() => $mimeDb->getMimeType($extension));
    
  • Fallback Logic: Handle unknown types gracefully:
    $mime = $mimeDb->getMimeType($extension) ?: 'application/octet-stream';
    
  • Laravel Service Provider: Bind MimeDb as a singleton for global access:
    $this->app->singleton(MimeDb::class, fn() => new MimeDb());
    

Gotchas and Tips

Pitfalls

  1. Case Sensitivity

    • Extensions/MIME types are case-insensitive in lookups, but the database stores them in lowercase. Avoid issues by normalizing inputs:
      $mimeDb->getMimeType(strtolower($extension));
      
  2. Missing Types

    • The database is comprehensive but not exhaustive. For niche types (e.g., application/vnd.custom), add them manually:
      $mimeDb->addType('application/vnd.custom', ['custom']);
      
  3. Performance

    • The database is loaded into memory on instantiation. For large-scale apps, ensure the MimeDb instance is reused (e.g., via a service container).
  4. Overwriting Defaults

    • addType() and addExtension() modify the instance’s state. If using a singleton, changes persist globally. Clone the instance if isolation is needed:
      $customDb = clone $mimeDb;
      $customDb->addType('...');
      

Debugging Tips

  • Verify Data: Dump the internal database to debug:
    dd($mimeDb->getAllTypes()); // Array of all registered MIME types.
    
  • Check for Typos: Use getAllExtensions() to list all supported extensions and cross-reference with your inputs.

Extension Points

  1. Custom Data Sources
    • Extend JSHttp\MimeDb\MimeDb to load from a custom file or API:
      class CustomMimeDb extends MimeDb {
          public function __construct() {
              parent::__construct();
              $this->loadFromArray($customTypesArray);
          }
      }
      
  2. Laravel Events
    • Trigger events when MIME types are added/updated:
      $mimeDb->addType('...');
      event(new MimeTypeAdded($mimeDb));
      
  3. Middleware for API Responses
    • Automatically set Content-Type headers:
      $response->header('Content-Type', $mimeDb->getMimeType($extension));
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
abbasudo/artisan-route
mirvan/ippanel
ignibyte/aic-framework
glavweb/data-schema-bundle
glavweb/datagrid-bundle
tacticmedia/scim-server
uni-method/frame
neika-ai/sdk
adachsoft/php-code-style
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package