Skip to content

Commit

Permalink
Merge pull request #317 from graasp/242/postFile
Browse files Browse the repository at this point in the history
feat: save file locally when receiving postFile message
  • Loading branch information
juancarlosfarah authored Sep 4, 2020
2 parents ba4627a + 5a7627a commit a3a7851
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"enzyme": "3.11.0",
"enzyme-adapter-react-16": "1.15.2",
"extract-zip": "1.6.7",
"fs-extra": "9.0.1",
"history": "4.10.1",
"i18next": "19.3.2",
"immutable": "4.0.0-rc.12",
Expand Down
1 change: 1 addition & 0 deletions public/app/config/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ module.exports = {
GET_SPACE_IN_CLASSROOM_CHANNEL: 'classroom:space:get',
LOAD_SPACE_IN_CLASSROOM_CHANNEL: 'classroom:space:load',
GET_SPACE_TO_LOAD_IN_CLASSROOM_CHANNEL: 'classroom:space:load:get-space',
POST_FILE_CHANNEL: 'file:post',
};
8 changes: 8 additions & 0 deletions public/app/config/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// eslint-disable-next-line import/no-extraneous-dependencies
const { app } = require('electron');
const ObjectId = require('bson-objectid');
const isWindows = require('../utils/isWindows');

// types that we support downloading
Expand Down Expand Up @@ -84,6 +85,12 @@ const ACTIONS_VERBS = {
LOGOUT: 'logout',
};

const buildFilePath = ({ userId, spaceId, name }) => {
// add generated id to handle duplicate files
const generatedId = ObjectId().str;
return `${VAR_FOLDER}/${spaceId}/files/${userId}/${generatedId}_${name}`;
};

module.exports = {
DEFAULT_LOGGING_LEVEL,
DEFAULT_PROTOCOL,
Expand All @@ -110,4 +117,5 @@ module.exports = {
VISIBILITIES,
DEFAULT_FORMAT,
ACTIONS_VERBS,
buildFilePath,
};
2 changes: 2 additions & 0 deletions public/app/config/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const ERROR_GETTING_SPACE_IN_CLASSROOM_MESSAGE =
const ERROR_SETTING_ACTION_ACCESSIBILITY =
'There was an error setting the action accessibility';
const ERROR_SETTING_ACTIONS_AS_ENABLED = 'There was an error enabling actions';
const ERROR_POSTING_FILE_MESSAGE = 'There was an error uploading the file';

module.exports = {
ERROR_GETTING_DEVELOPER_MODE,
Expand Down Expand Up @@ -172,4 +173,5 @@ module.exports = {
ERROR_INVALID_USERNAME_MESSAGE,
ERROR_SETTING_ACTION_ACCESSIBILITY,
ERROR_SETTING_ACTIONS_AS_ENABLED,
ERROR_POSTING_FILE_MESSAGE,
};
2 changes: 2 additions & 0 deletions public/app/listeners/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const loadSpaceInClassroom = require('./loadSpaceInClassroom');
const setActionAccessibility = require('./setActionAccessibility');
const setActionsAsEnabled = require('./setActionsAsEnabled');
const windowAllClosed = require('./windowAllClosed');
const postFile = require('./postFile');

module.exports = {
loadSpace,
Expand Down Expand Up @@ -103,4 +104,5 @@ module.exports = {
setActionAccessibility,
setActionsAsEnabled,
windowAllClosed,
postFile,
};
31 changes: 31 additions & 0 deletions public/app/listeners/postFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fse = require('fs-extra');
const path = require('path');
const { POST_FILE_CHANNEL } = require('../config/channels');
const { buildFilePath } = require('../config/config');
const logger = require('../logger');
const { ERROR_GENERAL } = require('../config/errors');

const postFile = mainWindow => (event, payload = {}) => {
const { userId, spaceId, data } = payload;
// download file given path
const { path: filepath, name } = data;
const savePath = buildFilePath({ userId, spaceId, name });
const dirPath = path.dirname(savePath);
try {
fse.ensureDirSync(dirPath);
fse.copySync(filepath, path.resolve(savePath));
logger.debug(`the file ${name} was uploaded`);

// update data
const newData = { name, uri: `file://${savePath}` };
const newPayload = { ...payload, data: newData };

// send back the resource
mainWindow.webContents.send(POST_FILE_CHANNEL, newPayload);
} catch (e) {
logger.error(e);
mainWindow.webContents.send(POST_FILE_CHANNEL, ERROR_GENERAL);
}
};

module.exports = postFile;
6 changes: 6 additions & 0 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const {
LOAD_SPACE_IN_CLASSROOM_CHANNEL,
SET_ACTION_ACCESSIBILITY_CHANNEL,
SET_ACTIONS_AS_ENABLED_CHANNEL,
POST_FILE_CHANNEL,
} = require('./app/config/channels');
const env = require('./env.json');
const {
Expand Down Expand Up @@ -127,6 +128,7 @@ const {
setActionAccessibility,
setActionsAsEnabled,
windowAllClosed,
postFile,
} = require('./app/listeners');
const isMac = require('./app/utils/isMac');

Expand Down Expand Up @@ -488,6 +490,10 @@ app.on('ready', async () => {

// called when creating an action
ipcMain.on(POST_ACTION_CHANNEL, postAction(mainWindow, db));

// called when creating a file
ipcMain.on(POST_FILE_CHANNEL, postFile(mainWindow, db));

// called when logging in a user
ipcMain.on(SIGN_IN_CHANNEL, signIn(mainWindow, db));

Expand Down
45 changes: 45 additions & 0 deletions src/actions/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { POST_FILE_CHANNEL } from '../config/channels';
import { POST_FILE_SUCCEEDED, POST_FILE_FAILED } from '../types';
import { ERROR_GENERAL } from '../config/errors';
import { ERROR_POSTING_FILE_MESSAGE } from '../config/messages';

// eslint-disable-next-line import/prefer-default-export
export const postFile = async (
{ userId, appInstanceId, spaceId, subSpaceId, format, data, type } = {},
callback
) => {
try {
window.ipcRenderer.send(POST_FILE_CHANNEL, {
userId,
appInstanceId,
spaceId,
subSpaceId,
format,
type,
data,
});

window.ipcRenderer.once(POST_FILE_CHANNEL, async (event, response) => {
if (response === ERROR_GENERAL) {
callback({
appInstanceId,
type: POST_FILE_FAILED,
payload: ERROR_POSTING_FILE_MESSAGE,
});
} else {
callback({
// have to include the appInstanceId to avoid broadcasting
appInstanceId,
type: POST_FILE_SUCCEEDED,
payload: response,
});
}
});
} catch (err) {
callback({
appInstanceId,
type: POST_FILE_FAILED,
payload: ERROR_POSTING_FILE_MESSAGE,
});
}
};
1 change: 1 addition & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './syncSpace';
export * from './loadSpace';
export * from './exportSpace';
export * from './classroom';
export * from './file';
17 changes: 16 additions & 1 deletion src/components/phase/PhaseApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ import {
APP_INSTANCE_RESOURCE_TYPES,
POST_ACTION,
ACTION_TYPES,
POST_FILE,
FILE_TYPES,
} from '../../types';
import {
getAppInstanceResources,
patchAppInstanceResource,
postAppInstanceResource,
getAppInstance,
postAction,
postFile,
} from '../../actions';
import {
DEFAULT_LANGUAGE,
Expand Down Expand Up @@ -125,7 +128,13 @@ class PhaseApp extends Component {
const { id: componentAppInstanceId } = appInstance || {};
const { type, payload } = JSON.parse(event.data);
let { id: messageAppInstanceId } = payload;
if ([...APP_INSTANCE_RESOURCE_TYPES, ...ACTION_TYPES].includes(type)) {
if (
[
...APP_INSTANCE_RESOURCE_TYPES,
...ACTION_TYPES,
...FILE_TYPES,
].includes(type)
) {
({ appInstanceId: messageAppInstanceId } = payload);
}

Expand Down Expand Up @@ -153,6 +162,12 @@ class PhaseApp extends Component {
}
break;
}
case POST_FILE: {
if (isSpaceSaved) {
return postFile(payload, this.postMessage);
}
break;
}
default:
return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/config/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ module.exports = {
GET_SPACE_IN_CLASSROOM_CHANNEL: 'classroom:space:get',
LOAD_SPACE_IN_CLASSROOM_CHANNEL: 'classroom:space:load',
GET_SPACE_TO_LOAD_IN_CLASSROOM_CHANNEL: 'classroom:space:load:get-space',
POST_FILE_CHANNEL: 'file:post',
};
2 changes: 2 additions & 0 deletions src/config/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const ERROR_INVALID_USERNAME_MESSAGE = 'This username is invalid';
const ERROR_SETTING_ACTION_ACCESSIBILITY =
'There was an error setting the action accessibility';
const ERROR_SETTING_ACTIONS_AS_ENABLED = 'There was an error enabling actions';
const ERROR_POSTING_FILE_MESSAGE = 'There was an error uploading the file';

module.exports = {
ERROR_GETTING_DEVELOPER_MODE,
Expand Down Expand Up @@ -172,4 +173,5 @@ module.exports = {
ERROR_INVALID_USERNAME_MESSAGE,
ERROR_SETTING_ACTION_ACCESSIBILITY,
ERROR_SETTING_ACTIONS_AS_ENABLED,
ERROR_POSTING_FILE_MESSAGE,
};
5 changes: 5 additions & 0 deletions src/types/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const POST_FILE = 'POST_FILE';
export const POST_FILE_SUCCEEDED = 'POST_FILE_SUCCEEDED';
export const POST_FILE_FAILED = 'POST_FILE_FAILED';

export const FILE_TYPES = [POST_FILE, POST_FILE_SUCCEEDED, POST_FILE_FAILED];
1 change: 1 addition & 0 deletions src/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './syncSpace';
export * from './loadSpace';
export * from './exportSpace';
export * from './classroom';
export * from './file';
29 changes: 29 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2789,6 +2789,11 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha1-x57Zf380y48robyXkLzDZkdLS3k=

at-least-node@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/at-least-node/-/at-least-node-1.0.0.tgz#602cd4b46e844ad4effc92a8011a3c46e0238dc2"
integrity sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==

atob@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
Expand Down Expand Up @@ -6950,6 +6955,16 @@ fs-extra-p@^4.6.1:
bluebird-lst "^1.0.5"
fs-extra "^6.0.1"

[email protected]:
version "9.0.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.0.1.tgz#910da0062437ba4c39fedd863f1675ccfefcb9fc"
integrity sha512-h2iAoN838FqAFJY2/qVpzFXy+EBxfVE220PalAqQLDVsFOHLJrZvut5puAbCdNv6WJk+B8ihI+k0c7JK5erwqQ==
dependencies:
at-least-node "^1.0.0"
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^1.0.0"

fs-extra@^4.0.1, fs-extra@^4.0.2:
version "4.0.3"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-4.0.3.tgz#0d852122e5bc5beb453fb028e9c0c9bf36340c94"
Expand Down Expand Up @@ -9221,6 +9236,15 @@ jsonfile@^4.0.0:
optionalDependencies:
graceful-fs "^4.1.6"

jsonfile@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.0.1.tgz#98966cba214378c8c84b82e085907b40bf614179"
integrity sha512-jR2b5v7d2vIOust+w3wtFKZIfpC2pnRmFAhAC/BuweZFQR8qZzxH1OyrQ10HmdVYiXWkYUqPVsz91cG7EL2FBg==
dependencies:
universalify "^1.0.0"
optionalDependencies:
graceful-fs "^4.1.6"

jsonify@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"
Expand Down Expand Up @@ -15084,6 +15108,11 @@ universalify@^0.1.0:
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==

universalify@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-1.0.0.tgz#b61a1da173e8435b2fe3c67d29b9adf8594bd16d"
integrity sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==

[email protected], unpipe@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"
Expand Down

0 comments on commit a3a7851

Please sign in to comment.