whale-plus
Version:
A Component Library for Vue 3
94 lines (91 loc) • 2.65 kB
JavaScript
import { TaskQueue, Task } from '../upload-core/TaskQueue.mjs';
import { EventEmitter } from '../upload-core/EventEmitter.mjs';
import { MultiThreadSplitor } from './split/imp-work/MultiThreadSplitor.mjs';
import { TestRequest } from './testRequest.mjs';
class MaxFileUpload {
constructor(file) {
this.token = "";
this.uploadEmitter = new EventEmitter();
this.chunkSize = 5 * 1024 * 1024;
this.fileListener = null;
this.file = file;
this.requestStrategy = new TestRequest();
this.taskQueue = new TaskQueue(4);
this.splitStrategy = new MultiThreadSplitor(this.file, this.chunkSize);
}
setFileListener(listener) {
this.fileListener = listener;
return this;
}
setRequestStrategy(requestStrategy) {
this.requestStrategy = requestStrategy;
return this;
}
setSplitStrategy(splitStrategy) {
this.splitStrategy = splitStrategy;
return this;
}
setTaskQueue(taskQueue) {
this.taskQueue = taskQueue;
return this;
}
setFile(file) {
this.file = file;
return this;
}
setToken(token) {
this.token = token;
return this;
}
setChunkSize(chunkSize) {
this.chunkSize = chunkSize;
return this;
}
static with(file) {
return new MaxFileUpload(file);
}
async start() {
this.token = await this.requestStrategy.createFile(this.file);
this.splitStrategy.split();
this.splitStrategy.on("chunks", this.handleChunks.bind(this));
this.splitStrategy.on("wholeHash", this.handleWholeHash.bind(this));
this.uploadEmitter.on("end", (chunk) => {
var _a;
if ((_a = this.fileListener) == null ? void 0 : _a.uploadSuccessListener) {
this.fileListener.uploadSuccessListener(chunk);
}
});
}
handleChunks(chunks) {
var _a;
if ((_a = this.fileListener) == null ? void 0 : _a.chunkListener) {
this.fileListener.chunkListener(chunks);
}
chunks.forEach((chunk) => {
this.taskQueue.addAndStart(new Task(this.uploadChunk.bind(this), chunk));
});
}
async uploadChunk(chunk) {
const resp = await this.requestStrategy.patchHash(
this.token,
chunk.hash,
"chunk"
);
if (resp.hasFile) {
return;
}
await this.requestStrategy.uploadChunk(chunk, this.uploadEmitter);
}
async handleWholeHash(hash) {
var _a;
if ((_a = this.fileListener) == null ? void 0 : _a.chunkEndListener) {
this.fileListener.chunkEndListener(hash);
}
const resp = await this.requestStrategy.patchHash(this.token, hash, "file");
if (resp.hasFile) {
return;
}
}
}
export { MaxFileUpload };
//# sourceMappingURL=MaxFileUpload.mjs.map