index.coffee 1.26 KB
Newer Older
Aral Balkan's avatar
Aral Balkan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
express = require 'express'
multer = require 'multer'
fs = require 'fs'
moment = require 'moment'

app = express()

app.all '*', (req, res, next) ->
	# Cross-Origin Resource Sharing (CORS) — courtesy http://enable-cors.org/server_expressjs.html
	res.header "Access-Control-Allow-Origin", "*"
	res.header "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"
	next()

app.use multer { 
	dest: './videos/'
	
	limits:
		files: 1,
		fileSize: 50000000 # 50MB
	
	rename: (fieldname, filename) ->
		return (fieldname + '-' + moment().format().replace(/:/g, '_').replace(/\+/g, '_').replace(/-/g, '_') + '-' + filename).toLowerCase()

	onFileUploadStart: (file) ->
		console.log file.fieldname + ' is about to start uploading…'
		console.log file

		# Do some security checks.
		fieldnameIsWrong = (file.fieldname != 'video-message')
		mimetypeIsWrong = (/^video.*/.test(file.mimetype) == false)

		if fieldnameIsWrong or mimetypeIsWrong
			console.log 'File failed security checks. Not uploading.'
			return false

}

app.post '/upload', (request, response) ->
	console.log 'POST /upload called.'
	# console.dir(request.files)
	# console.log request.files['video-message']['size']

server = app.listen 3000, ->
	console.log "Listening on port %d", server.address().port