Skip to content

Commit

Permalink
Initial support for hosting files
Browse files Browse the repository at this point in the history
For #18
  • Loading branch information
feross committed Jan 13, 2015
1 parent f8048a0 commit 4f44736
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
17 changes: 17 additions & 0 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var dragDrop = require('drag-drop/buffer')
var magnet = require('magnet-uri')
var path = require('path')
var prettysize = require('prettysize')
var querystring = require('querystring')
var thunky = require('thunky')
var toBuffer = require('typedarray-to-buffer')
var upload = require('upload-element')
Expand Down Expand Up @@ -44,6 +45,22 @@ function download (infoHash) {
}

function seed (files) {
// Store files for 24 hours (if user requests it)
files.forEach(function (file) {
xhr({
url: '/upload?' + querystring.stringify({ name: file.name }),
method: 'POST',
headers: {
'Content-Type': file.type
},
body: file
}, function (err) {
if (err) return error(err)
// TODO: add message that file was stored
})
})

// Seed from WebTorrent
getClient(function (err, client) {
if (err) return error(err)
client.seed(files, onTorrent)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"url": "git://github.com/feross/instant.io.git"
},
"scripts": {
"build": "mkdir -p static && npm run build-css && npm run build-js",
"build": "mkdir -p static && mkdir -p upload && npm run build-css && npm run build-js",
"build-css": "stylus -u nib css/main.styl -o static/ -c",
"build-js": "browserify client > static/bundle.js",
"secret-download": "rsync -a -O -v --delete -e \"ssh -p 44444\" [email protected]:\"/home/feross/www/instant.io/secret/\" secret/",
Expand Down
11 changes: 11 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ app.get('/rtcConfig', function (req, res) {
else res.send({ iceServers: iceServers })
})

app.post('/upload', function (req, res) {
var saveTo = path.join(__dirname, '../upload', path.basename(req.query.name))
req.pipe(fs.createWriteStream(saveTo))
.on('end', function () {
res.status(200).send({ status: 'ok' })
})
.on('error', function (err) {
res.status(500).send({ error: err })
})
})

app.get('*', function (req, res) {
res.status(404).render('error', { message: '404 Not Found' })
})
Expand Down

0 comments on commit 4f44736

Please sign in to comment.