2020-11-18 19:56:41 +01:00
|
|
|
import { SendData } from '../models/data/sendData';
|
|
|
|
|
|
|
|
import { SendRequest } from '../models/request/sendRequest';
|
|
|
|
|
|
|
|
import { SendResponse } from '../models/response/sendResponse';
|
|
|
|
|
|
|
|
import { Send } from '../models/domain/send';
|
|
|
|
import { SendFile } from '../models/domain/sendFile';
|
|
|
|
import { SendText } from '../models/domain/sendText';
|
|
|
|
import { SymmetricCryptoKey } from '../models/domain/symmetricCryptoKey';
|
|
|
|
|
2021-03-25 16:20:38 +01:00
|
|
|
import { FileUploadType } from '../enums/fileUploadType';
|
2020-11-18 19:56:41 +01:00
|
|
|
import { SendType } from '../enums/sendType';
|
|
|
|
|
|
|
|
import { SendView } from '../models/view/sendView';
|
|
|
|
|
|
|
|
import { ApiService } from '../abstractions/api.service';
|
|
|
|
import { CryptoService } from '../abstractions/crypto.service';
|
|
|
|
import { CryptoFunctionService } from '../abstractions/cryptoFunction.service';
|
2021-03-25 16:20:38 +01:00
|
|
|
import { FileUploadService } from '../abstractions/fileUpload.service';
|
2020-11-18 19:56:41 +01:00
|
|
|
import { I18nService } from '../abstractions/i18n.service';
|
|
|
|
import { SendService as SendServiceAbstraction } from '../abstractions/send.service';
|
|
|
|
import { StorageService } from '../abstractions/storage.service';
|
|
|
|
import { UserService } from '../abstractions/user.service';
|
|
|
|
|
|
|
|
import { Utils } from '../misc/utils';
|
2021-01-29 22:08:52 +01:00
|
|
|
import { CipherString } from '../models/domain';
|
2021-03-25 16:20:38 +01:00
|
|
|
import { ErrorResponse } from '../models/response';
|
2020-11-18 19:56:41 +01:00
|
|
|
|
|
|
|
const Keys = {
|
|
|
|
sendsPrefix: 'sends_',
|
|
|
|
};
|
|
|
|
|
|
|
|
export class SendService implements SendServiceAbstraction {
|
|
|
|
decryptedSendCache: SendView[];
|
|
|
|
|
|
|
|
constructor(private cryptoService: CryptoService, private userService: UserService,
|
2021-03-25 16:20:38 +01:00
|
|
|
private apiService: ApiService, private fileUploadService: FileUploadService,
|
|
|
|
private storageService: StorageService, private i18nService: I18nService,
|
|
|
|
private cryptoFunctionService: CryptoFunctionService) { }
|
2020-11-18 19:56:41 +01:00
|
|
|
|
|
|
|
clearCache(): void {
|
|
|
|
this.decryptedSendCache = null;
|
|
|
|
}
|
|
|
|
|
2021-01-29 22:08:52 +01:00
|
|
|
async encrypt(model: SendView, file: File | ArrayBuffer, password: string,
|
2020-11-18 19:56:41 +01:00
|
|
|
key?: SymmetricCryptoKey): Promise<[Send, ArrayBuffer]> {
|
|
|
|
let fileData: ArrayBuffer = null;
|
|
|
|
const send = new Send();
|
|
|
|
send.id = model.id;
|
|
|
|
send.type = model.type;
|
|
|
|
send.disabled = model.disabled;
|
2021-03-25 23:27:43 +01:00
|
|
|
send.hideEmail = model.hideEmail;
|
2020-11-18 19:56:41 +01:00
|
|
|
send.maxAccessCount = model.maxAccessCount;
|
|
|
|
if (model.key == null) {
|
|
|
|
model.key = await this.cryptoFunctionService.randomBytes(16);
|
|
|
|
model.cryptoKey = await this.cryptoService.makeSendKey(model.key);
|
|
|
|
}
|
|
|
|
if (password != null) {
|
|
|
|
const passwordHash = await this.cryptoFunctionService.pbkdf2(password, model.key, 'sha256', 100000);
|
|
|
|
send.password = Utils.fromBufferToB64(passwordHash);
|
|
|
|
}
|
|
|
|
send.key = await this.cryptoService.encrypt(model.key, key);
|
|
|
|
send.name = await this.cryptoService.encrypt(model.name, model.cryptoKey);
|
|
|
|
send.notes = await this.cryptoService.encrypt(model.notes, model.cryptoKey);
|
|
|
|
if (send.type === SendType.Text) {
|
|
|
|
send.text = new SendText();
|
|
|
|
send.text.text = await this.cryptoService.encrypt(model.text.text, model.cryptoKey);
|
|
|
|
send.text.hidden = model.text.hidden;
|
|
|
|
} else if (send.type === SendType.File) {
|
|
|
|
send.file = new SendFile();
|
|
|
|
if (file != null) {
|
2021-01-29 22:08:52 +01:00
|
|
|
if (file instanceof ArrayBuffer) {
|
|
|
|
const [name, data] = await this.encryptFileData(model.file.fileName, file, model.cryptoKey);
|
|
|
|
send.file.fileName = name;
|
|
|
|
fileData = data;
|
|
|
|
} else {
|
|
|
|
fileData = await this.parseFile(send, file, model.cryptoKey);
|
|
|
|
}
|
2020-11-18 19:56:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [send, fileData];
|
|
|
|
}
|
|
|
|
|
|
|
|
async get(id: string): Promise<Send> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const sends = await this.storageService.get<{ [id: string]: SendData; }>(
|
|
|
|
Keys.sendsPrefix + userId);
|
|
|
|
if (sends == null || !sends.hasOwnProperty(id)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Send(sends[id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAll(): Promise<Send[]> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const sends = await this.storageService.get<{ [id: string]: SendData; }>(
|
|
|
|
Keys.sendsPrefix + userId);
|
|
|
|
const response: Send[] = [];
|
|
|
|
for (const id in sends) {
|
|
|
|
if (sends.hasOwnProperty(id)) {
|
|
|
|
response.push(new Send(sends[id]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAllDecrypted(): Promise<SendView[]> {
|
|
|
|
if (this.decryptedSendCache != null) {
|
|
|
|
return this.decryptedSendCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasKey = await this.cryptoService.hasKey();
|
|
|
|
if (!hasKey) {
|
|
|
|
throw new Error('No key.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const decSends: SendView[] = [];
|
|
|
|
const promises: Promise<any>[] = [];
|
|
|
|
const sends = await this.getAll();
|
2021-02-04 16:49:23 +01:00
|
|
|
sends.forEach(send => {
|
|
|
|
promises.push(send.decrypt().then(f => decSends.push(f)));
|
2020-11-18 19:56:41 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
decSends.sort(Utils.getSortFunction(this.i18nService, 'name'));
|
|
|
|
|
|
|
|
this.decryptedSendCache = decSends;
|
|
|
|
return this.decryptedSendCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
async saveWithServer(sendData: [Send, ArrayBuffer]): Promise<any> {
|
2021-03-02 16:46:46 +01:00
|
|
|
const request = new SendRequest(sendData[0], sendData[1]?.byteLength);
|
2020-11-18 19:56:41 +01:00
|
|
|
let response: SendResponse;
|
|
|
|
if (sendData[0].id == null) {
|
|
|
|
if (sendData[0].type === SendType.Text) {
|
|
|
|
response = await this.apiService.postSend(request);
|
|
|
|
} else {
|
|
|
|
try {
|
2021-03-25 16:20:38 +01:00
|
|
|
const uploadDataResponse = await this.apiService.postFileTypeSend(request);
|
|
|
|
response = uploadDataResponse.sendResponse;
|
|
|
|
|
2021-03-29 15:18:07 +02:00
|
|
|
await this.fileUploadService.uploadSendFile(uploadDataResponse, sendData[0].file.fileName, sendData[1]);
|
2020-11-18 19:56:41 +01:00
|
|
|
} catch (e) {
|
2021-03-25 16:20:38 +01:00
|
|
|
if (e instanceof ErrorResponse && (e as ErrorResponse).statusCode === 404) {
|
|
|
|
response = await this.legacyServerSendFileUpload(sendData, request);
|
2021-03-26 22:57:07 +01:00
|
|
|
} else if (e instanceof ErrorResponse) {
|
|
|
|
throw new Error((e as ErrorResponse).getSingleMessage());
|
2020-11-18 19:56:41 +01:00
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sendData[0].id = response.id;
|
2021-02-08 21:55:32 +01:00
|
|
|
sendData[0].accessId = response.accessId;
|
2020-11-18 19:56:41 +01:00
|
|
|
} else {
|
|
|
|
response = await this.apiService.putSend(sendData[0].id, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const data = new SendData(response, userId);
|
|
|
|
await this.upsert(data);
|
|
|
|
}
|
|
|
|
|
2021-03-25 16:20:38 +01:00
|
|
|
/**
|
|
|
|
* @deprecated Mar 25 2021: This method has been deprecated in favor of direct uploads.
|
|
|
|
* This method still exists for backward compatibility with old server versions.
|
|
|
|
*/
|
|
|
|
async legacyServerSendFileUpload(sendData: [Send, ArrayBuffer], request: SendRequest): Promise<SendResponse>
|
|
|
|
{
|
|
|
|
const fd = new FormData();
|
|
|
|
try {
|
|
|
|
const blob = new Blob([sendData[1]], { type: 'application/octet-stream' });
|
|
|
|
fd.append('model', JSON.stringify(request));
|
|
|
|
fd.append('data', blob, sendData[0].file.fileName.encryptedString);
|
|
|
|
} catch (e) {
|
|
|
|
if (Utils.isNode && !Utils.isBrowser) {
|
|
|
|
fd.append('model', JSON.stringify(request));
|
|
|
|
fd.append('data', Buffer.from(sendData[1]) as any, {
|
|
|
|
filepath: sendData[0].file.fileName.encryptedString,
|
|
|
|
contentType: 'application/octet-stream',
|
|
|
|
} as any);
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return await this.apiService.postSendFileLegacy(fd);
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:56:41 +01:00
|
|
|
async upsert(send: SendData | SendData[]): Promise<any> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
let sends = await this.storageService.get<{ [id: string]: SendData; }>(
|
|
|
|
Keys.sendsPrefix + userId);
|
|
|
|
if (sends == null) {
|
|
|
|
sends = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (send instanceof SendData) {
|
|
|
|
const s = send as SendData;
|
|
|
|
sends[s.id] = s;
|
|
|
|
} else {
|
2021-02-04 16:49:23 +01:00
|
|
|
(send as SendData[]).forEach(s => {
|
2020-11-18 19:56:41 +01:00
|
|
|
sends[s.id] = s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.storageService.save(Keys.sendsPrefix + userId, sends);
|
|
|
|
this.decryptedSendCache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async replace(sends: { [id: string]: SendData; }): Promise<any> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
await this.storageService.save(Keys.sendsPrefix + userId, sends);
|
|
|
|
this.decryptedSendCache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async clear(userId: string): Promise<any> {
|
|
|
|
await this.storageService.remove(Keys.sendsPrefix + userId);
|
|
|
|
this.decryptedSendCache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(id: string | string[]): Promise<any> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const sends = await this.storageService.get<{ [id: string]: SendData; }>(
|
|
|
|
Keys.sendsPrefix + userId);
|
|
|
|
if (sends == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof id === 'string') {
|
|
|
|
if (sends[id] == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete sends[id];
|
|
|
|
} else {
|
2021-02-04 16:49:23 +01:00
|
|
|
(id as string[]).forEach(i => {
|
2020-11-18 19:56:41 +01:00
|
|
|
delete sends[i];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.storageService.save(Keys.sendsPrefix + userId, sends);
|
|
|
|
this.decryptedSendCache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteWithServer(id: string): Promise<any> {
|
|
|
|
await this.apiService.deleteSend(id);
|
|
|
|
await this.delete(id);
|
|
|
|
}
|
|
|
|
|
2020-12-30 22:23:52 +01:00
|
|
|
async removePasswordWithServer(id: string): Promise<any> {
|
|
|
|
const response = await this.apiService.putSendRemovePassword(id);
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const data = new SendData(response, userId);
|
|
|
|
await this.upsert(data);
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:56:41 +01:00
|
|
|
private parseFile(send: Send, file: File, key: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.readAsArrayBuffer(file);
|
2021-02-04 16:49:23 +01:00
|
|
|
reader.onload = async evt => {
|
2020-11-18 19:56:41 +01:00
|
|
|
try {
|
2021-01-29 22:08:52 +01:00
|
|
|
const [name, data] = await this.encryptFileData(file.name, evt.target.result as ArrayBuffer, key);
|
|
|
|
send.file.fileName = name;
|
|
|
|
resolve(data);
|
2020-11-18 19:56:41 +01:00
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
};
|
2021-02-04 16:49:23 +01:00
|
|
|
reader.onerror = evt => {
|
2020-11-18 19:56:41 +01:00
|
|
|
reject('Error reading file.');
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2021-01-29 22:08:52 +01:00
|
|
|
|
|
|
|
private async encryptFileData(fileName: string, data: ArrayBuffer,
|
|
|
|
key: SymmetricCryptoKey): Promise<[CipherString, ArrayBuffer]> {
|
|
|
|
const encFileName = await this.cryptoService.encrypt(fileName, key);
|
|
|
|
const encFileData = await this.cryptoService.encryptToBytes(data, key);
|
|
|
|
return [encFileName, encFileData];
|
|
|
|
}
|
2020-11-18 19:56:41 +01:00
|
|
|
}
|