This commit is contained in:
Peter Maquiran
2024-03-26 12:03:30 +01:00
parent 0b876e516b
commit ca0b968abe
32 changed files with 1016 additions and 794 deletions
@@ -3,7 +3,7 @@ import { ok, err, Result } from 'neverthrow';
import { ObjectMergeNotification } from 'src/app/services/socket-connection-mcr.service';
import { CMAPIService } from "src/app/shared/repository/CMAPI/cmapi.service"
import { DomSanitizer } from '@angular/platform-browser';
import { v4 as uuidv4 } from 'uuid'
export enum UploadError {
@@ -26,7 +26,7 @@ export class UploadStreamingService {
}
}
class UploadFileUseCase {
export class UploadFileUseCase {
CMAPIService: CMAPIService = window["CMAPIAPIRepository"]
constructor() {}
async execute(PublicationAttachmentEntity: PublicationAttachmentEntity): Promise<Result<true, IOUploadError >> {
@@ -168,7 +168,7 @@ export class PublicationAttachmentEntity {
this.toUpload = true
}
setChunkManger (chunks: Chunks) {
setChunkManger (chunks: Chunks | ChunksBase64) {
this.chucksManager = new ChucksManager({chunks})
}
get hasChunkManger() {
@@ -178,6 +178,10 @@ export class PublicationAttachmentEntity {
get hasChunkManager() {
return this.chucksManager != null
}
get hasBlob() {
return this.blobFile
}
}
interface IPublicationFormModelEntity {
@@ -210,6 +214,7 @@ export class PublicationFormModel implements IPublicationFormModelEntity {
Files: PublicationAttachmentEntity[] = []
hasSet = false
send = false
setData(data: IPublicationFormModelEntity) {
if(data.Files) {
@@ -224,12 +229,15 @@ export class PublicationFormModel implements IPublicationFormModelEntity {
export class PublicationFormMV {
readonly id = uuidv4()
private UploadFileUseCase = new UploadFileUseCase()
form = new PublicationFormModel()
ObjectMergeNotification = new ObjectMergeNotification()
totalPercentage = 0
constructor() {
// this.ObjectMergeNotification.connect();
this.ObjectMergeNotification.connect();
window['upload-header-set-add'](this.id, this.totalPercentage)
}
setDataToFrom(data: IPublicationFormModelEntity) {
@@ -245,9 +253,11 @@ export class PublicationFormMV {
const mergeRequest = await this.ObjectMergeNotification.socket.commit(PublicationAttachmentEntity.chucksManager.path)
if(mergeRequest.isOk()) {
console.log("commit")
PublicationAttachmentEntity.chucksManager.contentSetReady()
return true
} else {
console.log('no commit')
return false
}
}
@@ -257,20 +267,31 @@ export class PublicationFormMV {
return new Promise(async (resolve, reject)=> {
if(!PublicationAttachmentEntity.hasChunkManger) {
const fileBlob = PublicationAttachmentEntity.blobFile;
const fileChunks = new Chunks({chunkSize: 50 })
fileChunks.setFile(fileBlob)
PublicationAttachmentEntity.setChunkManger(fileChunks)
if(PublicationAttachmentEntity.hasBlob) {
const fileBlob = PublicationAttachmentEntity.blobFile;
const fileChunks = new Chunks({chunkSize: 50 })
fileChunks.setFile(fileBlob)
PublicationAttachmentEntity.setChunkManger(fileChunks)
} else {
const Base64 = PublicationAttachmentEntity.Base64;
const fileChunks = new ChunksBase64({chunkSize: 50 })
fileChunks.setFile(Base64)
PublicationAttachmentEntity.setChunkManger(fileChunks)
}
PublicationAttachmentEntity.chucksManager.updateTotalPercentageTrigger = () => {
this.uploadPercentage()
}
} else if(PublicationAttachmentEntity.chucksManager.doneUpload) {
return resolve(true)
}
let attemp = 0;
let result: Result<true, IOUploadError>
if( PublicationAttachmentEntity.chucksManager.isUploading == false) {
if( PublicationAttachmentEntity.chucksManager.isUploading == false && PublicationAttachmentEntity.chucksManager.doneUpload == false) {
do {
attemp++
@@ -293,7 +314,7 @@ export class PublicationFormMV {
}
} else if ( PublicationAttachmentEntity.chucksManager.contentReady == false) {
console.log("try to send again")
return await resolve(this.commit(PublicationAttachmentEntity))
} else {
@@ -311,6 +332,8 @@ export class PublicationFormMV {
// this.ObjectMergeNotification.socket.registerWhenConnected(() => {
const videosFiles = this.getVideoFiles()
window['upload-header-set-percentage'](this.id, 1)
const videosFilesToUploads = videosFiles.filter( e => e.FileType == "video" && e.toUpload && e.blobFile)
const Promises: Promise<any>[] = []
@@ -328,8 +351,10 @@ export class PublicationFormMV {
if (allPromisesResolvedSuccessfully) {
console.log('All promises resolved successfully.');
window['upload-header-set-remove'](this.id);
resolve(true)
} else {
window['upload-header-set-remove'](this.id);
resolve(false)
console.log('Some promises failed to resolve successfully.');
}
@@ -344,22 +369,26 @@ export class PublicationFormMV {
}
get uploadPercentage() {
uploadPercentage() {
const videosFiles = this.getVideoFiles()
const percentageArray = videosFiles.map((e) => e.chucksManager.calculatePercentage())
// Check if the array is not empty
if (percentageArray.length === 0) {
return null;
window['upload-header-set-percentage'](this.id, this.totalPercentage)
return 0;
} else {
let sum = percentageArray.reduce((acc, percentage) => acc + percentage, 0);
// Calculate the average percentage
let averagePercentage = sum / percentageArray.length;
this.totalPercentage = averagePercentage
window['upload-header-set-percentage'](this.id, this.totalPercentage)
return averagePercentage;
}
let sum = percentageArray.reduce((acc, percentage) => acc + percentage, 0);
// Calculate the average percentage
let averagePercentage = sum / percentageArray.length;
return averagePercentage;
}
}
@@ -405,6 +434,51 @@ export class Chunks {
}
export class ChunksBase64 {
chunkSize: number
private base64: string
bytes: Uint8Array
constructor({chunkSize}) {
this.chunkSize = chunkSize * 1024
}
get totalChunks () {
return Math.ceil(this.bytes.length / this.chunkSize);
}
setFile(base64: string) {
this.base64 = base64
let utf8Encoder = new TextEncoder();
this.bytes = utf8Encoder.encode(base64);
}
// Function to read a chunk of the file
async readChunk(start: number, end: number) {
// Slice the last 1MB of bytes
let slicedBytes = this.bytes.slice(start, end);
// Convert the sliced bytes back to a string
let text = new TextDecoder().decode(slicedBytes);
return text
}
async getChunks(i: number): Promise<string> {
i--
if(i < this.totalChunks) {
const start = i * this.chunkSize;
const end = Math.min(start + this.chunkSize, this.bytes.length);
const chunk = await this.readChunk(start, end);
return chunk
}
}
}
interface IUploadResponse {
result: Result<any, Error>
attemp: number
@@ -424,6 +498,8 @@ export class ChucksManager {
needToCommit = true
subscribeToUseCaseResponse: Function[] = []
updateTotalPercentageTrigger = () => {}
getUploadPercentage() {
return this.uploadPercentage
}
@@ -483,6 +559,7 @@ export class ChucksManager {
setPercentage() {
const percentage: number = this.calculatePercentage()
console.log({percentage})
this.updateTotalPercentageTrigger()
this.uploadPercentage = percentage.toString()+"%"
}