Quellcode durchsuchen

better X-Storage-Type

none: Cached: User has no skin.
cached: Skin cached
checked: cached & checked for updates. No (new) skin
downloaded: checked, Skin changed or unknown, downloaded.
error: (error)
jomo vor 10 Jahren
Ursprung
Commit
03306dff08
3 geänderte Dateien mit 28 neuen und 24 gelöschten Zeilen
  1. 2 2
      modules/helpers.js
  2. 19 21
      routes/avatars.js
  3. 7 1
      views/index.jade

+ 2 - 2
modules/helpers.js

@@ -79,7 +79,7 @@ function skin_url(profile) {
 //   0: cached as null
 //   1: found on disk
 //   2: profile requested/found, skin downloaded from mojang servers
-//   3: profile requested/found, but it has no skin
+//   3: profile requested/found, but it has not changed or no skin
 function get_image_hash(uuid, callback) {
   cache.get_details(uuid, function(err, details) {
     if (err) {
@@ -96,7 +96,7 @@ function get_image_hash(uuid, callback) {
             callback(err, -1, details && details.hash);
           } else {
             console.log(uuid + " hash: " + hash);
-            callback(null, (hash ? 2 : 3), hash);
+            callback(null, (hash != (details && details.hash) ? 2 : 3), hash);
           }
         });
       }

+ 19 - 21
routes/avatars.js

@@ -3,6 +3,14 @@ var router = require('express').Router();
 var config = require('../modules/config');
 var skins = require('../modules/skins');
 
+var human_status = {
+  0: "none",
+  1: "cached",
+  2: "checked",
+  3: "downloaded",
+  "-1": "error"
+};
+
 /* GET avatar request. */
 router.get('/:uuid.:ext?', function(req, res) {
   var uuid = req.params.uuid;
@@ -24,47 +32,37 @@ router.get('/:uuid.:ext?', function(req, res) {
 
   try {
     helpers.get_avatar(uuid, helm, size, function(err, status, image) {
-      console.log(uuid + " - " + status);
+      console.log(uuid + " - " + human_status[status]);
       if (err) {
         console.error(err);
-        if (image) {
-          console.warn("error occured, image found anyway");
-          sendimage(503, true, image);
-        } else {
-          handle_default(404);
-        }
-      } else if (status == 1 || status == 2) {
-        sendimage(200, status == 1, image);
-      } else if (status === 0 || status == 3) {
-        handle_default(404);
+      }
+      if (image) {
+        sendimage(err ? 503 : 200, status, image);
       } else {
-        console.error("unexpected error/status");
-        console.error("error: " + err);
-        console.error("status: " + status);
-        handle_default(404);
+        handle_default(404, status);
       }
     });
   } catch(e) {
     console.error("Error!");
     console.error(e);
-    handle_default(500);
+    handle_default(500, status);
   }
 
-  function handle_default(status) {
+  function handle_default(http_status, img_status) {
     if (def != "steve" && def != "alex") {
       def = skins.default_skin(uuid);
     }
     skins.resize_img("public/images/" + def + ".png", size, function(err, image) {
-      sendimage(status, true, image);
+      sendimage(http_status, img_status, image);
     });
   }
 
-  function sendimage(status, local, image) {
-    res.writeHead(status, {
+  function sendimage(http_status, img_status, image) {
+    res.writeHead(http_status, {
       'Content-Type': 'image/png',
       'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
       'Response-Time': new Date() - start,
-      'X-Storage-Type': local ? 'local' : 'downloaded'
+      'X-Storage-Type': human_status[img_status]
     });
     res.end(image);
   }

+ 7 - 1
views/index.jade

@@ -38,7 +38,13 @@ block content
         h4 Response-Time
         p The time, in milliseconds, it took Crafatar to process the request.
         h4 X-Storage-Type
-        p Either 'local' or 'downloaded'. Local means that Crafatar already had the image on disk, while downloaded means that it was retrieved from Mojang's skin servers.
+        ul
+          li <b>none</b>: No external requests. Cached: User has no skin.
+          li <b>cached</b>: No external requests. Skin cached and stored locally.
+          li <b>checked</b>: 1 external request. Skin cached, checked for updates, no skin downloaded.<br>
+          | This happens either when the user has no skin or it didn't change.
+          li <b>downloaded</b>: 2 external requests. Skin changed or unknown, downloaded.
+          li <b>error</b>: This can happen, for example, when Mojang's servers are down. If possible, an outdated image will be served instead.
 
         h3 Examples
         p Get jeb_'s avatar, 160 × 160 pixels