changeset 79:9d735fe565d0

The app now lazily mirrors the if-archive if the story querystring param begins with 'if-archive'.
author Atul Varma <varmaa@toolness.com>
date Wed, 21 May 2008 11:23:49 -0700
parents de12cd84cf0a
children 38664b81d628
files cgi-bin/xhr_proxy.py web-zui.js
diffstat 2 files changed, 39 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/cgi-bin/xhr_proxy.py	Wed May 21 10:45:54 2008 -0700
+++ b/cgi-bin/xhr_proxy.py	Wed May 21 11:23:49 2008 -0700
@@ -22,22 +22,21 @@
 
 result = None
 
-if not re.match(ZCODE_REGEXP, path):
-    result = "ERROR: File does not appear to be a zcode file."
-if not localpath.startswith(ROOT_DIR):
-    result = "ERROR: Security violation: can't retrieve file below root dir."
-elif os.path.exists(localpath):
-    result = "SUCCESS: Path exists."
-else:
-    try:
+try:
+    if not re.match(ZCODE_REGEXP, path):
+        result = "ERROR: File does not appear to be a zcode file."
+    if not localpath.startswith(ROOT_DIR):
+        result = "ERROR: Security violation: can't retrieve file below root dir."
+    elif os.path.exists(localpath):
+        result = "SUCCESS: Path exists."
+    else:
         fileobj = urllib2.urlopen("http://www.ifarchive.org/if-archive/%s" % path)
-    except urllib2.HTTPError, e:
-        result = "ERROR: HTTP Response %s" % e
-    if not result:
         contents = fileobj.read()
         distutils.dir_util.mkpath(os.path.dirname(localpath))
         open(localpath, "wb").write(contents)
         result = "SUCCESS: File retrieved."
+except Exception, e:
+    result = "ERROR: Unexpected exception: %s" % e
 
 print "Content-Type: text/plain"
 print
--- a/web-zui.js	Wed May 21 10:45:54 2008 -0700
+++ b/web-zui.js	Wed May 21 11:23:49 2008 -0700
@@ -374,7 +374,24 @@
   self._eraseBottomWindow();
 }
 
-function loadBinaryUrl(url, callback) {
+function downloadViaProxy(relPath, callback) {
+  var PROXY_URL = baseUrl + "/cgi-bin/xhr_proxy.py";
+  var url = PROXY_URL + "?file=" + relPath.slice(IF_ARCHIVE_PREFIX.length);
+
+  $.ajax({url: url,
+          success: function(data, textStatus) {
+            if (data.indexOf("SUCCESS") == 0)
+              loadBinaryUrl(relPath, callback, false);
+            else
+              callback("error", "downloadViaProxy() failed: " + data);
+          },
+          error: function(XMLHttpRequest, textStatus, errorThrown) {
+            callback("error", "downloadViaProxy() failed: " + textStatus);
+          } });
+}
+
+function loadBinaryUrl(relPath, callback, useProxy) {
+  var url = baseUrl + "/" + relPath;
   var req = new XMLHttpRequest();
   req.open('GET',url,true);
   //XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
@@ -383,8 +400,11 @@
     if (req.readyState == 4)
       if (req.status == 200)
         callback("success", req.responseText);
-      else
-        callback("error");
+      else if (relPath.indexOf(IF_ARCHIVE_PREFIX) == 0 && useProxy) {
+        downloadViaProxy(relPath, callback);
+      } else {
+        callback("error", "loadBinaryUrl() failed, status " + req.status);
+      }
   };
   req.send(null);
 };
@@ -397,7 +417,7 @@
     }
     _webZuiStartup(zcode);
   } else {
-    throw new Error("Error occurred when retrieving z-code.");
+    throw new Error("Error occurred when retrieving z-code: " + data);
   }
 }
 
@@ -416,11 +436,13 @@
   runner.run();
 }
 
+var thisUrl = location.protocol + "//" + location.host + location.pathname;
+var baseUrl = thisUrl.slice(0, thisUrl.lastIndexOf("/"));
+var IF_ARCHIVE_PREFIX = "if-archive/";
+
 $(document).ready(function() {
-  var thisUrl = location.protocol + "//" + location.host + location.pathname;
-  var baseUrl = thisUrl.slice(0, thisUrl.lastIndexOf("/"));
   var qs = new Querystring();
   var story = qs.get("story", "stories/troll.z5");
 
-  loadBinaryUrl(baseUrl + "/" + story, _zcodeLoaded);
+  loadBinaryUrl(story, _zcodeLoaded, true);
 });