parent
fd994fdd97
commit
e3fada0cbb
@ -375,6 +375,8 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|||||||
if (!cachedSavePromises[fileName]) {
|
if (!cachedSavePromises[fileName]) {
|
||||||
cachedSavePromises[fileName] = getFileStorage().saveFile(fileName, bytes).then(function (blob) {
|
cachedSavePromises[fileName] = getFileStorage().saveFile(fileName, bytes).then(function (blob) {
|
||||||
return cachedDownloads[fileName] = blob;
|
return cachedDownloads[fileName] = blob;
|
||||||
|
}, function (error) {
|
||||||
|
delete cachedSavePromises[fileName];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return cachedSavePromises[fileName];
|
return cachedSavePromises[fileName];
|
||||||
@ -384,7 +386,6 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|||||||
if (!FileManager.isAvailable()) {
|
if (!FileManager.isAvailable()) {
|
||||||
return $q.reject({type: 'BROWSER_BLOB_NOT_SUPPORTED'});
|
return $q.reject({type: 'BROWSER_BLOB_NOT_SUPPORTED'});
|
||||||
}
|
}
|
||||||
// console.log('dload small', location);
|
|
||||||
var fileName = getFileName(location),
|
var fileName = getFileName(location),
|
||||||
mimeType = location.sticker ? 'image/webp' : 'image/jpeg',
|
mimeType = location.sticker ? 'image/webp' : 'image/jpeg',
|
||||||
cachedPromise = cachedSavePromises[fileName] || cachedDownloadPromises[fileName];
|
cachedPromise = cachedSavePromises[fileName] || cachedDownloadPromises[fileName];
|
||||||
|
@ -449,9 +449,16 @@ angular.module('izhukov.utils', [])
|
|||||||
if (getBlobSize(blob) > 10 * 1024 * 1024) {
|
if (getBlobSize(blob) > 10 * 1024 * 1024) {
|
||||||
return $q.reject();
|
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 {
|
try {
|
||||||
var reader = new FileReader();
|
var reader = new FileReader();
|
||||||
reader.readAsDataURL(blob);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
storageIsAvailable = false;
|
storageIsAvailable = false;
|
||||||
return $q.reject();
|
return $q.reject();
|
||||||
@ -460,23 +467,46 @@ angular.module('izhukov.utils', [])
|
|||||||
var deferred = $q.defer();
|
var deferred = $q.defer();
|
||||||
|
|
||||||
reader.onloadend = function() {
|
reader.onloadend = function() {
|
||||||
try {
|
storagePutB64String(db, fileName, reader.result).then(function () {
|
||||||
var objectStore = db.transaction([dbStoreName], IDBTransaction.READ_WRITE || 'readwrite').objectStore(dbStoreName),
|
|
||||||
request = objectStore.put(reader.result, fileName);
|
|
||||||
} catch (error) {
|
|
||||||
storageIsAvailable = false;
|
|
||||||
deferred.reject(error);
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
request.onsuccess = function (event) {
|
|
||||||
deferred.resolve(blob);
|
deferred.resolve(blob);
|
||||||
};
|
}, function (error) {
|
||||||
|
|
||||||
request.onerror = function (error) {
|
|
||||||
deferred.reject(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(b64string, fileName);
|
||||||
|
} catch (error) {
|
||||||
|
storageIsAvailable = false;
|
||||||
|
return $q.reject(error);
|
||||||
|
};
|
||||||
|
|
||||||
|
var deferred = $q.defer();
|
||||||
|
|
||||||
|
request.onsuccess = function (event) {
|
||||||
|
deferred.resolve();
|
||||||
|
};
|
||||||
|
|
||||||
|
request.onerror = function (error) {
|
||||||
|
deferred.reject(error);
|
||||||
|
};
|
||||||
|
|
||||||
return deferred.promise;
|
return deferred.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user