hryvinskyi / magento2-external-media-prefetcher
N/A
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:magento2-module
Requires
- magento/framework: *
- magento/module-media-storage: 100.4.*
This package is auto-updated.
Last update: 2025-04-18 16:26:09 UTC
README
A Magento 2 module that automatically fetches missing media files from an external source.
Description
This module solves the common problem of missing media files in development or staging environments. When a media file is requested but not found locally, the module automatically attempts to download it from a configured external URL (such as your production site).
Requirements
- Magento 2.4.x
- PHP 7.4 or higher
Installation
Using Composer (recommended)
composer require hryvinskyi/magento2-external-media-prefetcher bin/magento module:enable Hryvinskyi_ExternalMediaPrefetcher bin/magento setup:upgrade
Manual Installation
- Create the following directory in your Magento installation:
app/code/Hryvinskyi/ExternalMediaPrefetcher
- Copy the module files to this directory
- Enable the module and update the database:
bin/magento module:enable Hryvinskyi_ExternalMediaPrefetcher bin/magento setup:upgrade
Configuration
Modify your staging or local environment's app/etc/env.php
file
and add this configuration under the system
key:
'system' => [ 'default' => [ 'external_media_prefetcher' => [ 'general' => [ 'enabled' => 1, 'external_media_url' => 'https://www.example.com/media/' ] ] ] ]
- external_media_prefetcher/general/enabled: Set to
1
to enable the module or0
to disable it. - external_media_prefetcher/general/external_media_url: Set the base URL where media files should be fetched from (e.g.,
https://www.example.com/media/
)
You can also set this configuration through cli commands:
bin/magento config:set external_media_prefetcher/general/enabled 1 --lock-env bin/magento config:set external_media_prefetcher/general/external_media_url https://www.example.com/media/ --lock-env
How It Works
When a media file is requested but not found locally:
- The module intercepts the request through a plugin on
Magento\MediaStorage\Model\File\Storage\Synchronization
- It cleans the path if it contains cache information
- It constructs the external URL by combining the configured external media URL with the requested file path
- It creates the necessary directory structure locally
- It downloads the file from the external source and saves it to the local filesystem
Why it's Useful
- Development and Staging: Easily sync media files from production to development or staging environments.
- Missing Files: Automatically fetch missing files without manual intervention.
- Efficiency: Saves time and effort in managing media files across different environments.
License
Author
Volodymyr Hryvinskyi
Email: volodymyr@hryvinskyi.com
GitHub: https://github.com/hryvinskyi