Skip to content

Commit

Permalink
serve uploaded files from useruploads.instant.io
Browse files Browse the repository at this point in the history
For #18
  • Loading branch information
feross committed Jan 14, 2015
1 parent 4376571 commit e17ca18
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
secret
main.css
node_modules
secret
static
upload
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"debug": "^2.0.0",
"drag-drop": "^2.0.0",
"express": "^4.8.5",
"finalhandler": "^0.3.3",
"jade": "^1.5.0",
"magnet-uri": "^4.1.0",
"posix": "^1.0.3",
Expand Down
32 changes: 24 additions & 8 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var compress = require('compression')
var debug = require('debug')('instant')
var express = require('express')
var finalhandler = require('finalhandler')
var fs = require('fs')
var http = require('http')
var https = require('https')
Expand Down Expand Up @@ -64,7 +65,19 @@ app.use(function (req, res, next) {
next()
})

app.use(express.static(__dirname + '/../static'))
var serveUpload = express.static(path.join(__dirname, '../upload'), {
index: false,
setHeaders: function (res) {
res.header('Content-Disposition', 'attachment') // force file download
res.header('Content-Type', 'application/octet-stream') // generic, safe mime type
}
})
app.use(function (req, res, next) {
if (req.subdomains[req.subdomains.length - 1] !== 'useruploads') return next()
serveUpload(req, res, finalhandler(req, res, { onerror: error }))
})

app.use(express.static(path.join(__dirname, '../static')))

app.get('/', function (req, res) {
res.render('index')
Expand All @@ -76,12 +89,11 @@ var twilioClient = twilio(secret.twilio.accountSid, secret.twilio.authToken)

function updateIceServers () {
twilioClient.tokens.create({}, function (err, token) {
if (err) {
return console.error(err.stack || err.message || err)
}
if (!token.ice_servers) {
return console.error('twilio response ' + token + ' missing ice_servers')
}
if (err)
return error(err)
if (!token.ice_servers)
return error(new Error('twilio response ' + token + ' missing ice_servers'))

iceServers = token.ice_servers
.filter(function (server) {
return server && server.url && !/^stun:/.test(server.url)
Expand Down Expand Up @@ -115,7 +127,7 @@ app.get('*', function (req, res) {

// error handling middleware
app.use(function (err, req, res, next) {
console.error(err.stack)
error(err)
res.status(500).render('error', { message: err.message || err })
})

Expand All @@ -131,3 +143,7 @@ parallel([
debug('listening on port %s', JSON.stringify(config.ports))
util.downgradeUid()
})

function error (err) {
console.error(err.stack || err.message || err)
}

0 comments on commit e17ca18

Please sign in to comment.