Skip to content
This repository has been archived by the owner on Nov 12, 2020. It is now read-only.

Commit

Permalink
fix #300: Retry download onError after an upload
Browse files Browse the repository at this point in the history
  • Loading branch information
maxme committed Feb 29, 2016
1 parent d2e3c65 commit 2ce7348
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions libs/editor-common/assets/ZSSRichTextEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -924,24 +924,42 @@ ZSSEditor.replaceLocalImageWithRemoteImage = function(imageNodeIdentifier, remot
ZSSEditor.markImageUploadDone(imageNodeIdentifier);
var joinedArguments = ZSSEditor.getJoinedFocusedFieldIdAndCaretArguments();
ZSSEditor.callback("callback-input", joinedArguments);

image.onerror = null;
image.classList.add("image-loaded");
}

image.onerror = function () {
// Even on an error, we swap the image for the time being. This is because private
// blogs are currently failing to download images due to access privilege issues.
//
imageNode.attr('src', image.src);
imageNode.addClass("wp-image-" + remoteImageId);
ZSSEditor.markImageUploadDone(imageNodeIdentifier);
var joinedArguments = ZSSEditor.getJoinedFocusedFieldIdAndCaretArguments();
ZSSEditor.callback("callback-input", joinedArguments);

// Try to reload the image on error.
ZSSEditor.tryToReload(image, 1);
}

image.src = remoteImageUrl;
};

ZSSEditor.reloadImage = function(node, nCall) {
if (node.classList.contains("image-loaded")) {
return;
}
console.log("Reloading image:", node, nCall);
node.onerror = tryToReload(node, nCall + 1);
// Force reloading by updating image src
node.src = node.src;
}

ZSSEditor.tryToReload = function (node, nCall) {
if (nCall > 8) { // 7 tries: 22500 ms total
return;
}
console.log("Image not loaded:", node, "- image reloading will happen soon.");
node.onerror = null;
// reload the image with a variable delay: 500ms, 1000ms, 1500ms, 2000ms, etc.
setTimeout(reloadImage, nCall * 500, node, nCall);
}

/**
* @brief Update the progress indicator for the image identified with the value in progress.
*
Expand Down

0 comments on commit 2ce7348

Please sign in to comment.