TeaSpeak-Client/modules/renderer/connection/FileTransfer.ts

181 lines
5.6 KiB
TypeScript
Raw Normal View History

import * as native from "tc-native/connection";
2019-10-25 19:51:40 -04:00
import * as path from "path";
2020-05-04 05:54:35 -04:00
import {DownloadKey, DownloadTransfer, UploadKey, UploadTransfer} from "tc-shared/file/FileManager";
import {base64_encode_ab, str2ab8} from "tc-shared/utils/buffers";
2020-05-04 05:54:35 -04:00
import {set_transfer_provider, TransferKey, TransferProvider} from "tc-shared/file/FileManager";
2019-10-25 19:51:40 -04:00
class NativeFileDownload implements DownloadTransfer {
readonly key: DownloadKey;
private _handle: native.ft.NativeFileTransfer;
private _buffer: Uint8Array;
2019-10-25 19:51:40 -04:00
private _result: Promise<void>;
private _response: Response;
2019-10-25 19:51:40 -04:00
private _result_success: () => any;
private _result_error: (error: any) => any;
2019-10-25 19:51:40 -04:00
constructor(key: DownloadKey) {
this.key = key;
this._buffer = new Uint8Array(key.total_size);
this._handle = native.ft.spawn_connection({
client_transfer_id: key.client_transfer_id,
server_transfer_id: key.server_transfer_id,
2019-10-25 19:51:40 -04:00
remote_address: key.peer.hosts[0],
remote_port: key.peer.port,
2019-10-25 19:51:40 -04:00
transfer_key: key.key,
2019-10-25 19:51:40 -04:00
object: native.ft.download_transfer_object_from_buffer(this._buffer.buffer)
});
}
2019-10-25 19:51:40 -04:00
get_key(): DownloadKey {
return this.key;
}
2019-10-25 19:51:40 -04:00
async request_file(): Promise<Response> {
if(this._response)
return this._response;
2019-10-25 19:51:40 -04:00
try {
await (this._result || this._start_transfer());
} catch(error) {
throw error;
}
2019-10-25 19:51:40 -04:00
if(this._response)
return this._response;
2019-10-25 19:51:40 -04:00
const buffer = this._buffer.buffer.slice(this._buffer.byteOffset, this._buffer.byteOffset + Math.min(64, this._buffer.byteLength));
2019-10-25 19:51:40 -04:00
/* may another task has been stepped by and already set the response */
return this._response || (this._response = new Response(this._buffer, {
status: 200,
statusText: "success",
headers: {
"X-media-bytes": base64_encode_ab(buffer)
2019-10-25 19:51:40 -04:00
}
}));
2019-10-25 19:51:40 -04:00
}
_start_transfer() : Promise<void> {
return this._result = new Promise((resolve, reject) => {
this._result_error = (error) => {
this._result_error = undefined;
this._result_success = undefined;
reject(error);
};
this._result_success = () => {
this._result_error = undefined;
this._result_success = undefined;
resolve();
};
this._handle.callback_failed = this._result_error;
this._handle.callback_finished = aborted => {
if(aborted)
this._result_error("aborted");
else
this._result_success();
};
this._handle.start();
});
}
}
2019-10-25 19:51:40 -04:00
class NativeFileUpload implements UploadTransfer {
readonly transfer_key: UploadKey;
private _handle: native.ft.NativeFileTransfer;
2019-10-25 19:51:40 -04:00
private _result: Promise<void>;
2019-10-25 19:51:40 -04:00
private _result_success: () => any;
private _result_error: (error: any) => any;
2019-10-25 19:51:40 -04:00
constructor(key: UploadKey) {
this.transfer_key = key;
}
2019-10-25 19:51:40 -04:00
async put_data(data: BlobPart | File) : Promise<void> {
if(this._result) {
await this._result;
return;
}
2019-10-25 19:51:40 -04:00
let buffer: native.ft.FileTransferSource;
2019-10-25 19:51:40 -04:00
if(data instanceof File) {
if(data.size != this.transfer_key.total_size)
throw "invalid size";
2019-10-25 19:51:40 -04:00
buffer = native.ft.upload_transfer_object_from_file(path.dirname(data.path), data.name);
} else if(typeof(data) === "string") {
if(data.length != this.transfer_key.total_size)
throw "invalid size";
2019-10-25 19:51:40 -04:00
buffer = native.ft.upload_transfer_object_from_buffer(str2ab8(data));
} else {
let buf = <BufferSource>data;
if(buf.byteLength != this.transfer_key.total_size)
throw "invalid size";
2019-10-25 19:51:40 -04:00
if(ArrayBuffer.isView(buf))
buf = buf.buffer.slice(buf.byteOffset);
2019-10-25 19:51:40 -04:00
buffer = native.ft.upload_transfer_object_from_buffer(buf);
2019-10-25 19:51:40 -04:00
}
this._handle = native.ft.spawn_connection({
client_transfer_id: this.transfer_key.client_transfer_id,
server_transfer_id: this.transfer_key.server_transfer_id,
remote_address: this.transfer_key.peer.hosts[0],
remote_port: this.transfer_key.peer.port,
transfer_key: this.transfer_key.key,
object: buffer
});
await (this._result = new Promise((resolve, reject) => {
this._result_error = (error) => {
this._result_error = undefined;
this._result_success = undefined;
reject(error);
};
this._result_success = () => {
this._result_error = undefined;
this._result_success = undefined;
resolve();
};
this._handle.callback_failed = this._result_error;
this._handle.callback_finished = aborted => {
if(aborted)
this._result_error("aborted");
else
this._result_success();
};
this._handle.start();
}));
2019-10-25 19:51:40 -04:00
}
get_key(): UploadKey {
return this.transfer_key;
2019-10-25 19:51:40 -04:00
}
}
2019-10-25 19:51:40 -04:00
2020-05-04 05:54:35 -04:00
set_transfer_provider(new class implements TransferProvider {
spawn_upload_transfer(key: TransferKey): UploadTransfer {
return new NativeFileUpload(key);
}
2019-10-25 19:51:40 -04:00
2020-05-04 05:54:35 -04:00
spawn_download_transfer(key: TransferKey): DownloadTransfer {
return new NativeFileDownload(key);
}
});