-
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.
feat: save file locally when receiving postFile message
- Loading branch information
Showing
14 changed files
with
139 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const fs = require('fs'); | ||
const { POST_FILE_CHANNEL } = require('../config/channels'); | ||
const { buildFilesPath } = require('../config/config'); | ||
const { ensureDirectoryExistence } = require('../utilities'); | ||
const logger = require('../logger'); | ||
const { ERROR_GENERAL } = require('../config/errors'); | ||
|
||
const postFile = mainWindow => (event, payload = {}) => { | ||
try { | ||
const { userId, spaceId, data } = payload; | ||
|
||
// download file given path | ||
const { path, name } = data; | ||
|
||
const savePath = buildFilesPath({ userId, spaceId, name }); | ||
ensureDirectoryExistence(savePath); | ||
fs.copyFile(path, savePath, err => { | ||
if (err) { | ||
throw err; | ||
} | ||
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) { | ||
console.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
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,41 @@ | ||
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) { | ||
console.error(err); | ||
} | ||
}; |
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