- How do I integrate this package with Laravel’s Storage facade (e.g., `Storage::put()`)?
- The package doesn’t natively support Laravel’s Filesystem contracts, so you’ll need to wrap the `DataUriInterface` in a custom adapter. For example, create a class that implements Laravel’s `FilesystemAdapter` and delegates reads/writes to the decoded data URI object. This allows seamless use with `Storage::disk()->put()`.
- Does this package support parsing data URIs from API responses (e.g., base64-encoded images in JSON)?
- Yes, the `DataDecoder::decode()` method handles data URIs (RFC 2397) directly, including base64-encoded payloads from APIs. Just pass the URI string (e.g., `data:image/png;base64,...`) to the decoder, and it will create a temporary file with the correct MIME type and auto-delete when done.
- What Laravel versions and PHP versions are officially supported?
- The package requires **PHP 8.1+** and is compatible with **Laravel 10+**. It has no hard dependencies on Laravel itself, so it should work in newer versions as long as PHP 8.1+ is maintained. Always check the [GitHub repo](https://github.com/1tomany/data-uri) for updates.
- How does the package handle large files (e.g., 200MB videos) without memory issues?
- The library uses PHP’s `stream_get_contents()` under the hood, which streams data in chunks rather than loading it entirely into memory. However, PHP’s `memory_limit` and `upload_max_filesize` settings may still constrain processing. For very large files, consider chunked processing manually or increasing PHP’s limits.
- Can I override the auto-detected MIME type for a data URI?
- Yes, the `decode()` method accepts a `$type` parameter where you can explicitly set the MIME type (e.g., `text/markdown` instead of `text/plain`). This is useful when auto-detection (via `mime_content_type()`) is unreliable or when you need to enforce specific types for consistency.
- What happens if the data URI is malformed or invalid? Does it throw exceptions?
- The package throws exceptions for invalid inputs (e.g., malformed base64, non-existent URLs). You’ll need to catch these (e.g., `InvalidDataUriException`) and handle them in your application. For Laravel, you could wrap the decoder in a custom service provider or middleware to standardize error handling.
- Is there a way to manually delete the temporary file before the object is destroyed?
- No, the package doesn’t expose a `delete()` method on `DataUriInterface`. The temporary file is **only** deleted when the object is destructed or garbage collected. For edge cases (e.g., long-running queue jobs), consider storing the temp file path and deleting it manually via `unlink()` when no longer needed.
- Does this package validate URLs for security (e.g., prevent SSRF or non-HTTPS requests)?
- No, the package doesn’t validate URLs beyond basic stream accessibility. For security, use Laravel’s `Validator` or middleware to enforce HTTPS, reject untrusted domains, or integrate with Guzzle for advanced URL validation before passing data to the decoder.
- How can I preserve the original filename from a data URI (e.g., for user uploads)?
- Use the `$name` parameter in `decode()` to specify the original filename. If the data URI includes a filename (e.g., `data:application/pdf;name=report.pdf;base64,...`), the package will auto-detect it. Otherwise, pass the desired name explicitly to retain it in Laravel’s storage system.
- Are there alternatives to this package for Laravel that offer similar functionality?
- For data URI parsing, alternatives include `spatie/data-transfer-object` (for structured data) or custom solutions using `file_get_contents()` and `base64_decode()`. However, this package stands out for its **immutable value object pattern**, **auto-deletion**, and **streaming efficiency**, which are rare in Laravel-focused libraries. For URL/file handling, Laravel’s built-in `Storage` facade may suffice if you don’t need data URI support.