mirror of
https://github.com/schlagmichdoch/PairDrop.git
synced 2025-04-20 15:06:15 -04:00
Tidy up zipper functions
This commit is contained in:
parent
65936a4d7d
commit
1df8fe258e
2 changed files with 56 additions and 61 deletions
|
@ -1231,23 +1231,31 @@ class ReceiveFileDialog extends ReceiveDialog {
|
||||||
}
|
}
|
||||||
|
|
||||||
async _processDataAsZip() {
|
async _processDataAsZip() {
|
||||||
let zipFileUrl, zipFileName;
|
let zipObjectUrl = "";
|
||||||
|
let zipName = "";
|
||||||
let sendAsZip = false;
|
let sendAsZip = false;
|
||||||
|
|
||||||
const tooBigToZip = window.iOS && this._data.totalSize > 256000000;
|
const tooBigToZip = window.iOS && this._data.totalSize > 256000000;
|
||||||
|
|
||||||
if (this._data.files.length > 1 && !tooBigToZip) {
|
if (this._data.files.length > 1 && !tooBigToZip) {
|
||||||
zipFileUrl = await this._createZipFile(this._data.files, zipProgress => {
|
Events.fire('set-progress', {
|
||||||
|
peerId: this._data.peerId,
|
||||||
|
progress: 0,
|
||||||
|
status: 'process'
|
||||||
|
});
|
||||||
|
|
||||||
|
zipObjectUrl = await zipper.getObjectUrlOfZipFile(this._data.files,zipProgress => {
|
||||||
Events.fire('set-progress', {
|
Events.fire('set-progress', {
|
||||||
peerId: this._data.peerId,
|
peerId: this._data.peerId,
|
||||||
progress: zipProgress / this._data.totalSize,
|
progress: zipProgress / this._data.totalSize,
|
||||||
status: 'process'
|
status: 'process'
|
||||||
})
|
|
||||||
});
|
});
|
||||||
zipFileName = this._createZipFilename()
|
});
|
||||||
|
zipName = this._createZipFilename();
|
||||||
|
|
||||||
sendAsZip = !!zipFileUrl;
|
sendAsZip = !!zipObjectUrl;
|
||||||
}
|
}
|
||||||
return {sendAsZip, zipFileUrl, zipFileName};
|
return {sendAsZip, zipObjectUrl, zipName};
|
||||||
}
|
}
|
||||||
|
|
||||||
_setDownloadButtonToZip(zipFileUrl, zipFileName) {
|
_setDownloadButtonToZip(zipFileUrl, zipFileName) {
|
||||||
|
@ -1318,27 +1326,6 @@ class ReceiveFileDialog extends ReceiveDialog {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async _createZipFile(files, onProgressCallback) {
|
|
||||||
try {
|
|
||||||
let bytesCompleted = 0;
|
|
||||||
|
|
||||||
zipper.createNewZipWriter();
|
|
||||||
|
|
||||||
for (let i = 0; i < files.length; i++) {
|
|
||||||
await zipper.addFile(files[i], {
|
|
||||||
onprogress: (progress) => onProgressCallback(bytesCompleted + progress)
|
|
||||||
});
|
|
||||||
bytesCompleted += files[i].size;
|
|
||||||
}
|
|
||||||
|
|
||||||
return await zipper.getBlobURL();
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
Logger.error(e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_createZipFilename() {
|
_createZipFilename() {
|
||||||
let now = new Date(Date.now());
|
let now = new Date(Date.now());
|
||||||
let year = now.getFullYear().toString();
|
let year = now.getFullYear().toString();
|
||||||
|
@ -1373,11 +1360,11 @@ class ReceiveFileDialog extends ReceiveDialog {
|
||||||
this.$downloadBtn.removeAttribute('disabled');
|
this.$downloadBtn.removeAttribute('disabled');
|
||||||
this.$downloadBtn.removeAttribute('hidden');
|
this.$downloadBtn.removeAttribute('hidden');
|
||||||
|
|
||||||
let {sendAsZip, zipFileUrl, zipFileName} = await this._processDataAsZip();
|
let {sendAsZip, zipObjectUrl, zipName} = await this._processDataAsZip();
|
||||||
|
|
||||||
// If single file or zipping failed -> download files individually -> else download zip
|
// If single file or zipping failed -> download files individually -> else download zip
|
||||||
if (sendAsZip) {
|
if (sendAsZip) {
|
||||||
this._setDownloadButtonToZip(zipFileUrl, zipFileName);
|
this._setDownloadButtonToZip(zipObjectUrl, zipName);
|
||||||
} else {
|
} else {
|
||||||
this._setDownloadButtonToFiles(this._data.files);
|
this._setDownloadButtonToFiles(this._data.files);
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,39 +82,47 @@ const audioPlayer = (() => {
|
||||||
|
|
||||||
const zipper = (() => {
|
const zipper = (() => {
|
||||||
|
|
||||||
let zipWriter;
|
|
||||||
return {
|
return {
|
||||||
createNewZipWriter() {
|
async getObjectUrlOfZipFile(files, onZipProgressCallback){
|
||||||
zipWriter = new zip.ZipWriter(new zip.BlobWriter("application/zip"), { bufferedWrite: true, level: 0 });
|
try {
|
||||||
},
|
const zipWriter = new zip.ZipWriter(new zip.BlobWriter("application/zip"));
|
||||||
addFile(file, options) {
|
|
||||||
return zipWriter.add(file.name, new zip.BlobReader(file), options);
|
let bytesProcessed = 0;
|
||||||
},
|
for (let i = 0; i < files.length; i++) {
|
||||||
async getBlobURL() {
|
await zipWriter.add(
|
||||||
if (zipWriter) {
|
files[i].name,
|
||||||
const blobURL = URL.createObjectURL(await zipWriter.close());
|
new zip.BlobReader(files[i]),
|
||||||
zipWriter = null;
|
{
|
||||||
return blobURL;
|
onprogress: (progress) => onZipProgressCallback(bytesProcessed + progress)
|
||||||
}
|
}
|
||||||
else {
|
);
|
||||||
throw new Error("Zip file closed");
|
bytesProcessed += files[i].size;
|
||||||
}
|
}
|
||||||
},
|
|
||||||
async getZipFile(filename = "archive.zip") {
|
return URL.createObjectURL(await zipWriter.close());
|
||||||
if (zipWriter) {
|
|
||||||
const file = new File([await zipWriter.close()], filename, {type: "application/zip"});
|
|
||||||
zipWriter = null;
|
|
||||||
return file;
|
|
||||||
}
|
}
|
||||||
else {
|
catch (e) {
|
||||||
throw new Error("Zip file closed");
|
Logger.error(e);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async getEntries(file, options) {
|
async getEntries(file, options) {
|
||||||
|
try {
|
||||||
return await (new zip.ZipReader(new zip.BlobReader(file))).getEntries(options);
|
return await (new zip.ZipReader(new zip.BlobReader(file))).getEntries(options);
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
Logger.error(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
async getData(entry, options) {
|
async getData(entry, options) {
|
||||||
|
try {
|
||||||
return await entry.getData(new zip.BlobWriter(), options);
|
return await entry.getData(new zip.BlobWriter(), options);
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
Logger.error(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue