Skip to content

Commit

Permalink
WIP on fix images requests that require API auth using a SSR helper
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Feb 27, 2019
1 parent 1fdd20b commit 0fed529
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/components/manage/Tiles/Image/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,10 @@ export default class Edit extends Component {
<img
src={
this.props.data.url.includes(settings.apiPath)
? `${this.props.data.url}/@@images/image`
? `${this.props.data.url}/@@images/image`.replace(
settings.apiPath,
'',
)
: this.props.data.url
}
alt=""
Expand Down
2 changes: 1 addition & 1 deletion src/components/manage/Tiles/Image/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const View = ({ data }) => (
<Image
src={
data.url.startsWith(settings.apiPath)
? `${data.url}/@@images/image`
? `${data.url}/@@images/image`.replace(settings.apiPath, '')
: data.url
}
alt=""
Expand Down
4 changes: 3 additions & 1 deletion src/components/theme/View/NewsItemView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import PropTypes from 'prop-types';
import Helmet from 'react-helmet';
import { Container, Image } from 'semantic-ui-react';

import { settings } from '~/config';

/**
* NewsItemView view component class.
* @function NewsItemView
Expand All @@ -34,7 +36,7 @@ const NewsItemView = ({ content }) => (
src={
content.image['content-type'] === 'image/svg+xml'
? content.image.download
: content.image.scales.mini.download
: content.image.scales.mini.download.replace(settings.apiPath, '')
}
floated="right"
/>
Expand Down
34 changes: 34 additions & 0 deletions src/helpers/Image/ImageAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Sitemap helper.
* @module helpers/Sitemap
*/

import superagent from 'superagent';
import { map } from 'lodash';
import cookie from 'react-cookie';
import zlib from 'zlib';

import { settings } from '~/config';

/**
* Generate sitemap
* @function imageAuth
* @param {Object} req Request object
* @return {string} Generated sitemap
*/
export const imageAuth = req =>
new Promise(resolve => {
const request = superagent.get(`${settings.apiPath}/${req.path}`);
// request.set('Accept', 'application/json');
const authToken = cookie.load('auth_token');
if (authToken) {
request.set('Authorization', `Bearer ${authToken}`);
}
request.end((error, res = {}) => {
if (error) {
resolve(res || error);
} else {
resolve(res);
}
});
});
1 change: 1 addition & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
} from '@plone/volto/helpers/AuthToken/AuthToken';
export { getBaseUrl, getIcon, getView } from '@plone/volto/helpers/Url/Url';
export { generateSitemap } from '@plone/volto/helpers/Sitemap/Sitemap';
export { imageAuth } from '@plone/volto/helpers/Image/ImageAuth';
export {
nestContent,
getLayoutFieldname,
Expand Down
21 changes: 15 additions & 6 deletions src/server.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import express from 'express';
import { renderToString } from 'react-dom/server';
import { createMemoryHistory } from 'history';
import { ReduxAsyncConnect, loadOnServer } from 'redux-connect';
import { Html, Api, persistAuthToken, generateSitemap } from './helpers';
import {
Html,
Api,
persistAuthToken,
generateSitemap,
imageAuth,
} from './helpers';
import { parse as parseUrl } from 'url';
import { keys } from 'lodash';
import Raven from 'raven';
Expand Down Expand Up @@ -73,13 +79,16 @@ server

if (req.path === '/sitemap.xml.gz') {
generateSitemap(req).then(sitemap => {
res.header('Content-Type: application/x-gzip');
res.header('Content-Encoding: gzip');
res.header(
'Content-Disposition: attachment; filename="sitemap.xml.gz"',
);
res.set('Content-Type', 'application/x-gzip');
res.set('Content-Encoding', 'gzip');
res.set('Content-Disposition', 'attachment; filename="sitemap.xml.gz"');
res.send(sitemap);
});
} else if (req.path.match(/(.*)\/@@images\/(.*)/)) {
imageAuth(req).then(image => {
res.set('Content-Type', image.headers['content-type']);
res.send(image.body);
});
} else {
loadOnServer({ store, location, routes, api })
.then(() => {
Expand Down

0 comments on commit 0fed529

Please sign in to comment.