Browse Source

Now processing web before fwrite

master
Igor Zhukov 10 years ago
parent
commit
59a24d8096
  1. 63
      app/js/lib/mtproto_wrapper.js
  2. 10
      app/js/lib/ng_utils.js

63
app/js/lib/mtproto_wrapper.js

@ -255,7 +255,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
} }
}) })
.factory('MtpApiFileManager', function (MtpApiManager, $q, FileManager, IdbFileStorage, TmpfsFileStorage, MemoryFileStorage, WebpManager) { .factory('MtpApiFileManager', function (MtpApiManager, $q, qSync, FileManager, IdbFileStorage, TmpfsFileStorage, MemoryFileStorage, WebpManager) {
var cachedFs = false; var cachedFs = false;
var cachedFsPromise = false; var cachedFsPromise = false;
@ -335,7 +335,11 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
if (!location.volume_id) { if (!location.volume_id) {
console.trace('Empty location', location); console.trace('Empty location', location);
} }
return location.volume_id + '_' + location.local_id + '_' + location.secret + '.jpg'; var ext = 'jpg';
if (location.sticker) {
ext = WebpManager.isWebpSupported() ? 'webp' : 'png';
}
return location.volume_id + '_' + location.local_id + '_' + location.secret + '.' + ext;
} }
}; };
@ -411,17 +415,17 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
}); });
}); });
var processDownloaded = function (blob) { var processDownloaded = function (bytes) {
if (!location.sticker || WebpManager.isWebpSupported()) { if (!location.sticker || WebpManager.isWebpSupported()) {
return qSync.when(blob); return qSync.when(bytes);
} }
return WebpManager.getPngBlobFromWebp(blob); return WebpManager.getPngBlobFromWebp(bytes);
}; };
return fileStorage.getFileWriter(fileName, mimeType).then(function (fileWriter) { return fileStorage.getFileWriter(fileName, mimeType).then(function (fileWriter) {
return downloadPromise.then(function (result) { return downloadPromise.then(function (result) {
return processDownloaded.then(function (proccessedResult) { return processDownloaded(result.bytes).then(function (proccessedResult) {
return FileManager.write(fileWriter, proccessedResult.bytes).then(function () { return FileManager.write(fileWriter, proccessedResult).then(function () {
return cachedDownloads[fileName] = fileWriter.finalize(); return cachedDownloads[fileName] = fileWriter.finalize();
}); });
}) })
@ -444,6 +448,16 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
options = options || {}; options = options || {};
var processSticker = false;
if (location.sticker && !WebpManager.isWebpSupported()) {
if (options.toFileEntry || size > 524288) {
delete location.sticker;
} else {
processSticker = true;
options.mime = 'image/png';
}
}
// console.log(dT(), 'Dload file', dcID, location, size); // console.log(dT(), 'Dload file', dcID, location, size);
var fileName = getFileName(location), var fileName = getFileName(location),
toFileEntry = options.toFileEntry || null, toFileEntry = options.toFileEntry || null,
@ -488,6 +502,13 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
}, function () { }, function () {
var fileWriterPromise = toFileEntry ? FileManager.getFileWriter(toFileEntry) : fileStorage.getFileWriter(fileName, mimeType); var fileWriterPromise = toFileEntry ? FileManager.getFileWriter(toFileEntry) : fileStorage.getFileWriter(fileName, mimeType);
var processDownloaded = function (bytes) {
if (!processSticker) {
return qSync.when(bytes);
}
return WebpManager.getPngBlobFromWebp(bytes);
};
fileWriterPromise.then(function (fileWriter) { fileWriterPromise.then(function (fileWriter) {
cacheFileWriter = fileWriter; cacheFileWriter = fileWriter;
var limit = 524288, var limit = 524288,
@ -530,20 +551,22 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
if (canceled) { if (canceled) {
return $q.when(); return $q.when();
} }
return FileManager.write(fileWriter, result.bytes).then(function () { return processDownloaded(result.bytes).then(function (processedResult) {
writeFileDeferred.resolve(); return FileManager.write(fileWriter, processedResult).then(function () {
}, errorHandler).then(function () { writeFileDeferred.resolve();
if (isFinal) { }, errorHandler).then(function () {
resolved = true; if (isFinal) {
if (toFileEntry) { resolved = true;
deferred.resolve(); if (toFileEntry) {
deferred.resolve();
} else {
deferred.resolve(cachedDownloads[fileName] = fileWriter.finalize());
}
} else { } else {
deferred.resolve(cachedDownloads[fileName] = fileWriter.finalize()); deferred.notify({done: offset + limit, total: size});
} };
} else { });
deferred.notify({done: offset + limit, total: size}); })
};
});
}); });
}); });
})(offset + limit >= size, offset, writeFileDeferred, writeFilePromise); })(offset + limit >= size, offset, writeFileDeferred, writeFilePromise);

10
app/js/lib/ng_utils.js

@ -43,7 +43,9 @@ angular.module('izhukov.utils', [])
}, },
reject: function (result) { reject: function (result) {
return {then: function (cb, badcb) { return {then: function (cb, badcb) {
return badcb(result); if (badcb) {
return badcb(result);
}
}}; }};
} }
} }
@ -704,7 +706,7 @@ angular.module('izhukov.utils', [])
ok = (status == 0); ok = (status == 0);
if (!ok) { if (!ok) {
console.error('[webpjs] decoding failed', status); console.error('[webpjs] decoding failed', status, StatusCode);
return false; return false;
} }
@ -748,10 +750,10 @@ angular.module('izhukov.utils', [])
function getPngBlobFromWebp (data) { function getPngBlobFromWebp (data) {
if (!getCanvasFromWebp(data)) { if (!getCanvasFromWebp(data)) {
return qSync.reject({type: 'WEBP_PROCESS_FAILEd'}); return $q.reject({type: 'WEBP_PROCESS_FAILED'});
} }
if (canvas.toBlob === undefined) { if (canvas.toBlob === undefined) {
return dataUrlToBlob(canvas.toDataURL('image/png')); return qSync.when(dataUrlToBlob(canvas.toDataURL('image/png')));
} }
var deferred = $q.defer(); var deferred = $q.defer();

Loading…
Cancel
Save