Skip to content

Commit

Permalink
Add depiction express-middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
avoinea committed Jun 17, 2021
1 parent ea063db commit d42ab02
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
[![Pipeline](https://ci.eionet.europa.eu/buildStatus/icon?job=volto-addons%2Fvolto-addon-template%2Fmaster&subject=master)](https://ci.eionet.europa.eu/view/Github/job/volto-addons/job/volto-addon-template/job/master/display/redirect)
[![Pipeline](https://ci.eionet.europa.eu/buildStatus/icon?job=volto-addons%2Fvolto-addon-template%2Fdevelop&subject=develop)](https://ci.eionet.europa.eu/view/Github/job/volto-addons/job/volto-addon-template/job/develop/display/redirect)

[Volto](https://github.com/plone/volto) add-on
[Volto](https://github.com/plone/volto) add-on: To use depiction image scales

## Features

Demo GIF
Make Volto aware of [depiction](https://github.com/eea/eea.depiction) image scales (e.g.: `image_preview`, `image_thumb`, `image_large`)

## Usage

See [volto-listing-block](https://github.com/eea/volto-listing-block)

## Getting started

Expand Down
16 changes: 16 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import imageMiddleware from './middleware/images';

const applyConfig = (config) => {
// Enable Depiction
if (config.settings.depiction === undefined) {
config.settings.depiction = '/image_preview';
}

// Depiction express-middleware
if (__SERVER__) {
const express = require('express');
config.settings.expressMiddleware = [
...(config.settings.expressMiddleware || []),
imageMiddleware(express),
];
}

return config;
};

Expand Down
27 changes: 27 additions & 0 deletions src/middleware/images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getAPIResourceWithAuth } from '@plone/volto/helpers';

const HEADERS = ['content-type', 'content-disposition', 'cache-control'];

function imageMiddleware(req, res, next) {
const { errorHandler } = req.app.locals;
getAPIResourceWithAuth(req)
.then((resource) => {
// Just forward the headers that we need
HEADERS.forEach((header) => {
if (resource.headers[header]) {
res.set(header, resource.headers[header]);
}
});

res.send(resource.body);
}, errorHandler)
.catch(errorHandler);
}

export default function (express) {
const middleware = express.Router();

middleware.all(['**/image_[a-z]+$'], imageMiddleware);
middleware.id = 'depictionResourcesProcessor';
return middleware;
}

0 comments on commit d42ab02

Please sign in to comment.