Initial commit.

This commit is contained in:
Jonathan Cremin 2015-07-09 23:01:43 +01:00
commit b48a4e92e1
169 changed files with 7538 additions and 0 deletions

59
web/routes/file.js Normal file
View file

@ -0,0 +1,59 @@
import fs from 'fs';
import path from 'path';
import mime from 'mime-types';
import hostrFileStream from '../../lib/hostr-file-stream';
import { formatFile } from '../../lib/format';
import debugname from 'debug';
const debug = debugname('hostr-web:file');
const storePath = process.env.STORE_PATH || path.join(process.env.HOME, '.hostr', 'uploads');
const userAgentCheck = function(userAgent) {
if (!userAgent){
return false;
}
return userAgent.match(/^(wget|curl|vagrant)/i);
};
export function* get(id, name, size) {
debug('%s, %s, %s', id, name, size);
const file = yield this.db.Files.findOne({_id: id, 'file_name': name, 'status': 'active'});
this.assert(file, 404);
let localPath = path.join(storePath, file._id[0], file._id + '_' + file.file_name);
let remotePath = path.join(file._id[0], file._id + '_' + file.file_name);
if (size > 0) {
localPath = path.join(storePath, file._id[0], size, file._id + '_' + file.file_name);
remotePath = path.join(size, file._id + '_' + file.file_name);
}
let type = 'application/octet-stream';
if (file.width > 0) {
type = mime.lookup(file.file_name);
}
this.set('Content-type', type);
this.set('Expires', new Date(2020, 1).toISOString());
this.set('Cache-control', 'max-age=2592000');
this.body = yield hostrFileStream(localPath, remotePath);
}
export function* resized(size, id, name) {
yield get.call(this, id, name, size);
}
export function* landing(id, next) {
if (id === 'config.js') {
return yield next;
}
const file = yield this.db.Files.findOne({_id: id});
this.assert(file, 404);
if(userAgentCheck(this.headers['user-agent'])) {
return direct(file._id, file.file_name);
}
const formattedFile = formatFile(file);
debug(formattedFile);
yield this.render('file', {file: formattedFile});
}

55
web/routes/index.js Normal file
View file

@ -0,0 +1,55 @@
import uuid from 'node-uuid';
import auth from '../lib/auth';
export function* main() {
if (this.session.user) {
if (this.query['app-token']) {
return this.redirect('/');
}
const token = uuid.v4();
yield this.redis.set(token, this.session.user.id, 'EX', 604800);
this.session.user.token = token;
yield this.render('index', {user: this.session.user});
} else {
if (this.query['app-token']) {
const user = yield auth.fromToken(this, this.query['app-token']);
yield auth.setupSession(this, user);
this.redirect('/');
} else if (this.cookies.r) {
const user = yield auth.fromCookie(this, this.cookies.r);
yield auth.setupSession(this, user);
this.redirect('/');
} else {
yield this.render('marketing');
}
}
}
export function* staticPage(next) {
if (this.session.user) {
const token = uuid.v4();
yield this.redis.set(token, this.session.user.id, 'EX', 604800);
this.session.user.token = token;
yield this.render('index', {user: this.session.user});
} else {
switch(this.originalUrl) {
case '/terms':
yield this.render('terms');
break;
case '/privacy':
yield this.render('privacy');
break;
case '/pricing':
yield this.render('pricing');
break;
case '/apps':
yield this.render('apps');
break;
case '/stats':
yield this.render('index', {user: {}});
break;
default:
yield next;
}
}
}

81
web/routes/pro.js Normal file
View file

@ -0,0 +1,81 @@
import path from 'path';
import views from 'co-views';
const render = views(path.join(__dirname, '/../views'), { default: 'ejs'});
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
import { Mandrill } from 'mandrill-api/mandrill';
const mandrill = new Mandrill(process.env.MANDRILL_KEY);
const fromEmail = process.env.EMAIL_FROM || 'nobody@example.com';
const fromName = process.env.EMAIL_NAME || 'Nobody';
export function* create() {
const Users = this.db.Users;
const Transactions = this.db.Transactions;
const stripeToken = this.request.body.stripeToken;
const createCustomer = {
card: stripeToken.id,
plan: 'usd_monthly',
email: this.session.email
};
const customer = yield stripe.customers.create(createCustomer);
this.assert(customer.subscription.status === 'active', 400, '{"status": "error"}');
delete customer.subscriptions;
yield Users.updateOne({_id: this.session.user.id}, {'$set': {'stripe_customer': customer, type: 'Pro'}});
const transaction = {
'user_id': this.session.user.id,
amount: customer.subscription.plan.amount,
desc: customer.subscription.plan.name,
date: new Date(customer.subscription.plan.created * 1000)
};
yield Transactions.insertOne(transaction);
this.session.user.plan = 'Pro';
this.body = {status: 'active'};
let html = yield render('email/inlined/pro');
let text = `Hey, thanks for upgrading to Hostr Pro!
You've signed up for Hostr Pro Monthly at $6/Month.
Jonathan Cremin, Hostr Founder
`;
mandrill.messages.send({message: {
html: html,
text: text,
subject: 'Hostr Pro',
'from_email': fromEmail,
'from_name': fromName,
to: [{
email: this.session.user.email,
type: 'to'
}],
'tags': [
'pro-upgrade'
]
}});
}
export function* cancel() {
const Users = this.db.Users;
const user = yield Users.findOne({_id: this.session.user.id});
const confirmation = yield stripe.customers.cancelSubscription(
user.stripe_customer.id,
user.stripe_customer.subscription.id,
{ 'at_period_end': true }
);
yield Users.updateOne({_id: this.session.user.id}, {'$set': {'stripe_customer.subscription': confirmation, type: 'Free'}});
this.session.user.plan = 'Pro';
this.body = {status: 'inactive'};
}

85
web/routes/user.js Normal file
View file

@ -0,0 +1,85 @@
import { authenticate, setupSession, signup as signupUser, activateUser, sendResetToken, validateResetToken, updatePassword } from '../lib/auth';
export function* signin() {
if (!this.request.body.email) {
return yield this.render('signin');
}
const user = yield authenticate(this, this.request.body.email, this.request.body.password);
if(!user) {
return yield this.render('signin', {error: 'Invalid login details'});
} else if (user.activationCode) {
return yield this.render('signin', {error: 'Your account hasn\'t been activated yet. Check your for an activation email.'});
} else {
yield setupSession(this, user);
this.redirect('/');
}
}
export function* signup() {
if (!this.request.body.email) {
return yield this.render('signup');
}
if (this.request.body.email !== this.request.body.confirm_email) {
return yield this.render('signup', {error: 'Emails do not match.'});
} else if (this.request.body.email && !this.request.body.terms) {
return yield this.render('signup', {error: 'You must agree to the terms of service.'});
} else if (this.request.body.password && this.request.body.password.length < 7) {
return yield this.render('signup', {error: 'Password must be at least 7 characters long.'});
}
const ip = this.headers['x-real-ip'] || this.ip;
const email = this.request.body.email;
const password = this.request.body.password;
try {
yield signupUser(this, email, password, ip);
} catch (e) {
return yield this.render('signup', {error: e.message});
}
return yield this.render('signup', {message: 'Thanks for signing up, we\'ve sent you an email to activate your account.'});
}
export function* forgot(token) {
const Reset = this.db.Reset;
const Users = this.db.Users;
if (this.request.body.email) {
var email = this.request.body.email;
yield sendResetToken(this, email);
return yield this.render('forgot', {message: 'We\'ve sent an email with a link to reset your password. Be sure to check your spam folder if you it doesn\'t appear within a few minutes', token: null});
} else if (token && this.request.body.password) {
if (this.request.body.password.length < 7) {
return this.render('forgot', {error: 'Password needs to be at least 7 characters long.', token: token});
}
const tokenUser = yield validateResetToken(this, token);
var userId = tokenUser._id;
yield updatePassword(this, userId, this.request.body.password);
yield Reset.remove({_id: userId});
const user = yield Users.findOne({_id: userId});
yield setupSession(this, user);
this.redirect('/');
} else if (token.length) {
const tokenUser = yield validateResetToken(this, token);
if (!tokenUser) {
return yield this.render('forgot', {error: 'Invalid password reset token. It might be expired, or has already been used.', token: null});
} else {
return yield this.render('forgot', {token: token});
}
} else {
return yield this.render('forgot', {token: null});
}
}
export function* logout() {
this.cookies.set('r', {expires: new Date(1), path: '/'});
this.session = null;
this.redirect('/');
}
export function* activate(code) {
yield activateUser(this, code);
this.redirect('/');
}