Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

content management system - Make Media Directory of a CMS available on local development, with fallback to production data (for example Magento 2 public/media)

When developing web shops, we do not want to copy all media assets to the local development machine.

Still it would be nice, to get a real view on the system, i.e. load the media images on demand from the production server.

How can this be achieved?

question from:https://stackoverflow.com/questions/65869307/make-media-directory-of-a-cms-available-on-local-development-with-fallback-to-p

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For Magento 2 there is a open source module which does exactly this.

If you are on Linux, a more generic solution seems to be possible, using http and union fuse file systems. This could work on different Shop- and CMS systems such as Shopware, TYPO3 and so on.

The following example was tested on Ubuntu 20.04

First install the required packages

apt install httpdirfs unionfs-fuse

We assume, the production server uses Apache. In the next step we enable directory listing, but only for a certain - secret - user agent. Make sure to replace "ThisIsSecret" by your secret.

<If "%{HTTP_USER_AGENT} == 'ThisIsSecret'">
  Options +Indexes +MultiViews
  AllowOverride None
</If>
<Else>
  Options -Indexes -MultiViews +FollowSymLinks
  AllowOverride All
</Else>

Go to your local development directory and move the current media directory:

cd /home/me/projects/example.com/public
mv media media_local

Now httpdirfs is used to mount the server's directory via HTTP. httpdirfs parses the directory listing and lets you see the remote files. Editing is not possible.

mkdir media_remote
httpdirfs --cache --user-agent ThisIsSecret https://example.com/media/ media_remote

Next, we use unionfs with the CopyOnWrite (cow) option to be able to still write to the media folder.

mkdir media
unionfs-fuse -o cow media_local=RW:media_remote=RO media

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...