This repository has been archived by the owner on Aug 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: import files from url cc @arturi
- Loading branch information
1 parent
9d98c9e
commit 0a33b48
Showing
3 changed files
with
72 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,68 @@ | ||
const router = require('express').Router | ||
const request = require('request') | ||
const Uploader = require('../Uploader') | ||
|
||
module.exports = () => { | ||
return router() | ||
.post('/meta', meta) | ||
.post('/get', get) | ||
} | ||
|
||
const meta = (req, res) => { | ||
// TODO: validate the body content | ||
const opts = { | ||
uri: req.body.url, | ||
method: 'HEAD', | ||
followAllRedirects: true | ||
} | ||
|
||
request(opts, (err, response, body) => { | ||
if (err) { | ||
console.error(err) | ||
return res.json({ err }) | ||
} | ||
|
||
res.json({ | ||
type: response.headers['content-type'], | ||
size: response.headers['content-length'] | ||
}) | ||
}) | ||
} | ||
|
||
const get = (req, res) => { | ||
// TODO: validate body content | ||
// @ts-ignore | ||
const { filePath } = req.uppy.options | ||
const uploader = new Uploader({ | ||
endpoint: req.body.endpoint, | ||
protocol: req.body.protocol, | ||
metadata: req.body.metadata, | ||
size: req.body.size, | ||
pathPrefix: `${filePath}`, | ||
pathSuffix: `${encodeURIComponent(req.body.url)}` | ||
}) | ||
|
||
uploader.onSocketReady(() => { | ||
downloadURL(req.body.url, uploader.handleChunk.bind(uploader)) | ||
}) | ||
|
||
const response = uploader.getResponse() | ||
return res.status(response.status).json(response.body) | ||
} | ||
|
||
/** | ||
* Downloads the content in the specified url, and passes the data | ||
* to the callback chunk by chunk. | ||
* | ||
* @param {string} url | ||
* @param {function} onDataChunk | ||
*/ | ||
const downloadURL = (url, onDataChunk) => { | ||
const opts = { | ||
uri: url, | ||
method: 'GET', | ||
followAllRedirects: true | ||
} | ||
|
||
request(opts).on('data', onDataChunk) | ||
} |
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