add try catch to video.foreach and video.pause

This commit is contained in:
Eudes Inácio
2024-02-05 10:29:42 +01:00
parent a873e192ee
commit d7980908dd
7 changed files with 137 additions and 56 deletions
+53
View File
@@ -0,0 +1,53 @@
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ChunksService {
chunkSize: number
private file: File
constructor() {
}
get totalChunks () {
return Math.ceil(this.file.size / this.chunkSize);
}
// Function to read a chunk of the file
readChunk(start: number, end: number): Promise<ArrayBuffer> {
const file = this.file
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
if (reader.result instanceof ArrayBuffer) {
resolve(reader.result);
} else {
reject(new Error("Failed to read chunk"));
}
};
reader.readAsArrayBuffer(file.slice(start, end));
});
}
setFile(file) {
this.file = file
}
async getChunks(i: number,chunkSize: number) {
i--
if(i < this.totalChunks) {
const start = i * chunkSize;
const end = Math.min(start + chunkSize, this.file.size);
const chunk = await this.readChunk(start, end);
return chunk
}
}
}