Load Remote Assets On-Demand from another ProcessWire Instance
Problem
You are working on a local, remote or any other copy of your live site and only have the basic files without any assets (normally saved in /site/assets/files/) - but you maybe need some of the files. This hook does this for you. In case any of your pages, either frontend or backend, need a linked file it will download a copy to your very instance of your project.
Solution
Place this in your /site/ready.php, change the domain.tld entry to your real live instance and you are already done with this enormous timesaver. Keep in mind that this only handles ProcessWire-instances and nothing like Processwire-2-Joomla or ProcessWire-2-WordPress matchings.
$wire->addHookAfter('Pagefile::url, Pagefile::filename', function($event) {
$config = $event->wire('config');
$file = $event->return;
if($event->method == 'url') {
// convert url to disk path
$file = $config->paths->root . substr($file, strlen($config->urls->root));
}
if(!file_exists($file)) {
// download file from source if it doesn't exist here
$src = 'https://domain.tld/site/assets/files/';
$url = str_replace($config->paths->files, $src, $file);
$http = new WireHttp();
$http->download($url, $file);
}
});