-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from graasp/242/postFile
feat: save file locally when receiving postFile message
- Loading branch information
Showing
15 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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" | ||
|