This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add helpers for file upload * Add verification for bytes size * Tune quote position * tune error text
- Loading branch information
Mykola Mokhnach
committed
Jan 18, 2018
1 parent
0371942
commit 2f79d46
Showing
5 changed files
with
115 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { createReadStream } from 'fs'; | ||
import { stat, exists } from './fs'; | ||
import url from 'url'; | ||
import B from 'bluebird'; | ||
import { toReadableSizeString } from './util'; | ||
import log from './logger'; | ||
import request from 'request-promise'; | ||
import Ftp from 'jsftp'; | ||
|
||
|
||
async function uploadFileToHttp (localFileStream, remoteUrl, uploadOptions = {}) { | ||
log.debug(`${remoteUrl.protocol} upload options: ${JSON.stringify(uploadOptions)}`); | ||
const response = await request(uploadOptions); | ||
const responseDebugMsg = `Response code: ${response.statusCode}. ` + | ||
`Response body: ${JSON.stringify(response.body)}`; | ||
log.debug(responseDebugMsg); | ||
if (response.statusCode >= 400) { | ||
throw new Error(`Cannot upload the recorded media to '${remoteUrl.href}'. ${responseDebugMsg}`); | ||
} | ||
} | ||
|
||
async function uploadFileToFtp (localFileStream, remoteUrl, uploadOptions = {}) { | ||
log.debug(`${remoteUrl.protocol} upload options: ${JSON.stringify(uploadOptions)}`); | ||
return await new B((resolve, reject) => { | ||
new Ftp(uploadOptions).put(localFileStream, remoteUrl.pathname, (err) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Uploads the given file to a remote location. HTTP(S) and FTP | ||
* protocols are supported. | ||
* | ||
* @param {string} localPath - The path to a file on the local storage. | ||
* @param {string} remotePath - The remote upload URL to upload the file to. | ||
* @param {Object} uploadOptions - The options set, which depends on the protocol set for remotePath. | ||
* See https://www.npmjs.com/package/request-promise and | ||
* https://www.npmjs.com/package/jsftp for more details. | ||
*/ | ||
async function uploadFile (localPath, remotePath, uploadOptions = {}) { | ||
if (!await exists(localPath)) { | ||
throw new Error (`'${localPath}' does not exists or is not accessible`); | ||
} | ||
const remoteUrl = url.parse(remotePath); | ||
const {size} = await stat(localPath); | ||
const localFileStream = createReadStream(localPath); | ||
log.info(`Uploading '${localPath}' of ${toReadableSizeString(size)} size to '${remotePath}'...`); | ||
const timeStarted = process.hrtime(); | ||
if (remoteUrl.protocol.startsWith('http')) { | ||
await uploadFileToHttp(localFileStream, remoteUrl, uploadOptions); | ||
} else if (remoteUrl.protocol === 'ftp') { | ||
await uploadFileToFtp(localFileStream, remoteUrl, uploadOptions); | ||
} else { | ||
throw new Error(`Cannot upload the file at '${localPath}' to '${remotePath}'. ` + | ||
`Unsupported remote protocol '${remoteUrl.protocol}'. ` + | ||
`Only http/https and ftp protocols are supported.`); | ||
} | ||
const timeElapsed = process.hrtime(timeStarted)[0]; | ||
log.info(`Uploaded '${localPath}' of ${toReadableSizeString(size)} size in ${timeElapsed} second${timeElapsed === 1 ? '' : 's'}`); | ||
} | ||
|
||
export { uploadFile }; |
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