Browse Source

Fixed IDb images load

Closes #597
Closes #845
master
Igor Zhukov 9 years ago
parent
commit
e3fada0cbb
  1. 3
      app/js/lib/mtproto_wrapper.js
  2. 42
      app/js/lib/ng_utils.js

3
app/js/lib/mtproto_wrapper.js

@ -375,6 +375,8 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) @@ -375,6 +375,8 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
if (!cachedSavePromises[fileName]) {
cachedSavePromises[fileName] = getFileStorage().saveFile(fileName, bytes).then(function (blob) {
return cachedDownloads[fileName] = blob;
}, function (error) {
delete cachedSavePromises[fileName];
});
}
return cachedSavePromises[fileName];
@ -384,7 +386,6 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) @@ -384,7 +386,6 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
if (!FileManager.isAvailable()) {
return $q.reject({type: 'BROWSER_BLOB_NOT_SUPPORTED'});
}
// console.log('dload small', location);
var fileName = getFileName(location),
mimeType = location.sticker ? 'image/webp' : 'image/jpeg',
cachedPromise = cachedSavePromises[fileName] || cachedDownloadPromises[fileName];

42
app/js/lib/ng_utils.js

@ -449,9 +449,16 @@ angular.module('izhukov.utils', []) @@ -449,9 +449,16 @@ angular.module('izhukov.utils', [])
if (getBlobSize(blob) > 10 * 1024 * 1024) {
return $q.reject();
}
if (!(blob instanceof Blob)) {
var mimeType = blob.type || 'image/jpeg';
var address = 'data:' + mimeType + ';base64,' + bytesToBase64(blob);
return storagePutB64String(db, fileName, address).then(function () {
return blob;
});
}
try {
var reader = new FileReader();
reader.readAsDataURL(blob);
} catch (e) {
storageIsAvailable = false;
return $q.reject();
@ -460,22 +467,45 @@ angular.module('izhukov.utils', []) @@ -460,22 +467,45 @@ angular.module('izhukov.utils', [])
var deferred = $q.defer();
reader.onloadend = function() {
storagePutB64String(db, fileName, reader.result).then(function () {
deferred.resolve(blob);
}, function (error) {
deferred.reject(error);
});
}
reader.onerror = function (error) {
deferred.reject(error);
}
try {
reader.readAsDataURL(blob);
} catch (e) {
storageIsAvailable = false;
return $q.reject();
}
return deferred.promise;
}
function storagePutB64String (db, fileName, b64string) {
try {
var objectStore = db.transaction([dbStoreName], IDBTransaction.READ_WRITE || 'readwrite').objectStore(dbStoreName),
request = objectStore.put(reader.result, fileName);
request = objectStore.put(b64string, fileName);
} catch (error) {
storageIsAvailable = false;
deferred.reject(error);
return;
return $q.reject(error);
};
var deferred = $q.defer();
request.onsuccess = function (event) {
deferred.resolve(blob);
deferred.resolve();
};
request.onerror = function (error) {
deferred.reject(error);
};
}
return deferred.promise;
}

Loading…
Cancel
Save