mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
fix message and and updload
This commit is contained in:
@@ -78,6 +78,8 @@ export class PublicationFromMvService {
|
||||
this.form.send = true
|
||||
const upload = await this.uploadVideosFiles()
|
||||
|
||||
console.log('release chunks')
|
||||
|
||||
|
||||
const needChunk = this.needToUploadChunk()
|
||||
if(needChunk.length == 0 ) {
|
||||
@@ -120,7 +122,6 @@ export class PublicationFromMvService {
|
||||
this.httpErroHandle.httpStatusHandle(error)
|
||||
if (error.status == 404) {
|
||||
this.PublicationFolderService.deletePost(this.form.ProcessId, this.form.DocumentId)
|
||||
// this.goBack();
|
||||
} else {
|
||||
window['upload-header-set-retry'](this.id)
|
||||
}
|
||||
@@ -130,7 +131,6 @@ export class PublicationFromMvService {
|
||||
|
||||
|
||||
} else {
|
||||
window['upload-header-set-retry'](this.id)
|
||||
this.toastService._badRequest("É necessário adicionar uma imagem ou vídeo")
|
||||
}
|
||||
|
||||
@@ -152,6 +152,8 @@ export class PublicationFromMvService {
|
||||
window['upload-header-set-percentage'](this.id, 100)
|
||||
}
|
||||
|
||||
console.log('release chunk')
|
||||
|
||||
if(upload) {
|
||||
this.form.Files = this.form.Files.map((e:PublicationAttachmentEntity) => {
|
||||
if(e.FileType == 'video' && e.toUpload) {
|
||||
@@ -167,6 +169,12 @@ export class PublicationFromMvService {
|
||||
|
||||
return e
|
||||
})
|
||||
} else {
|
||||
window['upload-header-set-retry'](this.id)
|
||||
this.toastService._badRequest("ocorreu um erro ao enviar o ficheiro")
|
||||
return true
|
||||
// loader.remove()
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -181,14 +189,12 @@ export class PublicationFromMvService {
|
||||
publication.DocumentId = null;
|
||||
publication.ProcessId = this.folderId
|
||||
|
||||
|
||||
|
||||
try {
|
||||
|
||||
await this.publications.CreatePublication(publication.ProcessId, publication).toPromise()
|
||||
this.closeApp()
|
||||
if (this.publicationType == '1') {
|
||||
|
||||
if (this.publicationType == '1') {
|
||||
this.httpErroHandle.httpsSucessMessagge('Criar publicação')
|
||||
} else if (this.publicationType == '2') {
|
||||
this.httpErroHandle.httpsSucessMessagge('Criar publicação')
|
||||
} else if (this.publicationType == '3') {
|
||||
@@ -197,6 +203,7 @@ export class PublicationFromMvService {
|
||||
|
||||
// this.goBackToViewPublications.emit();
|
||||
window['upload-header-set-remove'](this.id);
|
||||
this.closeApp()
|
||||
this.publicationFolderService.getPublicationsIds(this.folderId)
|
||||
} catch (error) {
|
||||
window['upload-header-set-retry'](this.id)
|
||||
@@ -205,14 +212,6 @@ export class PublicationFromMvService {
|
||||
// loader.remove()
|
||||
}
|
||||
|
||||
} else {
|
||||
window['upload-header-set-retry'](this.id)
|
||||
this.toastService._badRequest("ocorreu um erro ao enviar o ficheiro")
|
||||
// loader.remove()
|
||||
}
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
this.toastService._badRequest("É necessário adicionar uma imagem ou vídeo")
|
||||
@@ -230,6 +229,7 @@ export class PublicationFromMvService {
|
||||
|
||||
closeApp() {
|
||||
if (window["sharedContent"]) {
|
||||
alert('close android')
|
||||
|
||||
if (this.platform.is('android')) {
|
||||
App.exitApp()
|
||||
@@ -275,7 +275,7 @@ export class PublicationFromMvService {
|
||||
if(PublicationAttachmentEntity.hasBlob) {
|
||||
|
||||
const fileBlob = PublicationAttachmentEntity.blobFile;
|
||||
const fileChunks = new Chunks({chunkSize: 1000 })
|
||||
const fileChunks = new Chunks({chunkSize: 50 })
|
||||
fileChunks.setFile(fileBlob)
|
||||
PublicationAttachmentEntity.setChunkManger(fileChunks)
|
||||
} else {
|
||||
@@ -322,6 +322,7 @@ export class PublicationFromMvService {
|
||||
|
||||
} else {
|
||||
console.log('already uploading')
|
||||
resolve(true)
|
||||
}
|
||||
|
||||
})
|
||||
@@ -347,6 +348,8 @@ export class PublicationFromMvService {
|
||||
Promises.push(promise)
|
||||
}
|
||||
|
||||
console.log('Promises', Promises)
|
||||
|
||||
// Use Promise.all to wait for all promises to resolve
|
||||
Promise.all(Promises)
|
||||
.then((results) => {
|
||||
|
||||
@@ -227,171 +227,6 @@ 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();
|
||||
window['upload-header-set-add'](this.id, this.totalPercentage)
|
||||
}
|
||||
|
||||
setDataToFrom(data: IPublicationFormModelEntity) {
|
||||
this.form.setData(data)
|
||||
}
|
||||
|
||||
private getVideoFiles() {
|
||||
return this.form.Files.filter( x => x.FileType == 'video')
|
||||
}
|
||||
|
||||
async commit(PublicationAttachmentEntity: PublicationAttachmentEntity) {
|
||||
PublicationAttachmentEntity.chucksManager.doneChunkUpload()
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
private upload(PublicationAttachmentEntity: PublicationAttachmentEntity) {
|
||||
|
||||
return new Promise(async (resolve, reject)=> {
|
||||
|
||||
if(!PublicationAttachmentEntity.hasChunkManger) {
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let attemp = 0;
|
||||
let result: Result<true, IOUploadError>
|
||||
|
||||
if( PublicationAttachmentEntity.chucksManager.isUploading == false && PublicationAttachmentEntity.chucksManager.doneUpload == false) {
|
||||
|
||||
do {
|
||||
attemp++
|
||||
|
||||
PublicationAttachmentEntity.chucksManager.clearManualRetry()
|
||||
PublicationAttachmentEntity.chucksManager.setUploading()
|
||||
result = await this.UploadFileUseCase.execute(PublicationAttachmentEntity)
|
||||
PublicationAttachmentEntity.chucksManager.clearUploading()
|
||||
|
||||
} while (attemp<3 && result.isErr() && result.error == 'slow')
|
||||
|
||||
|
||||
if(result.isErr()) {
|
||||
PublicationAttachmentEntity.chucksManager.setManualRetry()
|
||||
resolve(false)
|
||||
} else {
|
||||
|
||||
return await resolve(this.commit(PublicationAttachmentEntity))
|
||||
|
||||
}
|
||||
|
||||
} else if ( PublicationAttachmentEntity.chucksManager.contentReady == false) {
|
||||
console.log("try to send again")
|
||||
return await resolve(this.commit(PublicationAttachmentEntity))
|
||||
|
||||
} else {
|
||||
console.log('already uploading')
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
uploadVideosFiles(): Promise<Boolean> {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
// 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>[] = []
|
||||
|
||||
for(const file of videosFilesToUploads) {
|
||||
const promise = this.upload(file)
|
||||
Promises.push(promise)
|
||||
}
|
||||
|
||||
// Use Promise.all to wait for all promises to resolve
|
||||
Promise.all(Promises)
|
||||
.then((results) => {
|
||||
// Check if every promise resolved successfully
|
||||
const allPromisesResolvedSuccessfully = results.every((result) => result == true);
|
||||
|
||||
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.');
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
resolve(false)
|
||||
console.error('An error occurred while resolving promises:', error);
|
||||
});
|
||||
//})
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
uploadPercentage() {
|
||||
|
||||
const videosFiles = this.getVideoFiles()
|
||||
const percentageArray = videosFiles.map((e) => e.chucksManager.calculatePercentage())
|
||||
|
||||
// Check if the array is not empty
|
||||
if (percentageArray.length === 0) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class Chunks {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user