moved saveFile from utils to fileReceiver

This commit is contained in:
Danny Coates 2018-02-21 14:58:41 -08:00
parent 12443db891
commit e4b98fe65a
No known key found for this signature in database
GPG key ID: 4C442633C62E00CB
3 changed files with 34 additions and 35 deletions

View file

@ -78,16 +78,44 @@ export default class FileReceiver extends Nanobus {
if (this.cancelled) {
throw new Error(0);
}
this.msg = 'downloadFinish';
this.state = 'complete';
return {
await saveFile({
plaintext,
name: decodeURIComponent(this.fileInfo.name),
type: this.fileInfo.type
};
});
this.msg = 'downloadFinish';
this.state = 'complete';
return;
} catch (e) {
this.state = 'invalid';
throw e;
}
}
}
async function saveFile(file) {
return new Promise(function(resolve, reject) {
const reader = new FileReader();
const dataView = new DataView(file.plaintext);
const blob = new Blob([dataView], { type: file.type });
if (window.navigator.msSaveBlob) {
window.navigator.msSaveBlob(blob, file.name);
return resolve();
}
reader.addEventListener('loadend', function() {
if (reader.error) {
return reject(reader.error);
}
if (reader.result) {
const a = document.createElement('a');
a.href = reader.result;
a.download = file.name;
document.body.appendChild(a);
a.click();
}
resolve();
});
reader.readAsDataURL(blob);
});
}