Added resolvePath helper to normalize and prepend the public_path and as needed

This commit is contained in:
Cássio Zen 2016-11-17 15:33:02 -02:00
parent 8d9f894928
commit 695f533c8e
3 changed files with 27 additions and 8 deletions

View File

@ -3,6 +3,7 @@ import ImmutablePropTypes from 'react-immutable-proptypes';
import Waypoint from 'react-waypoint';
import { Map } from 'immutable';
import history from '../../routing/history';
import { resolvePath } from '../../lib/pathHelper';
import { selectFields, selectInferedField } from '../../reducers/collections';
import { Card } from '../UI';
import styles from './EntryListing.css';
@ -39,9 +40,7 @@ export default class EntryListing extends React.Component {
const label = entry.get('label');
const title = label || entry.getIn(['data', inferedFields.titleField]);
let image = entry.getIn(['data', inferedFields.imageField]);
if (image && image.indexOf('/') === -1) {
image = `/${ publicFolder }/${ image }`;
}
image = resolvePath(image, publicFolder);
return (
<Card
key={entry.get('slug')}

18
src/lib/pathHelper.js Normal file
View File

@ -0,0 +1,18 @@
const absolutePath = new RegExp('^(?:[a-z]+:)?//', 'i');
const normalizePath = path => path.replace(/[\\\/]+/g, '/');
export function resolvePath(path, basePath) { // eslint-disable-line
// No path provided, skip
if (!path) return null;
// It's an absolute path.
if (absolutePath.test(path)) return normalizePath(path);
if (path.indexOf('/') === -1) {
// It's a single file name, no directories. Prepend public folder
return normalizePath(`/${ basePath }/${ path }`);
}
// It's a relative path. Prepend a forward slash.
return normalizePath(`/${ path }`);
}

View File

@ -1,4 +1,5 @@
import { Map } from 'immutable';
import { resolvePath } from '../lib/pathHelper';
import { ADD_MEDIA, REMOVE_MEDIA } from '../actions/media';
import MediaProxy from '../valueObjects/MediaProxy';
@ -17,13 +18,14 @@ const medias = (state = Map(), action) => {
export default medias;
export const getMedia = (publicFolder, state, path) => {
// No path provided, skip
if (!path) return null;
if (state.has(path)) {
// There is already a MediaProxy in memmory for this path. Use it.
return state.get(path);
}
let localPath = path;
if (path && path.indexOf('/') === -1) {
localPath = `/${ publicFolder }/${ localPath }`;
}
return new MediaProxy(localPath, null, true);
// Create a new MediaProxy (for consistency) and return it.
return new MediaProxy(resolvePath(path, publicFolder), null, true);
};