mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-22 07:51:17 +01:00
use Etag, fix #18
This commit is contained in:
parent
040aeb1c0c
commit
95ebaf86c5
@ -100,7 +100,7 @@ exp.get_image_hash = function(uuid, callback) {
|
|||||||
|
|
||||||
|
|
||||||
// handles requests for +uuid+ images with +size+
|
// handles requests for +uuid+ images with +size+
|
||||||
// callback contains error, status, image buffer
|
// callback contains error, status, image buffer, hash
|
||||||
// image is the user's face+helm when helm is true, or the face otherwise
|
// image is the user's face+helm when helm is true, or the face otherwise
|
||||||
// for status, see get_image_hash
|
// for status, see get_image_hash
|
||||||
exp.get_avatar = function(uuid, helm, size, callback) {
|
exp.get_avatar = function(uuid, helm, size, callback) {
|
||||||
@ -115,16 +115,16 @@ exp.get_avatar = function(uuid, helm, size, callback) {
|
|||||||
}
|
}
|
||||||
skins.resize_img(filepath, size, function(img_err, result) {
|
skins.resize_img(filepath, size, function(img_err, result) {
|
||||||
if (img_err) {
|
if (img_err) {
|
||||||
callback(img_err, -1, null);
|
callback(img_err, -1, null, hash);
|
||||||
} else {
|
} else {
|
||||||
// we might have a hash although an error occured
|
// we might have a hash although an error occured
|
||||||
// (e.g. Mojang servers not reachable, using outdated hash)
|
// (e.g. Mojang servers not reachable, using outdated hash)
|
||||||
callback(err, (err ? -1 : status), result);
|
callback(err, (err ? -1 : status), result, hash);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// hash is null when uuid has no skin
|
// hash is null when uuid has no skin
|
||||||
callback(err, status, null);
|
callback(err, status, null, null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -58,6 +58,7 @@ router.get('/avatars/:uuid.:ext?', function(req, res) {
|
|||||||
var def = req.query.default;
|
var def = req.query.default;
|
||||||
var helm = req.query.hasOwnProperty('helm');
|
var helm = req.query.hasOwnProperty('helm');
|
||||||
var start = new Date();
|
var start = new Date();
|
||||||
|
var etag = null;
|
||||||
|
|
||||||
// Prevent app from crashing/freezing
|
// Prevent app from crashing/freezing
|
||||||
if (size < config.min_size || size > config.max_size) {
|
if (size < config.min_size || size > config.max_size) {
|
||||||
@ -74,13 +75,23 @@ router.get('/avatars/:uuid.:ext?', function(req, res) {
|
|||||||
uuid = uuid.replace(/-/g, "");
|
uuid = uuid.replace(/-/g, "");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
helpers.get_avatar(uuid, helm, size, function(err, status, image) {
|
helpers.get_avatar(uuid, helm, size, function(err, status, image, hash) {
|
||||||
logging.log(uuid + " - " + human_status[status]);
|
logging.log(uuid + " - " + human_status[status]);
|
||||||
if (err) {
|
if (err) {
|
||||||
logging.error(err);
|
logging.error(err);
|
||||||
}
|
}
|
||||||
|
etag = hash && hash.substr(0, 32) + (helm ? "-helm-" : "-face-") + size || "none";
|
||||||
|
var matches = req.get("If-None-Match") == '"' + etag + '"';
|
||||||
if (image) {
|
if (image) {
|
||||||
sendimage(err ? 503 : 200, status, image);
|
var http_status = 200;
|
||||||
|
if (matches) {
|
||||||
|
http_status = 304;
|
||||||
|
} else if (err) {
|
||||||
|
http_status = 503;
|
||||||
|
}
|
||||||
|
console.log("matches: " + matches);
|
||||||
|
console.log("status: " + http_status);
|
||||||
|
sendimage(http_status, status, image);
|
||||||
} else {
|
} else {
|
||||||
handle_default(404, status);
|
handle_default(404, status);
|
||||||
}
|
}
|
||||||
@ -113,9 +124,10 @@ router.get('/avatars/:uuid.:ext?', function(req, res) {
|
|||||||
'Content-Type': 'image/png',
|
'Content-Type': 'image/png',
|
||||||
'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
|
'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
|
||||||
'Response-Time': new Date() - start,
|
'Response-Time': new Date() - start,
|
||||||
'X-Storage-Type': human_status[img_status]
|
'X-Storage-Type': human_status[img_status],
|
||||||
|
'Etag': '"' + etag + '"'
|
||||||
});
|
});
|
||||||
res.end(image);
|
res.end(http_status == 304 ? null : image);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user