Преглед на файлове

re-add checking for existing file before downloading

jomo преди 10 години
родител
ревизия
e01cff53b2
променени са 3 файла, в които са добавени 31 реда и са изтрити 66 реда
  1. 28 38
      modules/helpers.js
  2. 2 1
      modules/networking.js
  3. 1 27
      test/test.js

+ 28 - 38
modules/helpers.js

@@ -35,26 +35,34 @@ function store_images(uuid, details, callback) {
           logging.log(uuid + " new hash: " + hash);
           var facepath = __dirname + '/../' + config.faces_dir + hash + ".png";
           var helmpath = __dirname + '/../' + config.helms_dir + hash + ".png";
-          // download skin
-          networking.get_skin(skin_url, function(err, img) {
-            if (err || !img) {
-              callback(err, null);
-            } else {
-              // extract face / helm
-              skins.extract_face(img, facepath, function(err) {
-                if (err) {
-                  callback(err);
-                } else {
-                  logging.log(facepath + " face extracted");
-                  skins.extract_helm(facepath, img, helmpath, function(err) {
-                    logging.log(helmpath + " helm extracted.");
-                    cache.save_hash(uuid, hash);
-                    callback(err, hash);
-                  });
-                }
-              });
-            }
-          });
+
+          if (fs.existsSync(facepath)) {
+            logging.log(uuid + " Avatar already exists, not downloading");
+            callback(null, hash);
+          } else {
+            // download skin
+            networking.get_skin(skin_url, function(err, img) {
+              if (err || !img) {
+                callback(err, null);
+              } else {
+                // extract face / helm
+                skins.extract_face(img, facepath, function(err) {
+                  if (err) {
+                    callback(err);
+                  } else {
+                    logging.log(uuid + " face extracted");
+                    logging.debug(facepath);
+                    skins.extract_helm(facepath, img, helmpath, function(err) {
+                      logging.log(uuid + " helm extracted");
+                      logging.debug(helmpath);
+                      cache.save_hash(uuid, hash);
+                      callback(err, hash);
+                    });
+                  }
+                });
+              }
+            });
+          }
         }
       } else {
         // profile found, but has no skin
@@ -144,22 +152,4 @@ exp.get_avatar = function(uuid, helm, size, callback) {
   });
 };
 
-exp.get_skin = function(uuid, callback) {
-  logging.log("\nskin request: " + uuid);
-  exp.get_image_hash(uuid, function(err, status, hash) {
-    if (hash) {
-      var skinurl = "http://textures.minecraft.net/texture/" + hash;
-      networking.get_skin(skinurl, null, function(err, img) {
-        if (err) {
-          logging.log("\nerror while downloading skin");
-          callback(err, hash, null);
-        } else {
-          logging.log("\nreturning skin");
-          callback(null, hash, img);
-        }
-      });
-    }
-  });
-};
-
 module.exports = exp;

+ 2 - 1
modules/networking.js

@@ -108,7 +108,8 @@ exp.get_skin = function(url, callback) {
   }, function (error, response, body) {
     if (!error && response.statusCode == 200) {
       // skin downloaded successfully
-      logging.log(url + " skin downloaded");
+      logging.log("downloaded skin");
+      logging.debug(url);
       callback(null, body);
     } else {
       if (error) {

+ 1 - 27
test/test.js

@@ -133,23 +133,6 @@ describe('Crafatar', function() {
         done();
       });
     });
-    it("should already have the files / not download", function(done) {
-
-      // FIXME: implement & remove this line
-      throw("Please re-implement this and add a new test");
-
-
-      assert.doesNotThrow(function() {
-        fs.openSync("face.png", "w");
-        fs.openSync("helm.png", "w");
-        networking.get_skin("http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", function(err, img) {
-          assert.strictEqual(err, null); // no error here, but it shouldn't throw exceptions
-          fs.unlinkSync("face.png");
-          fs.unlinkSync("helm.png");
-          done();
-        });
-      });
-    });
     it("should default to Alex", function(done) {
       assert.strictEqual(skins.default_skin("ec561538f3fd461daff5086b22154bce"), "alex");
       done();
@@ -218,13 +201,4 @@ describe('Crafatar', function() {
       done();
     });
   });
-
-  after(function() {
-    try {
-      // these files could be here if a test failed
-      fs.unlinkSync("face.png");
-      fs.unlinkSync("helm.png");
-    } catch(e){}
-  });
-
-});
+});