Skip to content

Commit 731a9cd

Browse files
fix: make asset paths relative
closes #76
1 parent c639528 commit 731a9cd

File tree

13 files changed

+142
-47
lines changed

13 files changed

+142
-47
lines changed

public/app/config/channels.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ module.exports = {
1717
RESPOND_LOAD_SPACE_PROMPT_CHANNEL: 'prompt:space:load:response',
1818
RESPOND_EXPORT_SPACE_PROMPT_CHANNEL: 'prompt:space:export:respond',
1919
RESPOND_DELETE_SPACE_PROMPT_CHANNEL: 'prompt:space:delete:respond',
20+
GET_USER_FOLDER_CHANNEL: 'user:folder:get',
2021
};

public/electron.js

+26-15
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const {
5252
SHOW_LOAD_SPACE_PROMPT_CHANNEL,
5353
RESPOND_LOAD_SPACE_PROMPT_CHANNEL,
5454
SAVE_SPACE_CHANNEL,
55+
GET_USER_FOLDER_CHANNEL,
5556
} = require('./app/config/channels');
5657
const {
5758
ERROR_SPACE_ALREADY_AVAILABLE,
@@ -273,7 +274,7 @@ app.on('ready', async () => {
273274

274275
const { phases, image } = spaceToSave;
275276

276-
const spacePath = `${VAR_FOLDER}/${id}`;
277+
const spacePath = id;
277278

278279
// todo: follow new format
279280
// if there is a background/thumbnail image, save it
@@ -292,18 +293,18 @@ app.on('ready', async () => {
292293
const hash = generateHash({ url });
293294
const imageFileName = `${hash}.${ext}`;
294295
const imagePath = `${spacePath}/${imageFileName}`;
296+
const absoluteSpacePath = `${VAR_FOLDER}/${spacePath}`;
297+
const absoluteImagePath = `${VAR_FOLDER}/${imagePath}`;
295298
// eslint-disable-next-line no-await-in-loop
296-
const imageAvailable = await isFileAvailable(imagePath);
297-
if (imageAvailable) {
298-
spaceToSave.image[key] = imagePath;
299-
} else {
299+
const imageAvailable = await isFileAvailable(absoluteImagePath);
300+
if (!imageAvailable) {
300301
// eslint-disable-next-line no-await-in-loop
301-
const imageDl = await download(mainWindow, url, {
302-
directory: spacePath,
302+
await download(mainWindow, url, {
303+
directory: absoluteSpacePath,
303304
filename: imageFileName,
304305
});
305-
spaceToSave.image[key] = imageDl.getSavePath();
306306
}
307+
spaceToSave.image[key] = imagePath;
307308
}
308309
}
309310
}
@@ -325,22 +326,22 @@ app.on('ready', async () => {
325326
const ext = getExtension(resource);
326327
const fileName = `${hash}.${ext}`;
327328
const filePath = `${spacePath}/${fileName}`;
329+
const absoluteSpacePath = `${VAR_FOLDER}/${spacePath}`;
330+
const absoluteFilePath = `${VAR_FOLDER}/${filePath}`;
328331
phase.items[i].hash = hash;
329332

330333
// eslint-disable-next-line no-await-in-loop
331-
const fileAvailable = await isFileAvailable(filePath);
334+
const fileAvailable = await isFileAvailable(absoluteFilePath);
332335

333336
// if the file is available, point this resource to its path
334-
if (fileAvailable) {
335-
phase.items[i].asset = filePath;
336-
} else {
337+
if (!fileAvailable) {
337338
// eslint-disable-next-line no-await-in-loop
338-
const dl = await download(mainWindow, url, {
339-
directory: spacePath,
339+
await download(mainWindow, url, {
340+
directory: absoluteSpacePath,
340341
filename: fileName,
341342
});
342-
phase.items[i].asset = dl.getSavePath();
343343
}
344+
phase.items[i].asset = filePath;
344345
}
345346
}
346347
}
@@ -557,6 +558,16 @@ app.on('ready', async () => {
557558
mainWindow.webContents.send(RESPOND_DELETE_SPACE_PROMPT_CHANNEL, respond);
558559
});
559560
});
561+
562+
// called when getting user folder
563+
ipcMain.on(GET_USER_FOLDER_CHANNEL, () => {
564+
try {
565+
mainWindow.webContents.send(GET_USER_FOLDER_CHANNEL, VAR_FOLDER);
566+
} catch (e) {
567+
logger.error(e);
568+
mainWindow.webContents.send(GET_USER_FOLDER_CHANNEL, ERROR_GENERAL);
569+
}
570+
});
560571
});
561572

562573
app.on('window-all-closed', () => {

src/App.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
VISIT_PATH,
1919
LOAD_SPACE_PATH,
2020
} from './config/paths';
21-
import { getGeolocation } from './actions/user';
21+
import { getGeolocation, getUserFolder } from './actions/user';
2222

2323
const theme = createMuiTheme({
2424
typography: {
@@ -38,8 +38,15 @@ export class App extends Component {
3838

3939
static propTypes = {
4040
dispatchGetGeolocation: PropTypes.func.isRequired,
41+
dispatchGetUserFolder: PropTypes.func.isRequired,
4142
};
4243

44+
constructor(props) {
45+
super(props);
46+
const { dispatchGetUserFolder } = this.props;
47+
dispatchGetUserFolder();
48+
}
49+
4350
componentDidMount() {
4451
const { dispatchGetGeolocation } = this.props;
4552
dispatchGetGeolocation();
@@ -85,6 +92,7 @@ export class App extends Component {
8592

8693
const mapDispatchToProps = {
8794
dispatchGetGeolocation: getGeolocation,
95+
dispatchGetUserFolder: getUserFolder,
8896
};
8997

9098
export default connect(

src/App.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('<App />', () => {
1010
},
1111
t: jest.fn(),
1212
dispatchGetGeolocation: jest.fn(),
13+
dispatchGetUserFolder: jest.fn(),
1314
};
1415
const component = shallow(<App {...props} />);
1516
it('renders correctly', () => {

src/actions/user/index.js

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import { toastr } from 'react-redux-toastr';
22
import { getCurrentPosition } from '../../utils/geolocation';
3-
import { GET_GEOLOCATION_SUCCEEDED } from '../../types';
3+
import {
4+
GET_GEOLOCATION_SUCCEEDED,
5+
GET_USER_FOLDER_SUCCEEDED,
6+
FLAG_GETTING_USER_FOLDER,
7+
} from '../../types';
48
import {
59
ERROR_GETTING_GEOLOCATION,
10+
ERROR_GETTING_USER_FOLDER,
611
ERROR_MESSAGE_HEADER,
712
} from '../../config/messages';
13+
import { GET_USER_FOLDER_CHANNEL } from '../../config/channels';
14+
import { createFlag } from '../common';
15+
import { ERROR_GENERAL } from '../../config/errors';
16+
17+
const flagGettingUserFolder = createFlag(FLAG_GETTING_USER_FOLDER);
818

919
const getGeolocation = async () => async dispatch => {
1020
// only fetch location if online
@@ -34,7 +44,25 @@ const getGeolocation = async () => async dispatch => {
3444
}
3545
};
3646

37-
export {
38-
// eslint-disable-next-line import/prefer-default-export
39-
getGeolocation,
47+
const getUserFolder = async () => dispatch => {
48+
try {
49+
dispatch(flagGettingUserFolder(true));
50+
window.ipcRenderer.send(GET_USER_FOLDER_CHANNEL);
51+
window.ipcRenderer.once(GET_USER_FOLDER_CHANNEL, (event, folder) => {
52+
if (folder === ERROR_GENERAL) {
53+
toastr.error(ERROR_MESSAGE_HEADER, ERROR_GETTING_USER_FOLDER);
54+
} else {
55+
dispatch({
56+
type: GET_USER_FOLDER_SUCCEEDED,
57+
payload: folder,
58+
});
59+
}
60+
dispatch(flagGettingUserFolder(false));
61+
});
62+
} catch (e) {
63+
console.error(e);
64+
toastr.error(ERROR_MESSAGE_HEADER, ERROR_GETTING_USER_FOLDER);
65+
}
4066
};
67+
68+
export { getUserFolder, getGeolocation };

src/components/phase/PhaseApp.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3+
import { connect } from 'react-redux';
34
import './PhaseApp.css';
45

5-
const PhaseApp = ({ url, asset, name }) => {
6+
const PhaseApp = ({ url, asset, name, folder }) => {
67
let uri = url;
78
if (asset) {
8-
uri = `file://${asset}`;
9+
uri = `file://${folder}/${asset}`;
910
}
1011
return (
1112
// state = {
@@ -58,6 +59,7 @@ PhaseApp.propTypes = {
5859
url: PropTypes.string,
5960
asset: PropTypes.string,
6061
name: PropTypes.string,
62+
folder: PropTypes.string.isRequired,
6163
};
6264

6365
PhaseApp.defaultProps = {
@@ -66,4 +68,10 @@ PhaseApp.defaultProps = {
6668
name: 'Image',
6769
};
6870

69-
export default PhaseApp;
71+
const mapStateToProps = ({ User }) => ({
72+
folder: User.getIn(['current', 'folder']),
73+
});
74+
75+
const ConnectedComponent = connect(mapStateToProps)(PhaseApp);
76+
77+
export default ConnectedComponent;

src/components/phase/PhaseImage.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3+
import { connect } from 'react-redux';
34
import './PhaseImage.css';
45

5-
const PhaseImage = ({ url, asset, name }) => {
6+
const PhaseImage = ({ url, asset, name, folder }) => {
67
let uri = url;
78
if (asset) {
8-
uri = `file://${asset}`;
9+
uri = `file://${folder}/${asset}`;
910
}
1011
return (
1112
<div className="ImageDiv">
@@ -18,6 +19,7 @@ PhaseImage.propTypes = {
1819
url: PropTypes.string,
1920
asset: PropTypes.string,
2021
name: PropTypes.string,
22+
folder: PropTypes.string.isRequired,
2123
};
2224

2325
PhaseImage.defaultProps = {
@@ -26,4 +28,10 @@ PhaseImage.defaultProps = {
2628
name: 'Image',
2729
};
2830

29-
export default PhaseImage;
31+
const mapStateToProps = ({ User }) => ({
32+
folder: User.getIn(['current', 'folder']),
33+
});
34+
35+
const ConnectedComponent = connect(mapStateToProps)(PhaseImage);
36+
37+
export default ConnectedComponent;

src/components/phase/PhaseVideo.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/* eslint-disable jsx-a11y/media-has-caption */
22
import React from 'react';
33
import PropTypes from 'prop-types';
4+
import { connect } from 'react-redux';
45
import './PhaseVideo.css';
56

6-
const PhaseVideo = ({ url, asset, name, mimeType }) => {
7+
const PhaseVideo = ({ url, asset, name, mimeType, folder }) => {
78
let uri = url;
89
if (asset) {
9-
uri = `file://${asset}`;
10+
uri = `file://${folder}/${asset}`;
1011
}
1112
return (
1213
<video title={name} className="Video" controls>
@@ -20,6 +21,7 @@ PhaseVideo.propTypes = {
2021
asset: PropTypes.string,
2122
name: PropTypes.string,
2223
mimeType: PropTypes.string,
24+
folder: PropTypes.string.isRequired,
2325
};
2426

2527
PhaseVideo.defaultProps = {
@@ -29,4 +31,10 @@ PhaseVideo.defaultProps = {
2931
mimeType: 'video/mp4',
3032
};
3133

32-
export default PhaseVideo;
34+
const mapStateToProps = ({ User }) => ({
35+
folder: User.getIn(['current', 'folder']),
36+
});
37+
38+
const ConnectedComponent = connect(mapStateToProps)(PhaseVideo);
39+
40+
export default ConnectedComponent;

src/components/space/SpaceGrid.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ import { clearSpace } from '../../actions';
1313
import DefaultThumbnail from '../../data/graasp.jpg';
1414

1515
class SpaceGrid extends Component {
16+
static propTypes = {
17+
folder: PropTypes.string.isRequired,
18+
classes: PropTypes.shape({ appBar: PropTypes.string.isRequired })
19+
.isRequired,
20+
spaces: PropTypes.instanceOf(Set).isRequired,
21+
history: PropTypes.shape({ length: PropTypes.number.isRequired })
22+
.isRequired,
23+
dispatchClearSpace: PropTypes.func.isRequired,
24+
};
25+
1626
componentDidMount() {
1727
const { dispatchClearSpace } = this.props;
1828
dispatchClearSpace();
@@ -22,6 +32,7 @@ class SpaceGrid extends Component {
2232
// the image from url if provided if not provided then pass
2333
// the default background image
2434
generateThumbnail = ({ image }) => {
35+
const { folder } = this.props;
2536
const {
2637
backgroundUrl,
2738
thumbnailUrl,
@@ -31,10 +42,10 @@ class SpaceGrid extends Component {
3142

3243
// prioritise assets
3344
if (thumbnailAsset) {
34-
return `file://${thumbnailAsset}`;
45+
return `file://${folder}/${thumbnailAsset}`;
3546
}
3647
if (backgroundAsset) {
37-
return `file://${backgroundAsset}`;
48+
return `file://${folder}/${backgroundAsset}`;
3849
}
3950

4051
// fallback on urls
@@ -51,6 +62,7 @@ class SpaceGrid extends Component {
5162

5263
render() {
5364
const { spaces, classes, history } = this.props;
65+
5466
const MediaCards = spaces.map(space => {
5567
const { id, name, image = {}, text } = space;
5668
const { replace } = history;
@@ -96,22 +108,17 @@ class SpaceGrid extends Component {
96108
}
97109
}
98110

111+
const mapStateToProps = ({ User }) => ({
112+
folder: User.getIn(['current', 'folder']),
113+
});
114+
99115
const mapDispatchToProps = {
100116
dispatchClearSpace: clearSpace,
101117
};
102118

103-
SpaceGrid.propTypes = {
104-
classes: PropTypes.shape({ appBar: PropTypes.string.isRequired }).isRequired,
105-
spaces: PropTypes.instanceOf(Set).isRequired,
106-
history: PropTypes.shape({ length: PropTypes.number.isRequired }).isRequired,
107-
dispatchClearSpace: PropTypes.func.isRequired,
108-
};
119+
const ConnectedComponent = connect(
120+
mapStateToProps,
121+
mapDispatchToProps
122+
)(SpaceGrid);
109123

110-
export default withRouter(
111-
withStyles({})(
112-
connect(
113-
null,
114-
mapDispatchToProps
115-
)(SpaceGrid)
116-
)
117-
);
124+
export default withRouter(withStyles({})(ConnectedComponent));

src/config/channels.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ module.exports = {
1717
RESPOND_LOAD_SPACE_PROMPT_CHANNEL: 'prompt:space:load:response',
1818
RESPOND_EXPORT_SPACE_PROMPT_CHANNEL: 'prompt:space:export:respond',
1919
RESPOND_DELETE_SPACE_PROMPT_CHANNEL: 'prompt:space:delete:respond',
20+
GET_USER_FOLDER_CHANNEL: 'user:folder:get',
2021
};

src/config/messages.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const ERROR_GETTING_GEOLOCATION =
2424
'There was an error getting your current location';
2525
const ERROR_GETTING_SPACES_NEARBY =
2626
'There was an error getting the spaces nearby';
27+
const ERROR_GETTING_USER_FOLDER =
28+
'There was an error getting your user folder.';
2729

2830
module.exports = {
2931
ERROR_DOWNLOADING_MESSAGE,
@@ -45,4 +47,5 @@ module.exports = {
4547
ERROR_LOADING_MESSAGE,
4648
ERROR_GETTING_GEOLOCATION,
4749
ERROR_GETTING_SPACES_NEARBY,
50+
ERROR_GETTING_USER_FOLDER,
4851
};

0 commit comments

Comments
 (0)