diff --git a/src/components/EntryListing/EntryListing.js b/src/components/EntryListing/EntryListing.js index 2aaac27f..60b66419 100644 --- a/src/components/EntryListing/EntryListing.js +++ b/src/components/EntryListing/EntryListing.js @@ -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 ( 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 }`); +} diff --git a/src/reducers/medias.js b/src/reducers/medias.js index 0b551c5c..65862b95 100644 --- a/src/reducers/medias.js +++ b/src/reducers/medias.js @@ -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); };