X-Git-Url: http://git.pjr.cc/?p=nodejs-repoproxy.git;a=blobdiff_plain;f=lib%2Fcache.js;fp=lib%2Fcache.js;h=6b6327d629d0da2e42f8d82e036c0c8205848351;hp=3e9ec770456645f90549b9980f4cc91885b9dd73;hb=8b95cb4f106b906a362babc2c21b18d57c7a4748;hpb=3fa5e831263792a76c9430b9d86e353901bc4477 diff --git a/lib/cache.js b/lib/cache.js index 3e9ec77..6b6327d 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -1,6 +1,7 @@ var fs = require("fs"); var http = require("http"); var url = require("url"); +var path = require("path"); function maintainCache() { // TODO i should check that im already running here and exit if i am @@ -35,8 +36,8 @@ function upstreamRequest(unify) { var headReq = url.parse(xpath); headReq["method"] = "HEAD"; - getup = http.request(xpath, function(res) { - res.setEncoding("utf8"); + getup = http.request(headReq, function(res) { + //res.setEncoding("utf8"); if(!endData) { console.log("status code is ", typeof res.statusCode); @@ -75,9 +76,14 @@ function upstreamRequest(unify) { endData = true; } else { // this is where it gets ugly + var filesize = res.headers["content-length"]; console.log("do ugly write: ", unify); //unify.b.write(data); - getAndService(unify, xpath); + var metafilename = unify.fullPathDirName + "/.meta."+ path.basename(unify.requestFor) +".filesize"; + var metafile = fs.createWriteStream(metafilename); + metafile.write(filesize); + metafile.end(); + getAndService(unify, xpath, filesize); } break; @@ -96,7 +102,11 @@ function upstreamRequest(unify) { exports.upstreamRequest = upstreamRequest; -function getAndService(unify, xpath) { +function getAndService(unify, xpath, filesize) { + + console.log("calling in here with filesize, ", filesize) + unify.b.writeHead(200, {'Content-Length' : filesize}); + if(typeof global.repoproxy.downloads[unify.fullFilePath] != "undefined" && global.repoproxy.downloads[unify.fullFilePath] == 1) { @@ -105,12 +115,13 @@ function getAndService(unify, xpath) { unify.b.end(); } else { global.repoproxy.downloads[unify.fullFilePath] = 1; + http.get(xpath, function(res) { var file = fs.createWriteStream(unify.fullFilePath); - console.log("res: ", res); + //console.log("res: ", res); //res.setEncoding("utf8"); @@ -138,21 +149,70 @@ function getAndService(unify, xpath) { function serviceFile(unify) { // for now, ignore range. + // however we need to check if a metadata file exists describing the filesize, check if its all correct + // and if not, erase the file (and metafile) and forward the request back to upstream request - // file should already exist, so we just poop it out - var inp = fs.createReadStream(unify.fullFilePath); - //inp.setEncoding("utf8"); - inp.on("data", function(data) { - unify.b.write(data); - }); - inp.on("end", function(closed) { - unify.b.end(); + checkFile(unify, function() { + + // file should already exist, so we just poop it out + var inp = fs.createReadStream(unify.fullFilePath); + //inp.setEncoding("utf8"); + inp.on("data", function(data) { + unify.b.write(data); + }); + + inp.on("end", function(closed) { + unify.b.end(); + }); }); } exports.serviceFile = serviceFile; + +function checkFile(unify, callback) { + // in here we do the metadata checks + var metafilename = unify.fullPathDirName + "/.meta."+ path.basename(unify.requestFor) +".filesize"; + + fs.exists(metafilename, function(existence) { + if(existence) { + var fsizef = fs.createReadStream(metafilename); + var fsize = ""; + fsizef.on("data", function(data) { + fsize += data; + }); + + fsizef.on("end", function() { + fs.stat(unify.fullFilePath, function(err, stats) { + var rfsize = stats["size"]; + if(rfsize != fsize.trim()) { + // remove the file and start again + console.log("reported filesizes dont match, '%s', '%s', removing file and starting again", rfsize, stats["size"]); + try { + fs.unlink(metafilename, function(){ + fs.unlink(unify.fullFilePath, function(){ + upstreamRequest(unify); + }) + }); + } catch(e) { + upstreamRequest(unify); + } + } else { + // we're good + unify.b.writeHead(200, {"Content-Length" : unify.fileSize}) + callback(); + } + }); + }); + } else { + console.log("file, '%s' exists but has no filesize meta data, assuming it was put here manually and servicing", unify.fullFilePath); + unify.b.writeHead(200, {"Content-Length" : unify.fileSize}) + callback(); + } + }); +} + function makeCacheDir(path) { console.log("attempting to create... '%s' as '%s'", path.fullPathDirName, path.subPathDirName);