Skip to content
This repository has been archived by the owner on Aug 6, 2018. It is now read-only.

Commit

Permalink
feature: import files from url cc @arturi
Browse files Browse the repository at this point in the history
  • Loading branch information
ifedapoolarewaju committed Jan 21, 2018
1 parent 9d98c9e commit 0a33b48
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/server/Uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class Uploader {
* @property {string} protocol
* @property {object} metadata
* @property {number} size
* @property {string} fieldname
* @property {string=} fieldname
* @property {string} pathPrefix
* @property {string} pathSuffix
* @property {object} storage
* @property {object=} storage
* @property {string=} path
*
* @param {Options} options
Expand Down
68 changes: 68 additions & 0 deletions src/server/controllers/url.js
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)
}
2 changes: 2 additions & 0 deletions src/uppy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const grantConfig = require('./config/grant')()
const providerManager = require('./server/provider')
const dispatcher = require('./server/controllers/dispatcher')
const s3 = require('./server/controllers/s3')
const url = require('./server/controllers/url')
const SocketServer = require('ws').Server
const emitter = require('./server/WebsocketEmitter')
const merge = require('lodash.merge')
Expand Down Expand Up @@ -53,6 +54,7 @@ module.exports.app = (options = {}) => {
// add uppy options to the request object so it can be accessed by subsequent handlers.
app.use('*', getOptionsMiddleware(options))
app.use('/s3', s3(options.providerOptions.s3))
app.use('/url', url())
app.get('/:providerName/:action', dispatcher)
app.get('/:providerName/:action/:id', dispatcher)
app.post('/:providerName/:action', dispatcher)
Expand Down

0 comments on commit 0a33b48

Please sign in to comment.