Skip to content

Instantly share code, notes, and snippets.

@AleksandrT
Last active December 22, 2015 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AleksandrT/6534082 to your computer and use it in GitHub Desktop.
Save AleksandrT/6534082 to your computer and use it in GitHub Desktop.
list.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Video Data</title>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script>
<script>
var ListView = Backbone.View.extend({
initialize: function() {
this.collection = new Backbone.Collection([], {
url: '/get-words'
});
this.listenTo(this.collection, 'add', this.append);
this.collection.fetch();
},
append: function (model, collection) {
$('#total').html(collection.length);
var row = new RowView({model: model});
this.$el.append(row.render().el);
},
render: function() {
this.$el.html('');
return this;
}
});
var RowView = Backbone.View.extend({
tagName: 'tr',
appendCol: function(attrName) {
var col = new ColView({
className: attrName,
text: this.model.get(attrName)
});
this.$el.append(col.render().el);
},
render: function() {
this.$el.html('');
this.appendCol('word');
this.appendCol('wclass');
this.appendCol('definition');
this.appendCol('comment');
return this;
}
});
var ColView = Backbone.View.extend({
tagName: 'td',
render: function () {
this.$el.html((this.options.className == 'word') ? '<a href="show/' + this.options.text + '">' + this.options.text + '</a>' : this.$el.html(this.options.text));
return this;
}
});
$(function () {
var view = new ListView({ el: $('#inject') });
});
</script>
</head>
<body style="padding: 10px;">
<div id="total"></div>
<table class="table table-condensed table-bordered table-striped">
<thead>
<tr>
<th>Word</th>
<th>Class</th>
<th>Definition</th>
<th>Comment</th>
</tr>
</thead>
<tbody id="inject"></tbody>
</table>
</body>
</html>
var express = require('express'),
mongo = require('mongoskin'),
fs = require('fs'),
exec = require('child_process').exec,
format = require('util').format;
var app = express();
var db = mongo.db('localhost:27017/t3?auto_reconnect', {safe: true});
db.bind('words', {
getAll: function(fn) {
this.find({}, {sort: [['word', 1]]}).toArray(fn);
}
});
app.use(express.bodyParser());
var Init = require('./init'),
init = new Init();
var rootPath = init.rootPath;
var availVideoFormats = [
'mp4-1080p',
'mp4-720p'
];
var availWordClasses = [
'Noun',
'Adjective',
'Adverb',
'Pronoun',
'Conjunction',
'Preposition',
'Interjection',
'Numeral',
'Verb',
'Sentence'
];
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
app.post('/', function (req, res, next) {
var word = req.body.word.trim(),
wclass = req.body.wclass,
definition = req.body.definition.trim(),
comment = req.body.comment.trim(),
vformat = req.body.videoformat,
vsize = req.files.video.size,
vpath = req.files.video.path;
if ( word.length < 1
|| vsize < 1
|| availWordClasses.indexOf(wclass) === -1
|| availVideoFormats.indexOf(vformat) === -1)
{
res.send(400, 'Bad request. <a href="/">Back</a>');
return;
}
var data = {
word : word,
wclass : wclass,
definition : definition,
comment : comment,
videos: [{
type : vformat,
size : vsize
}],
inclinations : []
};
db.collection('words').insert(data, function (err, result) {
var path = rootPath + '/' + result[0]._id;
fs.mkdir(path, '0775', function (err) {
var dest = path + '/' + vformat;
fs.rename(vpath, dest, function (err) {
res.send('Upload complete! <a href="/">Back</a>');
});
});
});
});
app.get('/list', function(req, res) {
res.sendfile(__dirname + '/list.html');
});
app.get('/get-words', function(req, res) {
db.collection('words').getAll(function(err, result) {
res.send(JSON.stringify(result));
});
});
app.get('/get-word/:id', function (req, res) {
var id = req.params.id;
db.collection('words').getAll(function (err, result) {
res.send(JSON.stringify(result));
});
});
app.get('/show/:word', function (req, res) {
var _word = req.params.word;
console.log(_word);
db.collection('words').findOne({'word': 'arv'}, function (err, result) {
console.log(typeof result._id);
if (err || !result.count) {
if (err) {
console.log(err.toString());
}
else {
console.log(result.count);
}
res.send(404, 'Not found.');
return;
}
res.send("<!DOCTYPE html><html lang=\"en\">" +
"<head><title>Video</title>" +
"<link href='http://vjs.zencdn.net/4.1/video-js.css' rel='stylesheet'>" +
"<script src='http://vjs.zencdn.net/4.1/video.js'></script>" +
"</head>" +
"<body><center>" +
"<video id='video' class='video-js vjs-default-skin'" +
" controls preload='auto' width='640' height='264'" +
" source src='/video/" + result._id + "' type='video/mp4' />" +
"</center></body></html>");
});
});
app.get('/video/:id', function (req, res) {
var id = req.params.id;
db.collection('words').findById(id, function (err, result) {
if (!err || !result.count) {
res.send(404, 'Not found.');
return;
}
var path = __dirname + '\\videos\\' + result._id + "\\" + result.videos[0].type;
fs.exists(path, function (exists) {
if (exists) {
res.writeHead(200, { 'Content-Length': result.videos[0].size, 'Content-Type': 'video/mp4' });
var stream = fs.createReadStream(path);
stream.pipe(res);
}
});
});
});
app.get('/delete/:id', function(req, res) {
var id = req.params.id;
db.collection('words').removeById(id, function(err, count) {
if (err || !count) {
res.send('Item not found. <a href="/list">Back</a>');
return;
}
exec('rm -r ' + rootPath + '/' + id, function(error, stdout, stderr) {
if (error) {
console.log(stderr);
}
res.send('Item removed. <a href="/list">Back</a>');
});
});
});
app.listen(process.argv[2] || 80);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment