Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Unified Diff: ui/file_manager/image_loader/request.js

Issue 913203007: Estimate content type from url when it's not available. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix type annotation. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/file_manager/file_manager/foreground/css/file_manager.css ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/file_manager/image_loader/request.js
diff --git a/ui/file_manager/image_loader/request.js b/ui/file_manager/image_loader/request.js
index 5e2e11162057a553087a95058448daa0c3073123..49fdd37e0f278cb7dbc470e56de837836f207b1b 100644
--- a/ui/file_manager/image_loader/request.js
+++ b/ui/file_manager/image_loader/request.js
@@ -228,6 +228,9 @@ Request.prototype.downloadOriginal_ = function(onSuccess, onFailure) {
// Fetch the image via authorized XHR and parse it.
var parseImage = function(contentType, blob) {
+ if (contentType)
+ this.contentType_ = contentType;
+
this.image_.src = URL.createObjectURL(blob);
}.bind(this);
@@ -245,6 +248,19 @@ function AuthorizedXHR() {
}
/**
+ * A map which is used to estimate content type from extension.
+ * @enum {string}
+ */
+AuthorizedXHR.ExtensionContentTypeMap = {
+ gif: 'image/gif',
+ png: 'image/png',
+ svg: 'image/svg',
+ bmp: 'image/bmp',
+ jpg: 'image/jpeg',
+ jpeg: 'image/jpeg'
+};
+
+/**
* Aborts the current request (if running).
*/
AuthorizedXHR.prototype.abort = function() {
@@ -268,6 +284,12 @@ AuthorizedXHR.prototype.load = function(url, onSuccess, onFailure) {
// Do not call any callbacks when aborting.
var onMaybeSuccess = /** @type {function(string, Blob)} */ (
function(contentType, response) {
+ // When content type is not available, try to estimate it from url.
+ if (!contentType) {
+ contentType = AuthorizedXHR.ExtensionContentTypeMap[
+ this.extractExtension_(url)];
+ }
+
if (!this.aborted_)
onSuccess(contentType, response);
}.bind(this));
@@ -318,6 +340,16 @@ AuthorizedXHR.prototype.load = function(url, onSuccess, onFailure) {
};
/**
+ * Extracts extension from url.
+ * @param {string} url Url.
+ * @return {string} Extracted extensiion, e.g. png.
+ */
+AuthorizedXHR.prototype.extractExtension_ = function(url) {
+ var result = (/\.([a-zA-Z]+)$/i).exec(url);
+ return result ? result[1] : '';
+};
+
+/**
* Fetches data using authorized XmlHttpRequest with the provided OAuth2 token.
* If the token is invalid, the request will fail.
*
« no previous file with comments | « ui/file_manager/file_manager/foreground/css/file_manager.css ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698