Skip to content

Commit

Permalink
git-style folder-separation in cache dir
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Jan 18, 2013
1 parent f8caac3 commit b087611
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var spawn = require('child_process').spawn
var fs = require('fs')
var path = require('path')
var EventEmitter = require('events').EventEmitter
var crypto = require('crypto')

var app = module.exports = express()

Expand Down Expand Up @@ -48,20 +49,28 @@ app.get('/:url/:resolution/:wait', function (req, res, next) {
})
}

var hash = new Buffer(req.params['url'] + req.params['resolution']).toString('base64')
var filename = cacheDir + hash
var hash = crypto
.createHash('sha1')
.update(req.params['url'])
.update(req.params['resolution'])
.digest('hex')

var dir = hash.slice(0, 2)
var filename = cacheDir + dir + '/' + hash

function createCash () {
caching[hash] = true
snapshot(req.params, function (err, rasterized) {
if (err) return caching[hash] = false, next(err)

fs.writeFile(filename, rasterized, function (err) {
caching[hash] = false
if (err) return next(err)

res.set('Content-Type', 'image/png').end(rasterized)
caches.emit(hash)
fs.mkdir(cacheDir + dir, function () {
fs.writeFile(filename, rasterized, function (err) {
caching[hash] = false
if (err) return next(err)

res.set('Content-Type', 'image/png').end(rasterized)
caches.emit(hash)
})
})
})
}
Expand Down

0 comments on commit b087611

Please sign in to comment.