
We, in the XIAG company, in different projects constantly solve the same problem: how to store and display binary user data. These can be company logos, PDF files with job descriptions or a welcome video. Moreover, this content should be shown on the website pages in the most diverse way: the logo should be the right size, PDF - in the form of a small preview image, and from the video you need to show a couple of still frames.
I am sure that this task is familiar to all web developers. In this article we want to share our solution to the problem, published under an open source license for GitHub.
Meet the Barberry web service (
https://github.com/Magomogo/Barberry ), which we have been successfully using for about a year. The essence of the service is that it stores the originals of the loaded documents and is able to give them differently, converting on the fly.
Web service with REST interface
The first and obvious idea - the solution of the standard problem should not be part of a specific application. We need an isolated service that can be assigned this simple responsibility.
')
Barberry is hosted on the server separately and communicates with the world via HTTP. To try the service at work, you must install
https://github.com/Magomogo/barberry-service on your server. We have already done this in our country, a demonstration of the service can be seen at
http://barberry.xiag.chLoading
In order to upload data to Barberry, you must make a POST request. The service will determine the Content-Type and report the unique identifier of the saved document.
Example of loading a photo DSC01823.JPG, for example in the console with the curl command:
$ curl -X POST -F file=@/tmp/DSC01823.JPG barberry.xiag.ch
: POST / HTTP/1.1 -- -- : HTTP/1.1 201 Created {"id":"yPkjPk","contentType":"image/jpeg","ext":"jpg","length":218038,"filename":"DSC01823.JPG"}
JSON server response describes the received document. Further reference to it will occur by a unique “id”: ”yPkjPk”.
Unloading
The identifier of the loaded document must be saved on the side of your application. It is used for unloading:
: GET /yPkjPk.jpg HTTP/1.1 : HTTP/1.1 200 OK Content-Type: image/jpeg
An image will appear in the browser window. Nothing special. The whole power of Barberry is revealed further.
On-the-fly conversion and caching
What happens if you ask for a document in a different format? To do this, you need to add another extension to the document identifier.
: GET /yPkjPk.gif HTTP/1.1 : HTTP/1.1 200 OK Content-Type: image/gif
The image will change, the service has converted JPG -> GIF, and transferred to us. Barberry-plugin-imagemagick was used for the conversion, which takes over the image conversion.
The request may be even more complicated:
: GET /yPkjPk_200x200.gif HTTP/1.1
It will change not only the format of the original image, but also the size. In general, after the document identifier there is an arbitrary command that will be passed to the plugin, which will convert to the desired format.
If you ask for a strange one, the service will answer correctly:
: GET /yPkjPk.pdf HTTP/1.1 : HTTP/1.1 404 Not Found
This means that the service did not find an opportunity to convert the image into a PDF document. Of course, no one forbids to write a plugin for this, and connect Barberry to your installation.
About caching. Conversion does not occur with every GET request. The web server is configured to start the converter only if the necessary file was not found in the cache. The example uses apache with its rewrite module.
Flexibility
Despite the fact that the task is standard, there are some nuances for different projects. The Barberry architecture allows you to create specific plug-ins, expanding the possibilities for data conversion. For example, instead of the picture-handling plug-in
github.com/Magomogo/barberry-plugin-imagemagick, you can implement your own by adding a watermark of your site to the image.
So Barberry had rather exotic abilities:
Dependency management is given to the composer package manager. To create a barberry service for your application, we recommend you to fork
https://github.com/Magomogo/barberry-service and add your own specifics.
For example, look in composer.json and connect the plugins you need. Initially, only the plugin for working with images is connected:
"require": { "barberry/barberry": "1.1.*", "barberry/plugin-imagemagick": "1.0.*" }
To extend the functionality, you can connect plugins that will work with documents of other Content-Type.
Monitoring
Like any web application, Barberry in productive mode must be connected to the monitoring system. At the address
barberry.xiag.ch/monitoring.php is a script that polls all connected plugins and reports whether everything is in order.
Conclusion
The description of the implementation details is beyond the scope of this post. Those interested are invited to cooperate on the pages of GitHub.