Skip to content

Commit

Permalink
Add remote proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
kpdecker committed Feb 21, 2011
1 parent 8b1a728 commit 88773fe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
29 changes: 27 additions & 2 deletions test/user-image-cache.js
Expand Up @@ -6,7 +6,8 @@ $(document).ready(function(){
const ONE_PX_IMAGE = "data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=",
IMAGE_URL = "http://static.incaseofstairs.com/themes/pixeled/images/promotejsh.gif",
VALID_PAGE_STORE = "page-store://1",
MOCK_NAME = "/mockFile";
MOCK_NAME = "/mockFile",
REMOTE_PROXY = "proxy:";

var originalFile = window.File,
originalReader = window.FileReader;
Expand Down Expand Up @@ -119,7 +120,7 @@ $(document).ready(function(){
});
});

test("load Data URI", function() {
test("load Remote URL", function() {
expect(3);

UserImageCache.load(IMAGE_URL, loadFailed);
Expand All @@ -139,6 +140,30 @@ $(document).ready(function(){
equals(UserImageCache.getSrc(), ONE_PX_IMAGE, "getSrc");
});


test("load With Proxy", function() {
expect(9);

UserImageCache.setRemoteProxy(REMOTE_PROXY);
UserImageCache.load(ONE_PX_IMAGE, loadFailed);

equals(UserImageCache.getEntryId(), ONE_PX_IMAGE, "getEntryName");
equals(UserImageCache.getDisplayName(), ONE_PX_IMAGE, "getDisplayName");
equals(UserImageCache.getSrc(), ONE_PX_IMAGE, "getSrc");

UserImageCache.load(IMAGE_URL + "&%20", loadFailed);

equals(UserImageCache.getEntryId(), IMAGE_URL + "&%20", "getEntryName");
equals(UserImageCache.getDisplayName(), IMAGE_URL + "&%20", "getDisplayName");
equals(UserImageCache.getSrc(), REMOTE_PROXY + encodeURIComponent(IMAGE_URL) + "%26%2520", "getSrc");

UserImageCache.load(new File(), loadFailed);

equals(UserImageCache.getEntryId(), VALID_PAGE_STORE, "getEntryName");
equals(UserImageCache.getDisplayName(), MOCK_NAME, "getDisplayName");
equals(UserImageCache.getSrc(), ONE_PX_IMAGE, "getSrc");
});

test("Reload Mock File", function() {
expect(9);

Expand Down
16 changes: 15 additions & 1 deletion user-image-cache.js
Expand Up @@ -8,6 +8,7 @@
var UserImageCache;
(function() {
var curEntry,
remoteProxyUrl,
image;

// Check for browser support of session storage and that it is accessible
Expand Down Expand Up @@ -155,6 +156,14 @@ var UserImageCache;
image = el;
},

/*
* Sets the URL of the proxy server for loading remote URLs. On load the
* file href URL will be appended to the remote proxy url, if defined.
*/
setRemoteProxy: function(proxyUrl) {
remoteProxyUrl = proxyUrl;
},

/**
* Loads a given image.
*
Expand Down Expand Up @@ -188,7 +197,11 @@ var UserImageCache;
loadEntry.entryId = "page-store://" + match[1];
curEntry = loadEntry;
} else {
curEntry = { entryId: file, src: file, displayName: file };
var srcUrl = file;
if (remoteProxyUrl && /https?:\/\/.*/.test(file)) {
srcUrl = remoteProxyUrl + encodeURIComponent(file);
}
curEntry = { entryId: file, src: srcUrl, displayName: file };
}
image.src = UserImageCache.getSrc();
} else if (this.isLocalSupported() && file instanceof File) {
Expand Down Expand Up @@ -216,6 +229,7 @@ var UserImageCache;
localDataBinding.reset();
image = undefined;
curEntry = undefined;
remoteProxyUrl = undefined;
}
};
})();

0 comments on commit 88773fe

Please sign in to comment.