var Express = require("express");
var static = new Express.Router();
var Blog = require("blog");
var config = require("config");
var mime = require("mime-types");

// This route is used by Blot's CDN (AWS Cloudfront) to retrieve
// static assets generated by Blot and serve them to your readers.
// A request to:
//
// https://blotcdn.com/example/path-to/file.jpg
//
// Will have its SSl terminated by Cloudfront close to the user.
// Cloudfront will then request the following URL from Blot's
// main server and send back the response:
//
// https://blot.im/static/example/path-to/file.jpg
//
// Cloudfront will store this response to server future requests

// This route can only be used on Blot's main host (e.g. blot.im)
// and not on user's blogs (e.g. dev.blot.im or example.com)
static.use(function(req, res, next){
  
  if (req.get("host") === config.host) return next();
  
  var err;

  err = new Error("Not a request to main host");
  err.code = 'EINVAL';

  next(err);
});

// Eventually we'll use non-sequential blogIDs and just encode
// the blogID in the URL to remove this step.
static.param("handle", function(req, res, next, handle) {
  Blog.get({ handle: handle }, function(err, blog) {
    if (err) return next(err);
    if (!blog) return next(new Error("No blog"));

    req.blog = blog;
    next();
  });
});

static.route("/:handle/:path*").get(function(req, res) {
  var path = req.params.path + req.params[0];
  var options = {
    root: config.blog_static_files_dir + "/" + req.blog.id,
    headers: {
      "Content-Type": getContentType(path),
      "Cache-Control": "public, max-age=31536000"
    }
  };

  res.sendFile(path, options, function(err) {
    if (err) {
      res.sendStatus(404);
    }
  });
});

static.use(function(req, res, next) {
  res.sendStatus(404);
});

static.use(function(err, req, res, next) {

  if (err.code === 'EINVAL') return next();

  res.sendStatus(404);
});

function getContentType(path) {
  // If we can't determine a mime type for a given path,
  // assume it is HTML if we are responding to a request
  // for a directory, or an octet stream otherwise...
  var default_mime =
    path.indexOf(".") > -1 ? "application/octet-stream" : "text/html";

  return mime.contentType(mime.lookup(path) || default_mime);
}

module.exports = static;

var Express = require("express");
var static = new Express.Router();
var Blog = require("blog");
var config = require("config");
var mime = require("mime-types");

// This route is used by Blot's CDN (AWS Cloudfront) to retrieve
// static assets generated by Blot and serve them to your readers.
// A request to:
//
// https://blotcdn.com/{blogID}/path-to/file.jpg
//
// Will have its SSl terminated by Cloudfront close to the user.
// Cloudfront will then request the following URL from Blot's
// main server and send back the response:
//
// https://blot.im/static/{blogID}/path-to/file.jpg
//
// Cloudfront will store this response to server future requests

// This route can only be used on Blot's main host (e.g. blot.im)
// and not on user's blogs (e.g. dev.blot.im or example.com)
static.use(function(req, res, next) {
  if (req.get("host") === config.host) return next();

  var err;

  err = new Error("Not a request to main host");
  err.code = "EINVAL";

  next(err);
});

static.route("/:id/:path*").get(function(req, res) {
  var path = req.params.path + req.params[0];
  var options = {
    root: config.blog_static_files_dir + "/" + req.params.id,
    headers: {
      "Content-Type": getContentType(path),
      "Cache-Control": "public, max-age=31536000"
    }
  };

  res.sendFile(path, options, function(err) {
    if (err) {
      res.sendStatus(404);
    }
  });
});

static.use(function(req, res, next) {
  res.sendStatus(404);
});

static.use(function(err, req, res, next) {
  if (err.code === "EINVAL") return next();

  res.sendStatus(404);
});

function getContentType(path) {
  // If we can't determine a mime type for a given path,
  // assume it is HTML if we are responding to a request
  // for a directory, or an octet stream otherwise...
  var default_mime =
    path.indexOf(".") > -1 ? "application/octet-stream" : "text/html";

  return mime.contentType(mime.lookup(path) || default_mime);
}

module.exports = static;

Adjust static.js route to use new blogIDs instead of handles