Files
doneit-web/src/app/shared/publication/new-publication/new-publication.page.ts
T

1205 lines
40 KiB
TypeScript
Raw Normal View History

2021-03-15 12:06:06 +01:00
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { PublicationsService } from 'src/app/services/publications.service';
import { Publication } from 'src/app/models/publication';
import { PhotoService } from 'src/app/services/photo.service';
2021-06-15 15:09:20 +01:00
import { ToastService } from 'src/app/services/toast.service';
2021-07-06 12:26:45 +01:00
import { FormControl, FormGroup, Validators } from '@angular/forms';
2021-11-09 18:06:10 +01:00
import { ThemeService } from 'src/app/services/theme.service';
2023-11-10 15:37:12 +01:00
import { Camera, CameraResultType, CameraSource } from '@capacitor/camera';
2023-02-27 09:34:36 +01:00
import { HttpErrorHandle } from 'src/app/services/http-error-handle.service';
2023-08-18 17:37:11 +01:00
import { PublicationFolderService } from 'src/app/store/publication-folder.service';
2023-11-10 15:37:12 +01:00
import { FilePicker } from '@capawesome/capacitor-file-picker';
2023-11-29 12:17:52 +01:00
import { checkFileTypeService } from 'src/app/services/checkFileType.service';
import { FileValidatorService } from "src/app/services/file/file-validator.service"
2024-01-06 20:37:49 +01:00
import { MiddlewareServiceService } from "src/app/shared/API/middleware/middleware-service.service"
import { LakefsRepositoryService } from '../../repository/lakefs/lakefs-repository.service';
2024-01-09 11:14:27 +01:00
import { ok, err, Result } from 'neverthrow';
2024-01-06 20:37:49 +01:00
enum ActionType {
newRapid = "1",
new = "2",
edit = "3"
}
2023-08-18 17:37:11 +01:00
2021-03-15 12:06:06 +01:00
@Component({
selector: 'app-new-publication',
templateUrl: './new-publication.page.html',
styleUrls: ['./new-publication.page.scss'],
})
export class NewPublicationPage implements OnInit {
showLoader: boolean;
2021-04-06 17:39:27 +01:00
pub: Publication = new Publication();
2021-03-15 12:06:06 +01:00
2023-11-10 15:37:12 +01:00
publicationTitle: string;
imgUrl: any;
Defaultimage: any = '';
2021-03-15 12:06:06 +01:00
2021-07-06 12:26:45 +01:00
Form: FormGroup;
validateFrom = false
2021-03-15 12:06:06 +01:00
2023-08-24 22:15:56 +01:00
@Input() publication!: Publication;
2021-03-15 12:06:06 +01:00
@Input() publicationType: string;
2023-11-10 15:37:12 +01:00
@Input() folderId: string;
@Input() documentId: string;
2023-08-24 22:15:56 +01:00
2023-11-10 15:37:12 +01:00
@Output() closeDesktopComponent = new EventEmitter<any>();
2021-03-17 10:03:39 +01:00
@Output() openPublicationDetails = new EventEmitter<any>();
@Output() goBackToViewPublications = new EventEmitter<any>();
2023-11-10 15:37:12 +01:00
@Output() goBacktoPublicationDetails = new EventEmitter<any>();
2021-11-09 10:39:14 +01:00
2023-11-10 15:37:12 +01:00
guestPicture: any;
2021-03-15 12:06:06 +01:00
2023-11-10 15:37:12 +01:00
capturedImage: any = '';
capturedImageTitle: any = '';
fileType: string;
filecontent: boolean;
captureContent: any;
2024-01-06 20:37:49 +01:00
seletedContent: PublicationAttachmentEntity[] = []
2023-11-29 12:17:52 +01:00
displayLimit = 4;
2023-12-06 17:01:00 +01:00
filesSizeSum = 0;
2021-03-15 12:06:06 +01:00
2024-01-06 20:37:49 +01:00
publicationFormMV = new PublicationFormMV()
2021-03-15 12:06:06 +01:00
constructor(
public photoService: PhotoService,
private publications: PublicationsService,
2021-06-15 15:09:20 +01:00
private toastService: ToastService,
2023-02-27 09:34:36 +01:00
public ThemeService: ThemeService,
2023-08-18 17:37:11 +01:00
private httpErroHandle: HttpErrorHandle,
2023-11-29 12:17:52 +01:00
public PublicationFolderService: PublicationFolderService,
2023-11-29 16:06:56 +01:00
public checkFileType: checkFileTypeService,
2024-01-06 20:37:49 +01:00
private FileValidatorService: FileValidatorService,
private MiddlewareServiceService: MiddlewareServiceService,
private LakefsRepositoryService: LakefsRepositoryService
2021-08-23 16:06:05 +01:00
) {
this.publicationTitle = 'Nova Publicação';
2024-01-06 20:37:49 +01:00
2023-11-29 12:17:52 +01:00
if (this.publication) {
this.seletedContent = this.publication.Files;
this.filecontent = true;
}
2021-08-23 16:06:05 +01:00
}
2021-03-15 12:06:06 +01:00
ngOnInit() {
2023-08-24 22:15:56 +01:00
2023-11-29 12:17:52 +01:00
this.publication = {
DateIndex: "",
DocumentId: 0,
ProcessId: "",
Title: "",
Message: "",
DatePublication: "",
Files: [],
OriginalFileName: "",
}
this.pub = this.publication;
2021-03-15 12:06:06 +01:00
this.setTitle();
2023-08-24 22:15:56 +01:00
this.setData()
2021-03-15 12:06:06 +01:00
}
2021-04-08 11:55:44 +01:00
2023-08-24 22:15:56 +01:00
setData() {
2023-11-10 15:37:12 +01:00
if (!this.publicationType) {
2023-08-24 22:15:56 +01:00
setTimeout(() => {
this.setData()
}, 500)
2023-08-18 17:37:11 +01:00
} else {
2023-08-28 17:06:16 +01:00
// this.pub = this.publication
// this.publication = null
this.getPublicationDetail()
2023-08-18 17:37:11 +01:00
}
2021-04-08 11:55:44 +01:00
}
2023-08-28 17:06:16 +01:00
getPublicationDetail() {
2023-11-29 12:17:52 +01:00
console.log('edit 1', this.publicationType)
2024-01-06 20:37:49 +01:00
if (this.publicationType != ActionType.new) {
2023-08-28 17:06:16 +01:00
this.showLoader = true;
2023-11-29 12:17:52 +01:00
this.publications.GetPublicationWithArrayOfFilesById(this.documentId).subscribe(res => {
2023-08-28 17:06:16 +01:00
this.publication = {
DateIndex: res.DateIndex,
DocumentId: res.DocumentId,
2023-11-10 15:37:12 +01:00
ProcessId: res.ProcessId,
Title: res.Title,
2023-08-28 17:06:16 +01:00
Message: res.Message,
DatePublication: res.DatePublication,
2023-11-29 12:17:52 +01:00
Files: res.Files,
2023-08-28 17:06:16 +01:00
OriginalFileName: res.OriginalFileName,
FileExtension: res.FileExtension,
}
2023-11-29 12:17:52 +01:00
console.log('edit',this.publication)
2023-08-28 17:06:16 +01:00
this.pub = this.publication;
2023-11-29 12:17:52 +01:00
this.seletedContent = this.publication.Files;
2023-08-28 17:06:16 +01:00
this.showLoader = false;
2023-11-29 12:17:52 +01:00
}, (error) => {
console.log(error)
2023-08-28 17:06:16 +01:00
this.showLoader = false;
2023-11-10 15:37:12 +01:00
this.goBack()
2023-08-28 17:06:16 +01:00
});
}
}
2021-11-09 10:39:14 +01:00
async takePicture() {
const capturedImage = await Camera.getPhoto({
2023-07-26 13:06:42 +01:00
quality: 50,
2021-11-09 10:39:14 +01:00
// allowEditing: true,
2022-02-11 15:08:27 +01:00
resultType: CameraResultType.Base64,
2021-11-09 10:39:14 +01:00
source: CameraSource.Camera
});
2023-11-10 15:37:12 +01:00
this.capturedImage = 'data:image/jpeg;base64,' + capturedImage.base64String;
this.capturedImageTitle = 'foto';
2023-11-29 16:06:56 +01:00
/* if(validation.isOk) { */
2023-11-10 15:37:12 +01:00
const compressedImage = await this.compressImageBase64(
this.capturedImage,
800, // maxWidth
800, // maxHeight
0.9 // quality
).then((picture) => {
2023-11-29 12:17:52 +01:00
let fileObject = {
FileBase64: picture,
2023-12-06 17:01:00 +01:00
FileExtension: this.removeTextBeforeSlash('jpeg','/')
2023-11-29 12:17:52 +01:00
}
2024-01-06 20:37:49 +01:00
const FileExtension = this.removeTextBeforeSlash('jpeg','/')
const newAttachment = new PublicationAttachmentEntity(
{
base64: picture,
extension: FileExtension,
OriginalFileName: "foto",
FileType: 'image'
}
)
2024-01-09 11:14:27 +01:00
newAttachment.needUpload()
2024-01-06 20:37:49 +01:00
this.seletedContent.push(newAttachment)
2023-11-10 15:37:12 +01:00
});
2023-11-29 16:06:56 +01:00
/* } else {
this.toastService._badRequest("Imagem inválida")
} */
2023-11-10 15:37:12 +01:00
}
2021-11-09 10:39:14 +01:00
2023-11-10 15:37:12 +01:00
async laodPicture() {
const capturedImage = await Camera.getPhoto({
quality: 90,
resultType: CameraResultType.Base64,
source: CameraSource.Photos
});
this.fileType = capturedImage.format
console.log(this.fileType)
this.capturedImage = 'data:image/jpeg;base64,' + capturedImage.base64String;
this.capturedImageTitle = 'foto';
const compressedImage = await this.compressImageBase64(
this.capturedImage,
800, // maxWidth
800, // maxHeight
0.9 // quality
).then((picture) => {
2023-11-29 12:17:52 +01:00
let fileObject = {
FileBase64: picture,
2023-12-06 17:01:00 +01:00
FileExtension: this.removeTextBeforeSlash('jpeg','/')
2023-11-29 12:17:52 +01:00
}
2024-01-06 20:37:49 +01:00
const FileExtension = this.removeTextBeforeSlash('jpeg','/')
const newAttachment = new PublicationAttachmentEntity(
{
base64: picture,
extension: FileExtension,
OriginalFileName: "foto",
FileType: 'image'
}
)
2024-01-09 11:14:27 +01:00
newAttachment.needUpload();
2024-01-06 20:37:49 +01:00
this.seletedContent.push(newAttachment)
2023-11-10 15:37:12 +01:00
});
}
async loadVideo() {
2021-11-09 16:38:26 +01:00
2023-11-10 15:37:12 +01:00
const result = await FilePicker.pickMedia
({
multiple: true,
});
2023-12-06 17:01:00 +01:00
2024-01-09 11:14:27 +01:00
result.files.forEach(async blobFile => {
if(this.checkFileType.checkFileType(blobFile.mimeType) == 'image' || this.checkFileType.checkFileType(blobFile.mimeType) == 'video') {
this.convertBlobToBase64(blobFile.blob).then((value) => {
this.filesSizeSum = this.filesSizeSum + blobFile.size
2023-12-06 17:01:00 +01:00
if(this.fileSizeToMB(this.filesSizeSum) <= 20) {
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
const FileExtension = this.removeTextBeforeSlash(blobFile.mimeType,'/')
2024-01-06 20:37:49 +01:00
const newAttachment = new PublicationAttachmentEntity(
{
base64: value,
extension: FileExtension,
2024-01-09 11:14:27 +01:00
blobFile: blobFile,
2024-01-06 20:37:49 +01:00
FileType: 'video'
}
)
2024-01-09 11:14:27 +01:00
newAttachment.needUpload()
2024-01-06 20:37:49 +01:00
this.seletedContent.push(newAttachment)
2023-11-29 12:17:52 +01:00
this.filecontent = true;
2023-12-06 17:01:00 +01:00
} else {
if(this.seletedContent.length === 0)
this.filesSizeSum = 0
this.httpErroHandle.validationMessagge('filessize');
}
2023-11-29 12:17:52 +01:00
})
2023-11-10 15:37:12 +01:00
} else {
2023-11-29 12:17:52 +01:00
this.httpErroHandle.validationMessagge('filetype');
2023-11-10 15:37:12 +01:00
}
2024-01-06 20:37:49 +01:00
2023-11-29 12:17:52 +01:00
});
2023-11-29 16:06:56 +01:00
/* this.capturedImage = 'data:image/jpeg;base64,' +capturedImage.base64String;
2023-08-15 10:15:08 +01:00
this.capturedImageTitle = 'foto';
const validation = await this.FileValidatorService.ValidateImage(this.capturedImage)
if(validation.isOk) {
const compressedImage = await this.compressImageBase64(
this.capturedImage,
800, // maxWidth
800, // maxHeight
0.9 // quality
).then((picture) => {
console.log('Selected: ', picture)
this.capturedImage = picture
});
} else {
2023-11-10 14:58:33 +01:00
this.toastService._badRequest("Imagem inválida")
2023-11-29 16:06:56 +01:00
} */
2023-11-10 15:37:12 +01:00
};
2021-11-09 16:38:26 +01:00
2021-07-06 12:26:45 +01:00
runValidation() {
2023-11-10 15:37:12 +01:00
this.validateFrom = true
2021-07-06 12:26:45 +01:00
}
injectValidation() {
this.Form = new FormGroup({
Subject: new FormControl(this.pub.Title, [
2023-11-10 15:37:12 +01:00
Validators.required,
2021-07-06 12:26:45 +01:00
// Validators.minLength(4)
2021-07-15 15:39:10 +01:00
]),
Message: new FormControl(this.pub.Message, [
2023-11-10 15:37:12 +01:00
Validators.required,
2021-07-15 15:39:10 +01:00
Validators.maxLength(1000)
2021-07-06 12:26:45 +01:00
])
})
}
2021-07-14 19:04:33 +01:00
async save() {
2021-07-06 12:26:45 +01:00
this.injectValidation()
this.runValidation()
2023-11-10 15:37:12 +01:00
if (this.Form.invalid) {
2023-02-27 09:34:36 +01:00
return false
} else {
2023-11-10 15:37:12 +01:00
2023-02-27 09:34:36 +01:00
}
2021-07-06 12:26:45 +01:00
2024-01-06 20:37:49 +01:00
if (this.publicationType == ActionType.edit) {
2023-11-10 15:37:12 +01:00
if (!this.publication?.OriginalFileName || !this.pub.OriginalFileName) {
2023-08-28 17:06:16 +01:00
2023-11-10 15:37:12 +01:00
if (this.pub?.OriginalFileName) {
console.log('this.pub', this.pub)
2023-08-28 17:06:16 +01:00
}
2023-11-10 15:37:12 +01:00
throw ('no this.publication.OriginalFileName')
2023-08-28 17:06:16 +01:00
}
2023-11-10 15:37:12 +01:00
2023-08-23 09:55:14 +01:00
const loader = this.toastService.loading()
2021-07-26 16:40:51 +01:00
2023-11-29 12:17:52 +01:00
if (this.seletedContent.length > 0) {
2021-07-26 16:40:51 +01:00
2021-03-15 12:06:06 +01:00
this.publication = {
DateIndex: this.publication.DateIndex,
2023-11-10 15:37:12 +01:00
DocumentId: this.publication.DocumentId,
ProcessId: this.publication.ProcessId,
2021-03-15 12:06:06 +01:00
Title: this.pub.Title,
Message: this.pub.Message,
DatePublication: this.publication.DatePublication,
2023-08-22 15:43:20 +01:00
OriginalFileName: this.publication.OriginalFileName || 'foto',
2023-11-29 12:17:52 +01:00
Files: this.seletedContent,
2021-03-15 12:06:06 +01:00
}
2023-08-24 22:15:56 +01:00
2023-11-29 12:17:52 +01:00
try {
const response = await this.publications.UpdatePublication(this.publication.ProcessId, this.publication).toPromise()
this.httpErroHandle.httpsSucessMessagge('Editar publicação')
console.log({ response })
this.goBack();
} catch (error) {
this.httpErroHandle.httpStatusHandle(error)
if (error.status == 404) {
this.PublicationFolderService.deletePost(this.publication.ProcessId, this.publication.DocumentId)
this.goBack();
}
} finally {
loader.remove()
2023-11-10 15:37:12 +01:00
}
2023-11-29 12:17:52 +01:00
} else {
2024-01-06 20:37:49 +01:00
}/*
2023-08-22 15:43:20 +01:00
else if (!this.PublicationFolderService.PublicationHasImage(this.publication)) { //
2023-11-10 15:37:12 +01:00
2021-03-15 12:06:06 +01:00
this.publication = {
DateIndex: this.publication.DateIndex,
2023-11-10 15:37:12 +01:00
DocumentId: this.publication.DocumentId,
ProcessId: this.publication.ProcessId,
2021-03-15 12:06:06 +01:00
Title: this.pub.Title,
Message: this.pub.Message,
DatePublication: this.publication.DatePublication,
2023-08-22 15:43:20 +01:00
OriginalFileName: this.publication.OriginalFileName,
2023-11-29 12:17:52 +01:00
Files: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgMAAAIJ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RSjQtNzy8v1VbbKcHpKQGDKAH/sVguq3GhabXeVFttpwSnpwQMogT8x8PjYzUuNE1nw+acUILTUwIGUQJq1bjQdHc/qjbZ7vQl+HV1PRqPZ/P5erN5fn4pyj+Ufy3/sfOS+M+gBAyiBNQOvmTNiUtwPxo/Pj1VD/Wp/K8+P6W+dErAIEpA7e7+wIPFKUuwWPb64VufyyEXTQkYRAmoTabTamhoenj88kt308lKMGjN/T5Ta1wuJWAQJaC2XK2qoaGp51fvD6cpwQ4/eSubVA/yYygBgygBtcenp2poaBpPJtVWW5ymBOvNsHtbiz7LtF0oJWAQJaBFNTQ0lQGl2mSLE5Tgq4GvU9mweqifQQkYRAlocdgla05Qgt0WWC7Wm2GXIi6FEjCIEtCiz32W/eecOEEJnp6fq217enl9/evXVfVoP4ASMIgS0GLa45xJ//Pyxy7BoAOUpj53zV4cJWAQJaDFat19KbXUotrqK8cuQXn8asNBvvdSwfXNbZ/PZyglYBAloMXzy0s1OjTdj/ouWXPsEux5THCMgbin8so/VwMtY3fnlOD9KQGDKAHtyiBVDRCV/vfvH7sExZbpJbb7xusE5XnXm4fq9ZQwdM720YcSMIgS0O6AS9acoAR73Dv0bT8p+Oo193/XWygBgygB7Ubj7iVren4TP0EJvhr4OpUNq4c6jcVyVb2SPy1X6+rvh1ICBlEC2s3m3dPybB/OPp2gBMXnCff++izGcAx95kfac1okJWAQJaBdn1F4Mp1
2023-08-24 22:15:56 +01:00
FileExtension: 'jpeg',
2021-03-15 12:06:06 +01:00
}
2021-11-09 10:39:14 +01:00
2021-07-26 15:19:03 +01:00
} else {
this.publication = {
DateIndex: this.publication.DateIndex,
2023-11-10 15:37:12 +01:00
DocumentId: this.publication.DocumentId,
ProcessId: this.publication.ProcessId,
2021-07-26 15:19:03 +01:00
Title: this.pub.Title,
Message: this.pub.Message,
DatePublication: this.publication.DatePublication,
2023-08-22 15:43:20 +01:00
OriginalFileName: this.publication.OriginalFileName,
2023-11-29 12:17:52 +01:00
Files: this.publication.Files,
2023-08-24 22:15:56 +01:00
FileExtension: this.publication.FileExtension,
2021-07-26 15:19:03 +01:00
}
2023-11-29 12:17:52 +01:00
} */
2023-08-18 17:37:11 +01:00
2021-03-15 12:06:06 +01:00
}
2021-07-12 11:13:29 +01:00
else {
2023-11-10 15:37:12 +01:00
let time = new Date()
2023-11-29 12:17:52 +01:00
if (this.seletedContent.length > 0) {
2023-11-10 15:37:12 +01:00
this.publication = {
DateIndex: time,
DocumentId: null,
ProcessId: this.folderId,
Title: this.pub.Title,
Message: this.pub.Message,
DatePublication: time,
OriginalFileName: this.capturedImageTitle || 'foto',
2023-11-29 12:17:52 +01:00
Files: this.seletedContent,
2023-11-10 15:37:12 +01:00
}
2021-11-09 10:39:14 +01:00
2024-01-09 11:14:27 +01:00
const loader = this.toastService.loading()
try {
await this.publications.CreatePublication(this.publication.ProcessId, this.publication).toPromise()
if (this.publicationTitle == '1') {
} else if (this.publicationTitle == '2') {
this.httpErroHandle.httpsSucessMessagge('Criar publicação')
} else if (this.publicationTitle == '3') {
this.httpErroHandle.httpsSucessMessagge('Editar publicação')
}
this.goBackToViewPublications.emit();
} catch (error) {
this.httpErroHandle.httpStatusHandle(error)
} finally {
loader.remove()
}
2023-11-10 15:37:12 +01:00
2021-07-12 11:13:29 +01:00
} else {
2024-01-06 20:37:49 +01:00
2021-05-25 16:12:40 +01:00
}
2021-03-15 12:06:06 +01:00
}
}
2023-11-10 15:37:12 +01:00
close() {
2021-03-17 10:03:39 +01:00
this.goBack();
2021-03-15 12:06:06 +01:00
}
2021-11-10 15:51:27 +01:00
2023-08-15 10:15:08 +01:00
clear() {
this.capturedImageTitle = '';
2023-11-29 12:17:52 +01:00
this.seletedContent = [];
2021-03-15 12:06:06 +01:00
}
2023-08-15 10:15:08 +01:00
deletePublicationImage() {
2023-11-10 15:37:12 +01:00
2023-11-29 12:17:52 +01:00
this.publication.Files = []
2023-08-15 10:15:08 +01:00
}
2023-11-10 15:37:12 +01:00
setTitle() {
2024-01-06 20:37:49 +01:00
if (this.publicationType == ActionType.newRapid) {
2021-03-15 12:06:06 +01:00
this.publicationTitle = 'Nova Publicação Rápida';
}
2024-01-06 20:37:49 +01:00
else if (this.publicationType == ActionType.new) {
2021-03-15 12:06:06 +01:00
this.publicationTitle = 'Nova Publicação';
}
2024-01-06 20:37:49 +01:00
else if (this.publicationType == ActionType.edit) {
2021-03-15 12:06:06 +01:00
this.publicationTitle = 'Editar Publicação';
2021-03-16 14:35:52 +01:00
this.pub = this.publication;
2021-03-15 12:06:06 +01:00
}
}
2023-11-10 15:37:12 +01:00
async goBack() {
2021-11-09 10:39:14 +01:00
2024-01-06 20:37:49 +01:00
if (this.publicationType == ActionType.new) {
2021-03-17 10:03:39 +01:00
this.goBackToViewPublications.emit();
} else {
2021-04-08 11:55:44 +01:00
this.goBackToViewPublications.emit();
//this.goBacktoPublicationDetails.emit();
2021-03-17 10:03:39 +01:00
}
2021-11-09 10:39:14 +01:00
}
async compressImageBase64(base64String: string, maxWidth: number, maxHeight: number, quality: number): Promise<string> {
return new Promise((resolve, reject) => {
const image = new (window as any).Image();
image.src = base64String;
2023-11-10 15:37:12 +01:00
image.onload = async () => {
const canvas = document.createElement('canvas');
let newWidth = image.width;
let newHeight = image.height;
2023-11-10 15:37:12 +01:00
if (newWidth > maxWidth) {
newHeight *= maxWidth / newWidth;
newWidth = maxWidth;
}
2023-11-10 15:37:12 +01:00
if (newHeight > maxHeight) {
newWidth *= maxHeight / newHeight;
newHeight = maxHeight;
}
2023-11-10 15:37:12 +01:00
canvas.width = newWidth;
canvas.height = newHeight;
2023-11-10 15:37:12 +01:00
const context = canvas.getContext('2d');
context?.drawImage(image, 0, 0, newWidth, newHeight);
2023-11-10 15:37:12 +01:00
const compressedBase64 = canvas.toDataURL('image/jpeg', quality);
resolve(compressedBase64);
};
2023-11-10 15:37:12 +01:00
image.onerror = (error) => {
reject(error);
};
});
}
2023-11-10 15:37:12 +01:00
convertBlobToBase64(blob: Blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader;
reader.onerror = reject;
reader.onload = () => {
resolve(reader.result)
}
reader.readAsDataURL(blob)
})
}
2023-12-06 17:01:00 +01:00
removeTextBeforeSlash(inputString,mark) {
if (inputString.includes(mark)) {
const parts = inputString.split(mark);
2023-11-29 12:17:52 +01:00
return parts.length > 1 ? parts[1] : inputString;
} else {
return inputString;
}
}
2023-12-06 17:01:00 +01:00
async compressVideoBase64(base64String: string, maxWidth: number, maxHeight: number, quality: number): Promise<string> {
console.log(base64String)
return new Promise(async (resolve, reject) => {
try {
// Decode the base64 video string to an ArrayBuffer
const trimmedBase64 = base64String.trim();
const videoBuffer = this.base64ToArrayBuffer(this.removeTextBeforeSlash(trimmedBase64, ','));
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create a Blob from the ArrayBuffer
const videoBlob = new Blob([videoBuffer], { type: 'video/mp4' });
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create an object URL from the Blob
const videoObjectUrl = URL.createObjectURL(videoBlob);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create a video element
const video = document.createElement('video');
video.src = videoObjectUrl;
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Wait for the video to load metadata
video.addEventListener('loadedmetadata', async () => {
const canvas = document.createElement('canvas');
let newWidth = video.videoWidth;
let newHeight = video.videoHeight;
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
if (newWidth > maxWidth) {
newHeight *= maxWidth / newWidth;
newWidth = maxWidth;
}
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
if (newHeight > maxHeight) {
newWidth *= maxHeight / newHeight;
newHeight = maxHeight;
}
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
canvas.width = newWidth;
canvas.height = newHeight;
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
const context = canvas.getContext('2d');
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Start continuous rendering
function render() {
context?.drawImage(video, 0, 0, newWidth, newHeight);
requestAnimationFrame(render);
}
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
render();
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Convert the canvas to a Blob
canvas.toBlob(async (blob) => {
if (blob) {
// Read the Blob as an ArrayBuffer
const compressedVideoBuffer = await this.readBlobAsArrayBuffer(blob);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Convert the ArrayBuffer back to base64
const compressedBase64 = this.arrayBufferToBase64(compressedVideoBuffer);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
resolve(compressedBase64);
} else {
reject('Error creating compressed video blob.');
}
}, 'video/mp4', 0.5);
});
} catch (error) {
reject(error);
}
});
}
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
/* async compressVideoBase64(base64String: string, maxWidth: number, maxHeight: number, quality: number): Promise<string> {
return new Promise(async (resolve, reject) => {
try {
// Decode the base64 video string to an ArrayBuffer
const videoBuffer = this.base64ToArrayBuffer(this.removeTextBeforeSlash(base64String,','));
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create a Blob from the ArrayBuffer
const videoBlob = new Blob([videoBuffer], { type: 'video/mp4' });
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create an object URL from the Blob
const videoObjectUrl = URL.createObjectURL(videoBlob);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create a video element
const video = document.createElement('video');
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create a source element and set its type attribute
const source = document.createElement('source');
source.type = 'video/mp4';
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Set the source URL
source.src = videoObjectUrl;
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Append the source element to the video element
video.appendChild(source);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Wait for the video to load
video.addEventListener('loadedmetadata', async () => {
const canvas = document.createElement('canvas');
let newWidth = video.videoWidth;
let newHeight = video.videoHeight;
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
if (newWidth > maxWidth) {
newHeight *= maxWidth / newWidth;
newWidth = maxWidth;
}
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
if (newHeight > maxHeight) {
newWidth *= maxHeight / newHeight;
newHeight = maxHeight;
}
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
canvas.width = newWidth;
canvas.height = newHeight;
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
const context = canvas.getContext('2d');
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create a function to draw each video frame onto the canvas
const drawFrame = () => {
context?.drawImage(video, 0, 0, newWidth, newHeight);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Convert the canvas to a Blob with the correct MIME type
canvas.toBlob(async (blob) => {
if (blob) {
// Read the Blob as an ArrayBuffer
const compressedVideoBuffer = await this.readBlobAsArrayBuffer(blob);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Convert the ArrayBuffer back to base64
const compressedBase64 = this.arrayBufferToBase64(compressedVideoBuffer);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
resolve(compressedBase64);
} else {
reject('Error creating compressed video blob.');
}
}, 'video/mp4', quality);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Request the next video frame
requestAnimationFrame(drawFrame);
};
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Start drawing frames
drawFrame();
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Start playing the video
video.play();
});
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
} catch (error) {
reject(error);
}
});
} */
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
/* async compressVideoBase64(base64String: string, maxWidth: number, maxHeight: number, quality: number): Promise<string> {
return new Promise(async (resolve, reject) => {
try {
// Decode the base64 video string to an ArrayBuffer
const videoBuffer = this.base64ToArrayBuffer(this.removeTextBeforeSlash(base64String,','));
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create a Blob from the ArrayBuffer
const videoBlob = new Blob([videoBuffer], { type: 'video/mp4' });
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create an object URL from the Blob
const videoObjectUrl = URL.createObjectURL(videoBlob);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create a video element
const video = document.createElement('video');
video.src = videoObjectUrl;
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Wait for the video to load metadata
video.addEventListener('loadedmetadata', async () => {
const canvas = document.createElement('canvas');
let newWidth = video.videoWidth;
let newHeight = video.videoHeight;
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
if (newWidth > maxWidth) {
newHeight *= maxWidth / newWidth;
newWidth = maxWidth;
}
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
if (newHeight > maxHeight) {
newWidth *= maxHeight / newHeight;
newHeight = maxHeight;
}
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
canvas.width = newWidth;
canvas.height = newHeight;
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
const context = canvas.getContext('2d');
context?.drawImage(video, 0, 0, newWidth, newHeight);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Convert the canvas to a Blob with the correct MIME type
canvas.toBlob(async (blob) => {
if (blob) {
// Read the Blob as an ArrayBuffer
const compressedVideoBuffer = await this.readBlobAsArrayBuffer(blob);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Convert the ArrayBuffer back to base64
const compressedBase64 = this.arrayBufferToBase64(compressedVideoBuffer);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
resolve(compressedBase64);
} else {
reject('Error creating compressed video blob.');
}
}, 'video/mp4', quality);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
});
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
} catch (error) {
reject(error);
}
});
} */
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
/* async compressVideoBase64(base64String: string, maxWidth: number, maxHeight: number, quality: number): Promise<string> {
return new Promise(async (resolve, reject) => {
try {
// Decode the base64 video string to an ArrayBuffer
const trimmedBase64 = base64String.trim();
console.log(this.removeTextBeforeSlash(trimmedBase64,','))
const videoBuffer = this.base64ToArrayBuffer(this.removeTextBeforeSlash(trimmedBase64,','));
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create a Blob from the ArrayBuffer
const videoBlob = new Blob([videoBuffer], { type: 'video/mp4' });
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create an object URL from the Blob
const videoObjectUrl = URL.createObjectURL(videoBlob);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Create a video element
const video = document.createElement('video');
video.src = videoObjectUrl;
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Wait for the video to load metadata
video.addEventListener('loadedmetadata', async () => {
const canvas = document.createElement('canvas');
let newWidth = video.videoWidth;
let newHeight = video.videoHeight;
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
if (newWidth > maxWidth) {
newHeight *= maxWidth / newWidth;
newWidth = maxWidth;
}
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
if (newHeight > maxHeight) {
newWidth *= maxHeight / newHeight;
newHeight = maxHeight;
}
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
canvas.width = newWidth;
canvas.height = newHeight;
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
const context = canvas.getContext('2d');
context?.drawImage(video, 0, 0, newWidth, newHeight);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Convert the canvas to a Blob
canvas.toBlob(async (blob) => {
if (blob) {
// Read the Blob as an ArrayBuffer
const compressedVideoBuffer = await this.readBlobAsArrayBuffer(blob);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
// Convert the ArrayBuffer back to base64
const compressedBase64 = this.arrayBufferToBase64(compressedVideoBuffer);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
resolve(compressedBase64);
} else {
reject('Error creating compressed video blob.');
}
}, 'video/mp4', quality);
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
});
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
} catch (error) {
reject(error);
}
});
} */
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
private base64ToArrayBuffer(base64: string): ArrayBuffer {
const binaryString = window.atob(base64);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; ++i) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
}
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
private async readBlobAsArrayBuffer(blob: Blob): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
if (reader.result instanceof ArrayBuffer) {
resolve(reader.result);
} else {
reject('Error reading blob as ArrayBuffer.');
}
};
reader.readAsArrayBuffer(blob);
});
}
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
private arrayBufferToBase64(buffer: ArrayBuffer): string {
const binary = String.fromCharCode(...new Uint8Array(buffer));
return btoa(binary);
}
2024-01-06 20:37:49 +01:00
2023-12-06 17:01:00 +01:00
fileSizeToMB(sizeInBytes) {
var sizeInMB = (sizeInBytes / (1024 * 1024)).toFixed(2);
console.log(sizeInMB + 'MB');
return parseInt(sizeInMB)
}
2023-11-10 15:37:12 +01:00
2024-01-06 20:37:49 +01:00
deleteFromSeletedContent(index) {
this.seletedContent.splice(index, 1)
}
}
2024-01-09 11:14:27 +01:00
// class UploadFileUseCase {
// LakefsRepositoryService: LakefsRepositoryService = window["LakefsRepositoryService"]
// constructor() {}
// execute(attachment: PublicationAttachmentEntity) {
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// const file: File = attachment.blob
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// const chunkSize = 1024 * 100; // Adjust the chunk size as needed
// const fileSize = file.size;
// let uploadedSize = 0
// const totalChunks = Math.ceil(fileSize / chunkSize);
// let offset = 0;
// let i = 1;
// let j = 0;
// let path;
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// function count () {
// j++
// return j
// }
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// const readAndUploadChunk = async(index: number) => {
// return new Promise<void>((resolve, reject) => {
// const chunk = file.slice(offset, offset + chunkSize);
// const reader = new FileReader();
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// reader.onload = async () => {
// const blob = new Blob([reader.result as ArrayBuffer]);
// const formData = new FormData();
// const file = new File([blob], "test.mp4", { type: blob.type });
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// uploadedSize =+ file.size
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// formData.append("blobFile", file);
// formData.append("length", totalChunks.toString());
// formData.append("index", index.toString());
// formData.append("path", path);
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// try {
// const response = await this.LakefsRepositoryService.uploadFile(formData).toPromise()
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// console.log({response})
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// resolve()
// } catch (erro) {
// reject()
// }
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// };
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// reader.readAsArrayBuffer(chunk);
// offset += chunkSize;
// });
// }
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// const loop = async() => {
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// for (let index = 2; index <= totalChunks; index++) {
// await readAndUploadChunk(index);
// }
// }
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
// const startUpload = async() => {
// const index = count();
// const chunk = file.slice(offset, offset + chunkSize);
// const reader = new FileReader();
// reader.onload = async () => {
// const blob = new Blob([reader.result as ArrayBuffer]);
// const formData = new FormData();
// const file = new File([blob], "test.mp4", { type: blob.type });
// uploadedSize =+ file.size
// formData.append("blobFile", file);
// formData.append("length", totalChunks.toString());
// formData.append("index", index.toString());
// const response: any = await this.LakefsRepositoryService.uploadFile(formData).toPromise()
// console.log({ response, index });
// if (index === 1) {
// path = response.path;
// }
// await loop();
// };
// reader.readAsArrayBuffer(chunk);
// offset += chunkSize;
// }
// startUpload();
// }
// }
class UploadFileUseCase {
LakefsRepositoryService: LakefsRepositoryService = window["LakefsRepositoryService"]
constructor() {}
async execute(ChucksManager: ChucksManager): Promise<Result<ChucksManager, ChucksManager>> {
let path;
function makeFrom({blobFile, index, path}) {
const formData = new FormData();
formData.append("blobFile", blobFile);
formData.append("length", ChucksManager.chunks.totalChunks.toString());
formData.append("index", index.toString());
formData.append("path", path);
return formData
2024-01-06 20:37:49 +01:00
}
2024-01-09 11:14:27 +01:00
const readAndUploadChunk = async(index: number) => {
const chunk = await ChucksManager.chunks.getChunks(index)
const blob = new Blob([chunk]);
const blobFile = new File([blob], "test.mp4", { type: blob.type });
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
const form = makeFrom({blobFile, index, path})
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
return await this.LakefsRepositoryService.uploadFile(form)
}
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
if(!ChucksManager.hasPath()) {
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
const initIndex = ChucksManager.uploadsCount + 1
const chuck = await ChucksManager.chunks.getChunks(initIndex)
const blob = new Blob([chuck]);
const blobFile = new File([blob], "test.mp4", { type: blob.type });
const form = makeFrom({blobFile, index: initIndex, path})
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
const uploadRequest = await this.LakefsRepositoryService.uploadFile(form)
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
if(uploadRequest.isOk()) {
path = uploadRequest.value.path
} else {
return err(ChucksManager)
}
2024-01-06 20:37:49 +01:00
}
2024-01-09 11:14:27 +01:00
const indexContinuation = ChucksManager.uploadsCount + 1
for (let index = indexContinuation; index <= ChucksManager.chunks.totalChunks; index++) {
const uploadRequest = await readAndUploadChunk(index)
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
if(uploadRequest.isErr()) {
return err(ChucksManager)
}
}
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
return ok(ChucksManager)
2024-01-06 20:37:49 +01:00
}
}
class PublicationAttachmentEntity {
FileBase64: string
FileExtension: string
FileType: 'image' | 'video'
OriginalFileName: string
2024-01-09 11:14:27 +01:00
blobFile: File
toUpload = false;
chucksManager : ChucksManager
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
constructor({base64, extension, blobFile = null, OriginalFileName = null, FileType}) {
2024-01-06 20:37:49 +01:00
this.FileBase64 = base64;
this.FileExtension = extension;
2024-01-09 11:14:27 +01:00
this.blobFile = blobFile
2024-01-06 20:37:49 +01:00
this.OriginalFileName = OriginalFileName
this.FileType = FileType
this.fixFileBase64();
}
2024-01-06 20:37:49 +01:00
fixFileBase64() {
if(this.FileType == 'image' ) {
if(!this.FileBase64.startsWith('data:')) {
this.FileBase64 = 'data:image/jpg;base64,' + this.FileBase64
}
} else if (this.FileType == 'video' ) {
if(!this.FileBase64.startsWith('data:')) {
this.FileBase64 = 'data:video/mp4;base64,' + this.FileBase64
}
}
}
2024-01-09 11:14:27 +01:00
needUpload() {
this.toUpload = true
}
setChunkManger (chunks: Chunks) {
this.chucksManager = new ChucksManager({chunks})
}
get hasChunkManager() {
return this.chucksManager != null
}
2024-01-06 20:37:49 +01:00
}
interface IPublicationFormModelEntity {
DateIndex: any
DocumentId: any
ProcessId: any
Title: any
Message: any
DatePublication: any
Files: PublicationAttachmentEntity[]
}
class PublicationFormModel implements IPublicationFormModelEntity {
constructor() {}
DateIndex: any;
DocumentId: any;
ProcessId: any;
Title: any;
Message: any;
DatePublication: any;
OriginalFileName: string;
Files: PublicationAttachmentEntity[];
setData(data: IPublicationFormModelEntity) {
Object.assign(this, data)
}
2021-03-15 12:06:06 +01:00
}
2024-01-06 20:37:49 +01:00
class PublicationFormMV {
private UploadFileUseCase = new UploadFileUseCase()
private form = new PublicationFormModel()
setDataToFrom(data: IPublicationFormModelEntity) {
this.form.setData(data)
}
private getVideoFiles() {
return this.form.Files.filter( x => x.FileType == 'video')
}
private async upload(PublicationAttachmentEntity: PublicationAttachmentEntity) {
2024-01-09 11:14:27 +01:00
const fileBlob = PublicationAttachmentEntity.blobFile;
const fileChunks = new Chunks({chunkSize: 1024 })
fileChunks.setFile(fileBlob)
PublicationAttachmentEntity.setChunkManger(fileChunks)
const ChucksManagers = new ChucksManager({chunks: fileChunks})
this.UploadFileUseCase.execute(ChucksManagers)
2024-01-06 20:37:49 +01:00
}
uploadVideosFiles() {
const videosFiles = this.getVideoFiles()
2024-01-09 11:14:27 +01:00
const videosFilesToUploads = videosFiles.filter( e => e.toUpload == true)
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
for(const file of videosFilesToUploads) {
2024-01-06 20:37:49 +01:00
this.upload(file)
}
}
}
2024-01-09 11:14:27 +01:00
class Chunks {
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
chunkSize: number
private file: File
2024-01-06 20:37:49 +01:00
2024-01-09 11:14:27 +01:00
constructor({chunkSize}) {
this.chunkSize = chunkSize
}
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> {
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(this.file.slice(start, end));
});
}
setFile(file: File) {
this.file = file
}
async getChunks(i: number) {
i--
if(i < this.totalChunks) {
const start = i * this.chunkSize;
const end = Math.min(start + this.chunkSize, this.file.size);
const chunk = await this.readChunk(start, end);
return chunk
}
}
}
interface IUploadResponse {
result: Result<string, Error>
attemp: number
}
class ChucksManager {
chunks: Chunks
uploads: IUploadResponse[] = []
path: string =''
get uploadsCount() {
return this.uploads.length
}
uploadFunc: Function
constructor({chunks}) {
this.chunks = chunks
}
setPath(path: string) {
this.path = path
}
hasPath() {
return this.path != undefined
}
}