Skip to content

Commit

Permalink
feat(upload-core): adding encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
Christie Baker authored and Christie Baker committed Apr 12, 2018
1 parent a21e423 commit e08b1b2
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions packages/upload-core/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ class Upload {
this.status = 'pending';
this.timeoutID = undefined;
this.error = null;
this.waitForPw = true;
}

inStatusCategory(status, category) {
return status >= category && status < category + 100;
}

scan() {
scan(data) {
if (!this.isValidFile()) {
return;
}
Expand All @@ -54,6 +55,9 @@ class Upload {
xhr.setRequestHeader('X-Client-ID', this.options.clientId);
xhr.setRequestHeader('X-Availity-Customer-ID', this.options.customerId);
xhr.setRequestHeader('X-XSRF-TOKEN', this.getToken());
if (data) {
xhr.setRequestHeader(data.header, data.value);
}

xhr.onload = () => {
if (!this.inStatusCategory(xhr.status, 200)) {
Expand All @@ -69,15 +73,25 @@ class Upload {
this.percentage = this.getPercentage();

const result = this.getResult(xhr);

if (result.status === 'rejected') {
this.setError(result.status, result.message);
clearTimeout(this.timeoutId);
return;
}

if (result.status === 'encrypted') {
if (this.waitForPw) {
this.setError(result.status, result.message);
clearTimeout(this.timeoutId);
return;
}
}

if (result.status === 'accepted') {
this.percentage = 100;
this.status = result.status;
this.errorMessage = null;
const references = xhr.getResponseHeader('references');
if (references) {
this.references = JSON.parse(references);
Expand Down Expand Up @@ -165,6 +179,7 @@ class Upload {
if (result.status === 'accepted') {
this.percentage = 100;
this.status = result.status;
this.errorMessage = null;
const references = xhr.getResponseHeader('references');
if (references) {
this.references = JSON.parse(references);
Expand All @@ -182,11 +197,18 @@ class Upload {
},
});
this.upload = upload;
this.id = this.upload.options.fingerprint(this.file);
this.id = this.upload.options
.fingerprint(this.file)
.replace(/[^a-zA-Z0-9-]/g, '');

upload.start();
}

sendPassword(pw) {
this.waitForPw = false;
this.scan({ header: 'Encryption-Password', value: pw });
}

isValidFile() {
if (this.options.fileTypes) {
if (!this.file.name) {
Expand Down Expand Up @@ -219,15 +241,37 @@ class Upload {
}
const scanResult = xhr.getResponseHeader('AV-Scan-Result');
const uploadResult = xhr.getResponseHeader('Upload-Result');
const decryptResult = xhr.getResponseHeader('Decryption-Result');
const msg = xhr.getResponseHeader('Upload-Message');
if (scanResult === 'rejected') {
return { status: scanResult, message: msg || 'Failed AV scan' };
}

if (uploadResult === 'rejected') {
this.waitForPw = true;
if (decryptResult === 'rejected') {
return {
status: uploadResult,
message: msg || 'Maximum password attempts reached',
};
}
return { status: uploadResult, message: msg || 'File upload rejected' };
}

if (uploadResult === 'encrypted') {
// needs pw, isDecrypting, isScanning
if (!this.waitForPw) {
return { status: 'pending', message: msg || '' };
}
if (decryptResult === 'rejected') {
return { status: uploadResult, message: msg || 'Incorrect password' };
}
return {
status: uploadResult,
message: msg || 'Encrypted files require a password',
};
}

if (scanResult === 'accepted' && uploadResult === 'accepted') {
return { status: 'accepted', message: msg || '' };
}
Expand All @@ -244,7 +288,7 @@ class Upload {
}

hasError() {
if (this.errorMessage) {
if (this.errorMessage && this.status !== 'encrypted') {
return true;
}
return false;
Expand Down

0 comments on commit e08b1b2

Please sign in to comment.