moved saveFile from utils to fileReceiver
This commit is contained in:
parent
12443db891
commit
e4b98fe65a
|
@ -5,8 +5,7 @@ import {
|
|||
delay,
|
||||
fadeOut,
|
||||
openLinksInNewTab,
|
||||
percent,
|
||||
saveFile
|
||||
percent
|
||||
} from './utils';
|
||||
import * as metrics from './metrics';
|
||||
|
||||
|
@ -170,12 +169,11 @@ export default function(state, emitter) {
|
|||
try {
|
||||
const start = Date.now();
|
||||
metrics.startedDownload({ size: file.size, ttl: file.ttl });
|
||||
const f = await state.transfer.download();
|
||||
await state.transfer.download();
|
||||
const time = Date.now() - start;
|
||||
const speed = size / (time / 1000);
|
||||
await delay(1000);
|
||||
await fadeOut('.page');
|
||||
saveFile(f);
|
||||
state.storage.totalDownloads += 1;
|
||||
state.transfer.reset();
|
||||
metrics.completedDownload({ size, time, speed });
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
|
27
app/utils.js
27
app/utils.js
|
@ -141,32 +141,6 @@ function fadeOut(selector) {
|
|||
return delay(300);
|
||||
}
|
||||
|
||||
function saveFile(file) {
|
||||
const reader = new FileReader();
|
||||
const dataView = new DataView(file.plaintext);
|
||||
const blob = new Blob([dataView], { type: file.type });
|
||||
|
||||
if (window.navigator.msSaveBlob) {
|
||||
return window.navigator.msSaveBlob(blob, file.name);
|
||||
}
|
||||
reader.addEventListener('loadend', function() {
|
||||
if (reader.result) {
|
||||
const a = document.createElement('a');
|
||||
a.href = reader.result;
|
||||
a.download = file.name;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
return;
|
||||
}
|
||||
if (reader.error) {
|
||||
console.error(reader.error);
|
||||
window.location.href = '/error';
|
||||
//TODO
|
||||
}
|
||||
});
|
||||
reader.readAsDataURL(blob);
|
||||
}
|
||||
|
||||
function openLinksInNewTab(links, should = true) {
|
||||
links = links || Array.from(document.querySelectorAll('a:not([target])'));
|
||||
if (should) {
|
||||
|
@ -195,6 +169,5 @@ module.exports = {
|
|||
b64ToArray,
|
||||
canHasSend,
|
||||
isFile,
|
||||
saveFile,
|
||||
openLinksInNewTab
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue