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';
|
2024-01-18 13:08:10 +01:00
|
|
|
import { FileValidatorService } from "src/app/services/file/file-validator.service";
|
|
|
|
|
import { MiddlewareServiceService } from "src/app/shared/API/middleware/middleware-service.service";
|
2024-01-06 20:37:49 +01:00
|
|
|
import { LakefsRepositoryService } from '../../repository/lakefs/lakefs-repository.service';
|
2024-01-09 11:14:27 +01:00
|
|
|
import { ok, err, Result } from 'neverthrow';
|
2024-01-29 17:25:05 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
import { ObjectMergeNotification, SocketConnectionMCRService } from "src/app/services/socket-connection-mcr.service"
|
|
|
|
|
import { CMAPIService } from '../../repository/CMAPI/cmapi.service';
|
|
|
|
|
import { environment } from 'src/environments/environment';
|
2024-01-26 05:59:14 +01:00
|
|
|
import { CaptureError, CaptureImageOptions, MediaCapture, MediaFile } from '@awesome-cordova-plugins/media-capture/ngx';
|
|
|
|
|
import { Filesystem, Directory, Encoding, FilesystemDirectory } from '@capacitor/filesystem';
|
|
|
|
|
import { Platform } from '@ionic/angular';
|
2024-01-29 08:25:27 +01:00
|
|
|
import { Capacitor } from '@capacitor/core';
|
2024-01-29 13:49:53 +01:00
|
|
|
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
enum ActionType {
|
2024-01-06 20:37:49 +01:00
|
|
|
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;
|
2024-01-26 05:59:14 +01:00
|
|
|
photoOrVideo: boolean = false;
|
|
|
|
|
video: any;
|
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,
|
2024-01-17 10:25:16 +01:00
|
|
|
private LakefsRepositoryService: LakefsRepositoryService,
|
|
|
|
|
private SocketConnectionMCRService: SocketConnectionMCRService,
|
2024-01-26 05:59:14 +01:00
|
|
|
private CMAPIService: CMAPIService,
|
|
|
|
|
private mediaCapture: MediaCapture,
|
|
|
|
|
private httpErrorHandle: HttpErrorHandle,
|
|
|
|
|
private platform: Platform,
|
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;
|
|
|
|
|
}
|
2024-01-17 10:25:16 +01:00
|
|
|
|
2024-01-24 10:08:21 +01:00
|
|
|
this.SocketConnectionMCRService.connect()
|
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,
|
|
|
|
|
}
|
2024-01-26 05:59:14 +01:00
|
|
|
console.log('edit', this.publication)
|
2023-08-28 17:06:16 +01:00
|
|
|
this.pub = this.publication;
|
2024-01-26 05:59:14 +01:00
|
|
|
this.seletedContent = this.publication.Files.map(e => {
|
2024-01-12 12:13:51 +01:00
|
|
|
return new PublicationAttachmentEntity(
|
|
|
|
|
{
|
|
|
|
|
base64: e.FileBase64,
|
|
|
|
|
extension: e.FileExtension,
|
|
|
|
|
OriginalFileName: e.OriginalFileName,
|
|
|
|
|
FileType: this.checkFileType.checkFileType(e.FileExtension)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
})
|
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) => {
|
2024-01-26 05:59:14 +01:00
|
|
|
this.photoOrVideo = false;
|
2023-11-29 12:17:52 +01:00
|
|
|
let fileObject = {
|
|
|
|
|
FileBase64: picture,
|
2024-01-29 08:25:27 +01:00
|
|
|
FileExtension: this.removeTextBeforeSlash('jpeg', '/'),
|
|
|
|
|
OriginalFileName: 'imagem'
|
2023-11-29 12:17:52 +01:00
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
const FileExtension = this.removeTextBeforeSlash('jpeg', '/')
|
2024-01-06 20:37:49 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
});
|
2024-01-26 05:59:14 +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,
|
2024-01-29 08:25:27 +01:00
|
|
|
FileExtension: this.removeTextBeforeSlash('jpeg', '/'),
|
|
|
|
|
OriginalFileName: 'image'
|
2023-11-29 12:17:52 +01:00
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
const FileExtension = this.removeTextBeforeSlash('jpeg', '/')
|
2024-01-06 20:37:49 +01:00
|
|
|
|
|
|
|
|
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-08-28 16:36:31 +01:00
|
|
|
});
|
2023-12-06 17:01:00 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
console.log(result)
|
2023-12-06 17:01:00 +01:00
|
|
|
|
2024-01-09 11:14:27 +01:00
|
|
|
result.files.forEach(async blobFile => {
|
2024-01-26 05:59:14 +01:00
|
|
|
console.log(blobFile)
|
|
|
|
|
if (this.checkFileType.checkFileType(blobFile.mimeType) == 'image' || this.checkFileType.checkFileType(blobFile.mimeType) == 'video') {
|
2024-01-29 08:25:27 +01:00
|
|
|
/* console.log('converte new way',this.getBase64(blobFile)) */
|
|
|
|
|
|
|
|
|
|
/* const blob = await fetch(
|
|
|
|
|
Capacitor.convertFileSrc(blobFile.path)
|
|
|
|
|
).then(r => r.blob()); */
|
|
|
|
|
|
|
|
|
|
/* console.log(await blob.arrayBuffer());
|
|
|
|
|
|
2024-01-29 17:25:05 +01:00
|
|
|
|
|
|
|
|
|
2024-01-29 08:25:27 +01:00
|
|
|
console.log("base64 :data:video/mp4;base64,",this.arrayBufferToBase64(await blob.arrayBuffer())) */
|
|
|
|
|
|
2024-01-09 11:14:27 +01:00
|
|
|
this.convertBlobToBase64(blobFile.blob).then((value) => {
|
2024-01-26 05:59:14 +01:00
|
|
|
|
2024-01-29 08:25:27 +01:00
|
|
|
console.log(value)
|
|
|
|
|
|
2024-01-09 11:14:27 +01:00
|
|
|
this.filesSizeSum = this.filesSizeSum + blobFile.size
|
2024-01-26 05:59:14 +01:00
|
|
|
if (this.fileSizeToMB(this.filesSizeSum) <= 20) {
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
const FileExtension = this.removeTextBeforeSlash(blobFile.mimeType, '/')
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
const file = new File([blobFile.blob], blobFile.name);
|
2024-01-17 10:25:16 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
const newAttachment = new PublicationAttachmentEntity(
|
|
|
|
|
{
|
|
|
|
|
base64: value,
|
|
|
|
|
extension: FileExtension,
|
|
|
|
|
blobFile: file,
|
|
|
|
|
FileType: this.checkFileType.checkFileType(FileExtension)
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
newAttachment.needUpload()
|
2024-01-09 11:14:27 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
this.seletedContent.push(newAttachment)
|
|
|
|
|
console.log(this.seletedContent)
|
|
|
|
|
this.filecontent = true;
|
|
|
|
|
} else {
|
|
|
|
|
if (this.seletedContent.length === 0)
|
|
|
|
|
this.filesSizeSum = 0
|
2023-12-06 17:01:00 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
this.httpErroHandle.validationMessagge('filessize');
|
|
|
|
|
}
|
2024-01-29 08:25:27 +01:00
|
|
|
}).catch((erro) => {
|
|
|
|
|
console.log(erro)
|
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-08-28 16:36:31 +01:00
|
|
|
|
2024-01-29 17:25:05 +01:00
|
|
|
|
2024-01-29 08:25:27 +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';
|
2023-08-28 16:36:31 +01:00
|
|
|
|
2023-11-09 11:45:04 +01:00
|
|
|
|
|
|
|
|
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-09 11:45:04 +01:00
|
|
|
|
2023-11-10 15:37:12 +01:00
|
|
|
};
|
2021-11-09 16:38:26 +01:00
|
|
|
|
2024-01-29 08:25:27 +01:00
|
|
|
async loadVideoTablet() {
|
|
|
|
|
|
|
|
|
|
const result = await FilePicker.pickMedia
|
|
|
|
|
({
|
|
|
|
|
multiple: true,
|
|
|
|
|
});
|
|
|
|
|
console.log(result.files)
|
|
|
|
|
result.files.forEach(element => {
|
|
|
|
|
|
|
|
|
|
this.filesSizeSum = this.filesSizeSum + element.size
|
|
|
|
|
if (this.fileSizeToMB(this.filesSizeSum) <= 20) {
|
|
|
|
|
if (this.checkFileType.checkFileType(element.mimeType) == 'image' || this.checkFileType.checkFileType(element.mimeType) == 'video') {
|
|
|
|
|
let resultUrl = decodeURIComponent(element.path);
|
|
|
|
|
try {
|
|
|
|
|
Filesystem.readFile({ path: resultUrl })
|
|
|
|
|
|
|
|
|
|
.then(async (content) => {
|
|
|
|
|
console.log(result)
|
2024-01-29 13:43:49 +01:00
|
|
|
console.log('load video tablet base64',content)
|
2024-01-29 08:25:27 +01:00
|
|
|
this.filecontent = true;
|
|
|
|
|
let fileObject = new PublicationAttachmentEntity ({
|
|
|
|
|
base64: content.data,
|
|
|
|
|
extension: this.removeTextBeforeSlash(element.mimeType, '/'),
|
|
|
|
|
OriginalFileName: 'video',
|
|
|
|
|
FileType: this.checkFileType.checkFileType( this.removeTextBeforeSlash(element.mimeType, '/'))
|
|
|
|
|
})
|
|
|
|
|
this.seletedContent.push(fileObject)
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => console.error(err));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('upload video error: ', error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (this.seletedContent.length === 0)
|
|
|
|
|
this.filesSizeSum = 0
|
|
|
|
|
|
|
|
|
|
this.httpErrorHandle.validationMessagge('filessize')
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
|
2023-11-10 15:37:12 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
if (this.seletedContent.length == 0) {
|
2024-01-19 09:42:24 +01:00
|
|
|
|
2024-01-22 16:09:02 +01:00
|
|
|
const newAttachment = new PublicationAttachmentEntity(
|
|
|
|
|
{
|
|
|
|
|
base64: "data:image/png;base64,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
|
|
|
|
|
extension: "png",
|
|
|
|
|
OriginalFileName: "foto",
|
|
|
|
|
FileType: 'image'
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-01-19 09:42:24 +01:00
|
|
|
|
2024-01-22 16:09:02 +01:00
|
|
|
this.seletedContent.push(newAttachment)
|
|
|
|
|
}
|
2024-01-19 09:42:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.publicationType == ActionType.edit) {
|
2023-08-28 17:06:16 +01:00
|
|
|
|
2021-07-26 16:40:51 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
|
2024-01-17 10:25:16 +01:00
|
|
|
if (this.seletedContent.length >= 1) {
|
|
|
|
|
const loader = this.toastService.loading()
|
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,
|
2024-01-17 10:25:16 +01:00
|
|
|
Files: this.seletedContent,
|
|
|
|
|
}
|
2024-01-17 15:11:31 +01:00
|
|
|
|
|
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
this.publicationFormMV.setDataToFrom(this.publication)
|
|
|
|
|
const upload = await this.publicationFormMV.uploadVideosFiles()
|
|
|
|
|
|
|
|
|
|
if(upload) {
|
|
|
|
|
this.publication.Files = this.publication.Files.map((e:PublicationAttachmentEntity)=> {
|
|
|
|
|
if(e.FileType == 'video') {
|
|
|
|
|
e.FileBase64 = environment.apiURL + "/ObjectServer/StreamFiles?path="+ e.chucksManager.path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return e
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
console.log("this.publication.Files", this.publication.Files)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
this.publication.Files = this.publication.Files.map(e => ({
|
2024-01-17 10:25:16 +01:00
|
|
|
FileBase64: e.FileBase64,
|
|
|
|
|
FileExtension: e.FileExtension,
|
|
|
|
|
OriginalFileName: 'foto'
|
|
|
|
|
}))
|
2024-01-17 15:11:31 +01:00
|
|
|
|
2024-01-17 10:25:16 +01:00
|
|
|
try {
|
2024-01-17 15:11:31 +01:00
|
|
|
|
2024-01-17 10:25:16 +01:00
|
|
|
const response = await this.publications.UpdatePublication(this.publication.ProcessId, this.publication).toPromise()
|
2024-01-17 15:11:31 +01:00
|
|
|
|
2024-01-17 10:25:16 +01:00
|
|
|
this.httpErroHandle.httpsSucessMessagge('Editar publicação')
|
2024-01-17 15:11:31 +01:00
|
|
|
|
2024-01-17 10:25:16 +01:00
|
|
|
this.goBack();
|
2024-01-17 15:11:31 +01:00
|
|
|
|
2024-01-17 10:25:16 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
this.httpErroHandle.httpStatusHandle(error)
|
|
|
|
|
if (error.status == 404) {
|
|
|
|
|
this.PublicationFolderService.deletePost(this.publication.ProcessId, this.publication.DocumentId)
|
|
|
|
|
this.goBack();
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
loader.remove()
|
2021-03-15 12:06:06 +01:00
|
|
|
}
|
2021-11-09 10:39:14 +01:00
|
|
|
|
2024-01-17 10:25:16 +01:00
|
|
|
|
2021-07-26 15:19:03 +01:00
|
|
|
} else {
|
2024-01-17 10:30:04 +01:00
|
|
|
this.toastService._badRequest("É necessário adicionar uma imagem ou vídeo")
|
2024-01-17 10:25:16 +01:00
|
|
|
}
|
2021-07-26 15:19:03 +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()
|
2024-01-17 10:25:16 +01:00
|
|
|
if (this.seletedContent.length >= 1) {
|
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-17 15:11:31 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
this.publicationFormMV.setDataToFrom(this.publication)
|
|
|
|
|
const upload = await this.publicationFormMV.uploadVideosFiles()
|
|
|
|
|
|
|
|
|
|
if(upload) {
|
|
|
|
|
this.publication.Files = this.publication.Files.map((e:PublicationAttachmentEntity)=> {
|
|
|
|
|
if(e.FileType == 'video') {
|
|
|
|
|
e.FileBase64 = environment.apiURL + "/ObjectServer/StreamFiles?path="+ e.chucksManager.path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return e
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
console.log("this.publication.Files", this.publication.Files)
|
|
|
|
|
}
|
2024-01-18 13:08:10 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
this.publication.Files = this.publication.Files.map(e => ({
|
2024-01-18 13:08:10 +01:00
|
|
|
FileBase64: e.FileBase64,
|
|
|
|
|
FileExtension: e.FileExtension,
|
|
|
|
|
OriginalFileName: 'foto'
|
|
|
|
|
}))
|
2024-01-17 15:11:31 +01:00
|
|
|
|
2024-01-19 09:42:24 +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
|
|
|
|
2023-12-13 17:08:56 +01:00
|
|
|
} else {
|
2024-01-17 10:30:04 +01:00
|
|
|
this.toastService._badRequest("É necessário adicionar uma imagem ou vídeo")
|
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
|
|
|
}
|
2021-12-13 09:51:57 +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
|
|
|
|
2021-03-17 09:01:24 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-28 16:36:31 +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
|
|
|
|
2023-08-28 16:36:31 +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
|
|
|
|
2023-08-28 16:36:31 +01:00
|
|
|
if (newWidth > maxWidth) {
|
|
|
|
|
newHeight *= maxWidth / newWidth;
|
|
|
|
|
newWidth = maxWidth;
|
|
|
|
|
}
|
2023-11-10 15:37:12 +01:00
|
|
|
|
2023-08-28 16:36:31 +01:00
|
|
|
if (newHeight > maxHeight) {
|
|
|
|
|
newWidth *= maxHeight / newHeight;
|
|
|
|
|
newHeight = maxHeight;
|
|
|
|
|
}
|
2023-11-10 15:37:12 +01:00
|
|
|
|
2023-08-28 16:36:31 +01:00
|
|
|
canvas.width = newWidth;
|
|
|
|
|
canvas.height = newHeight;
|
2023-11-10 15:37:12 +01:00
|
|
|
|
2023-08-28 16:36:31 +01:00
|
|
|
const context = canvas.getContext('2d');
|
|
|
|
|
context?.drawImage(image, 0, 0, newWidth, newHeight);
|
2023-11-10 15:37:12 +01:00
|
|
|
|
2023-08-28 16:36:31 +01:00
|
|
|
const compressedBase64 = canvas.toDataURL('image/jpeg', quality);
|
|
|
|
|
resolve(compressedBase64);
|
|
|
|
|
};
|
2023-11-10 15:37:12 +01:00
|
|
|
|
2023-08-28 16:36:31 +01:00
|
|
|
image.onerror = (error) => {
|
|
|
|
|
reject(error);
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-10 15:37:12 +01:00
|
|
|
convertBlobToBase64(blob: Blob) {
|
2024-01-26 05:59:14 +01:00
|
|
|
console.log('Convert blob ',blob)
|
2023-11-10 15:37:12 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const reader = new FileReader;
|
|
|
|
|
reader.onerror = reject;
|
|
|
|
|
reader.onload = () => {
|
|
|
|
|
resolve(reader.result)
|
|
|
|
|
}
|
|
|
|
|
reader.readAsDataURL(blob)
|
2024-01-29 08:25:27 +01:00
|
|
|
},)
|
2023-11-10 15:37:12 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-29 08:25:27 +01:00
|
|
|
getBase64(file) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
|
reader.onload = () => resolve(reader.result);
|
|
|
|
|
reader.onerror = error => reject(error);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
removeTextBeforeSlash(inputString, mark) {
|
2023-12-06 17:01:00 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2024-01-26 05:59:14 +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
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Create a Blob from the ArrayBuffer
|
|
|
|
|
const videoBlob = new Blob([videoBuffer], { type: 'video/mp4' });
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Create an object URL from the Blob
|
|
|
|
|
const videoObjectUrl = URL.createObjectURL(videoBlob);
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Create a video element
|
|
|
|
|
const video = document.createElement('video');
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +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
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Set the source URL
|
|
|
|
|
source.src = videoObjectUrl;
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Append the source element to the video element
|
|
|
|
|
video.appendChild(source);
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +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
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
if (newWidth > maxWidth) {
|
|
|
|
|
newHeight *= maxWidth / newWidth;
|
|
|
|
|
newWidth = maxWidth;
|
|
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
if (newHeight > maxHeight) {
|
|
|
|
|
newWidth *= maxHeight / newHeight;
|
|
|
|
|
newHeight = maxHeight;
|
|
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
canvas.width = newWidth;
|
|
|
|
|
canvas.height = newHeight;
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
const context = canvas.getContext('2d');
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +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
|
|
|
|
2024-01-26 05:59:14 +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
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Convert the ArrayBuffer back to base64
|
|
|
|
|
const compressedBase64 = this.arrayBufferToBase64(compressedVideoBuffer);
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
resolve(compressedBase64);
|
|
|
|
|
} else {
|
|
|
|
|
reject('Error creating compressed video blob.');
|
|
|
|
|
}
|
|
|
|
|
}, 'video/mp4', quality);
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Request the next video frame
|
|
|
|
|
requestAnimationFrame(drawFrame);
|
|
|
|
|
};
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Start drawing frames
|
|
|
|
|
drawFrame();
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Start playing the video
|
|
|
|
|
video.play();
|
|
|
|
|
});
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
reject(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} */
|
2023-12-06 17:01:00 +01:00
|
|
|
|
2024-01-06 20:37:49 +01:00
|
|
|
|
|
|
|
|
|
2023-12-06 17:01:00 +01:00
|
|
|
|
2024-01-26 05:59:14 +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
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Create a Blob from the ArrayBuffer
|
|
|
|
|
const videoBlob = new Blob([videoBuffer], { type: 'video/mp4' });
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Create an object URL from the Blob
|
|
|
|
|
const videoObjectUrl = URL.createObjectURL(videoBlob);
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Create a video element
|
|
|
|
|
const video = document.createElement('video');
|
|
|
|
|
video.src = videoObjectUrl;
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +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
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
if (newWidth > maxWidth) {
|
|
|
|
|
newHeight *= maxWidth / newWidth;
|
|
|
|
|
newWidth = maxWidth;
|
|
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
if (newHeight > maxHeight) {
|
|
|
|
|
newWidth *= maxHeight / newHeight;
|
|
|
|
|
newHeight = maxHeight;
|
|
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
canvas.width = newWidth;
|
|
|
|
|
canvas.height = newHeight;
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
const context = canvas.getContext('2d');
|
|
|
|
|
context?.drawImage(video, 0, 0, newWidth, newHeight);
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +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
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Convert the ArrayBuffer back to base64
|
|
|
|
|
const compressedBase64 = this.arrayBufferToBase64(compressedVideoBuffer);
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
resolve(compressedBase64);
|
|
|
|
|
} else {
|
|
|
|
|
reject('Error creating compressed video blob.');
|
|
|
|
|
}
|
|
|
|
|
}, 'video/mp4', quality);
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
});
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
reject(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} */
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2023-12-06 17:01:00 +01:00
|
|
|
|
2024-01-26 05:59:14 +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
|
2023-12-06 17:01:00 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
const trimmedBase64 = base64String.trim();
|
|
|
|
|
console.log(this.removeTextBeforeSlash(trimmedBase64,','))
|
|
|
|
|
const videoBuffer = this.base64ToArrayBuffer(this.removeTextBeforeSlash(trimmedBase64,','));
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Create a Blob from the ArrayBuffer
|
|
|
|
|
const videoBlob = new Blob([videoBuffer], { type: 'video/mp4' });
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Create an object URL from the Blob
|
|
|
|
|
const videoObjectUrl = URL.createObjectURL(videoBlob);
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Create a video element
|
|
|
|
|
const video = document.createElement('video');
|
|
|
|
|
video.src = videoObjectUrl;
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +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
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
if (newWidth > maxWidth) {
|
|
|
|
|
newHeight *= maxWidth / newWidth;
|
|
|
|
|
newWidth = maxWidth;
|
|
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
if (newHeight > maxHeight) {
|
|
|
|
|
newWidth *= maxHeight / newHeight;
|
|
|
|
|
newHeight = maxHeight;
|
|
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
canvas.width = newWidth;
|
|
|
|
|
canvas.height = newHeight;
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
const context = canvas.getContext('2d');
|
2023-12-06 17:01:00 +01:00
|
|
|
context?.drawImage(video, 0, 0, newWidth, newHeight);
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
// Convert the canvas to a Blob
|
2023-12-06 17:01:00 +01:00
|
|
|
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
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
});
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +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-24 10:08:21 +01:00
|
|
|
|
|
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
async uploadChunk() {
|
2024-01-24 10:08:21 +01:00
|
|
|
let time = new Date()
|
|
|
|
|
|
|
|
|
|
this.publication = {
|
|
|
|
|
DateIndex: time,
|
|
|
|
|
DocumentId: null,
|
|
|
|
|
ProcessId: this.folderId,
|
|
|
|
|
Title: this.pub.Title,
|
|
|
|
|
Message: this.pub.Message,
|
|
|
|
|
DatePublication: time,
|
|
|
|
|
OriginalFileName: this.capturedImageTitle || 'foto',
|
|
|
|
|
Files: this.seletedContent,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-26 05:59:14 +01:00
|
|
|
chossePhotoOrVideo() {
|
|
|
|
|
this.photoOrVideo = !this.photoOrVideo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async startVideoRecording() {
|
|
|
|
|
try {
|
|
|
|
|
let options: CaptureImageOptions = { limit: 1 }
|
|
|
|
|
const data: any = await this.mediaCapture.captureVideo(options)
|
|
|
|
|
this.video = data[0];
|
|
|
|
|
console.log(data)
|
|
|
|
|
data.forEach(async element => {
|
|
|
|
|
this.filesSizeSum = this.filesSizeSum + element.size
|
|
|
|
|
if (this.fileSizeToMB(this.filesSizeSum) <= 20) {
|
|
|
|
|
const savedFile = await Filesystem.copy({
|
|
|
|
|
from: element.fullPath, // directory prop removed, Capacitor parses filename for us
|
|
|
|
|
to: "video.mp4",
|
|
|
|
|
toDirectory: FilesystemDirectory.Data
|
|
|
|
|
});
|
|
|
|
|
console.log(savedFile.uri)
|
|
|
|
|
Filesystem.readFile({ path: savedFile.uri })
|
|
|
|
|
|
|
|
|
|
.then(async (content) => {
|
|
|
|
|
this.filecontent = true;
|
2024-01-29 13:43:49 +01:00
|
|
|
console.log('',content)
|
2024-01-26 05:59:14 +01:00
|
|
|
let fileObject = new PublicationAttachmentEntity({
|
|
|
|
|
base64: content.data,
|
|
|
|
|
extension: 'mp4',
|
|
|
|
|
OriginalFileName: 'record',
|
|
|
|
|
FileType: 'video'
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
this.seletedContent.push(fileObject)
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => console.error(err));
|
|
|
|
|
} else {
|
|
|
|
|
if (this.seletedContent.length === 0)
|
|
|
|
|
this.filesSizeSum = 0
|
|
|
|
|
|
|
|
|
|
this.httpErrorHandle.validationMessagge('filessize')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('record video error: ', error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkTableDivice() {
|
|
|
|
|
if (!this.platform.is('desktop'))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-01-29 08:25:27 +01:00
|
|
|
|
|
|
|
|
checkDesktop() {
|
|
|
|
|
if (this.platform.is('desktop'))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
}
|
2024-01-09 11:14:27 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
class UploadFileUseCase {
|
|
|
|
|
CMAPIService: CMAPIService = window["CMAPIAPIRepository"]
|
|
|
|
|
constructor() {}
|
|
|
|
|
async execute(PublicationAttachmentEntity: PublicationAttachmentEntity): Promise<Result<PublicationAttachmentEntity, PublicationAttachmentEntity>> {
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
2024-01-09 11:14:27 +01:00
|
|
|
|
|
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
let path: string;
|
|
|
|
|
const length = PublicationAttachmentEntity.chucksManager.chunks.totalChunks.toString()
|
2024-01-09 11:14:27 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
const readAndUploadChunk = async(index: number) => {
|
2024-01-09 11:14:27 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
const chunk = await PublicationAttachmentEntity.chucksManager.chunks.getChunks(index)
|
|
|
|
|
const blob = new Blob([chunk]);
|
|
|
|
|
const blobFile = new File([blob], "test.mp4", { type: blob.type });
|
2024-01-09 11:14:27 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
return await this.CMAPIService.FileContent({length, path: PublicationAttachmentEntity.chucksManager.path, index, blobFile})
|
|
|
|
|
}
|
2024-01-09 11:14:27 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
if(!PublicationAttachmentEntity.chucksManager.hasPath()) {
|
|
|
|
|
const initIndex = 1
|
2024-01-09 11:14:27 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
const uploadRequest = await readAndUploadChunk(initIndex)
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
if(uploadRequest.isOk()) {
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
path = uploadRequest.value.data
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
PublicationAttachmentEntity.chucksManager.setPath(path)
|
|
|
|
|
PublicationAttachmentEntity.chucksManager.setResponse(initIndex, uploadRequest)
|
|
|
|
|
} else {
|
|
|
|
|
reject(err(PublicationAttachmentEntity))
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
const allRequest: Promise<any>[] = []
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
for (let index = 2; index <= PublicationAttachmentEntity.chucksManager.chunks.totalChunks -1; index++) {
|
|
|
|
|
const needUpload = PublicationAttachmentEntity.chucksManager.needToUploadChunkIndex(index)
|
2024-01-17 10:25:16 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
if(needUpload) {
|
2024-01-24 10:08:21 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
const request = readAndUploadChunk(index).then(async(uploadRequest) => {
|
|
|
|
|
if(uploadRequest.isErr()) {
|
|
|
|
|
const pingRequest = await this.CMAPIService.ping()
|
|
|
|
|
if( pingRequest.isErr()) {
|
|
|
|
|
reject(err(PublicationAttachmentEntity))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
PublicationAttachmentEntity.chucksManager.setResponse(index, uploadRequest)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
allRequest.push(request)
|
|
|
|
|
|
|
|
|
|
// const request = readAndUploadChunk(index)
|
|
|
|
|
// const uploadRequest = await request
|
|
|
|
|
|
|
|
|
|
// allRequest.push(request)
|
|
|
|
|
// if(uploadRequest.isErr()) {
|
|
|
|
|
// const pingRequest = await this.CMAPIService.ping()
|
|
|
|
|
// if( pingRequest.isErr()) {
|
|
|
|
|
// reject(err(PublicationAttachmentEntity))
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// PublicationAttachmentEntity.chucksManager.setResponse(index, uploadRequest)
|
|
|
|
|
// }
|
|
|
|
|
}
|
2024-01-09 11:14:27 +01:00
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
await Promise.all(allRequest)
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
const uploadRequest = await readAndUploadChunk(PublicationAttachmentEntity.chucksManager.chunks.totalChunks)
|
2024-01-09 11:14:27 +01:00
|
|
|
if(uploadRequest.isErr()) {
|
2024-01-29 13:49:53 +01:00
|
|
|
const pingRequest = await this.CMAPIService.ping()
|
|
|
|
|
if( pingRequest.isErr()) {
|
|
|
|
|
reject(err(PublicationAttachmentEntity))
|
|
|
|
|
}
|
2024-01-17 10:25:16 +01:00
|
|
|
} else {
|
2024-01-29 13:49:53 +01:00
|
|
|
PublicationAttachmentEntity.chucksManager.setResponse(PublicationAttachmentEntity.chucksManager.chunks.totalChunks, uploadRequest)
|
2024-01-09 11:14:27 +01:00
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
PublicationAttachmentEntity.chucksManager.doneChunkUpload()
|
|
|
|
|
resolve(ok(PublicationAttachmentEntity))
|
|
|
|
|
})
|
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();
|
2023-12-12 10:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
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' ) {
|
2024-01-12 12:13:51 +01:00
|
|
|
if(!this.FileBase64.startsWith('data:') && !this.FileBase64.startsWith('http')) {
|
2024-01-06 20:37:49 +01:00
|
|
|
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})
|
|
|
|
|
}
|
2024-01-29 13:49:53 +01:00
|
|
|
get hasChunkManger() {
|
|
|
|
|
return this.chucksManager?.chunks
|
|
|
|
|
}
|
2024-01-09 11:14:27 +01:00
|
|
|
|
|
|
|
|
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[];
|
|
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
hasSet = false
|
|
|
|
|
|
2024-01-06 20:37:49 +01:00
|
|
|
setData(data: IPublicationFormModelEntity) {
|
2024-01-29 13:49:53 +01:00
|
|
|
if(!this.hasSet) {
|
|
|
|
|
Object.assign(this, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.hasSet = true
|
2024-01-06 20:37:49 +01:00
|
|
|
}
|
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()
|
2024-01-29 13:49:53 +01:00
|
|
|
private ObjectMergeNotification = new ObjectMergeNotification()
|
2024-01-06 20:37:49 +01:00
|
|
|
|
|
|
|
|
setDataToFrom(data: IPublicationFormModelEntity) {
|
|
|
|
|
this.form.setData(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getVideoFiles() {
|
|
|
|
|
return this.form.Files.filter( x => x.FileType == 'video')
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
private upload(PublicationAttachmentEntity: PublicationAttachmentEntity) {
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
return new Promise(async (resolve, reject)=> {
|
2024-01-09 11:14:27 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
if(!PublicationAttachmentEntity.hasChunkManger) {
|
|
|
|
|
const fileBlob = PublicationAttachmentEntity.blobFile;
|
|
|
|
|
const fileChunks = new Chunks({chunkSize: 500 })
|
|
|
|
|
fileChunks.setFile(fileBlob)
|
2024-01-09 11:14:27 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
PublicationAttachmentEntity.setChunkManger(fileChunks)
|
2024-01-09 11:14:27 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
PublicationAttachmentEntity.chucksManager.registerOnLastChunk(()=> {
|
|
|
|
|
const guid = PublicationAttachmentEntity.chucksManager.path
|
|
|
|
|
|
|
|
|
|
this.ObjectMergeNotification.subscribe(guid, (data) => {
|
|
|
|
|
// console.log("data", data)
|
|
|
|
|
PublicationAttachmentEntity
|
|
|
|
|
resolve(true)
|
|
|
|
|
})
|
|
|
|
|
})
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
} else {
|
|
|
|
|
if(PublicationAttachmentEntity.chucksManager.doneUpload) {
|
|
|
|
|
return resolve(true)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
const result = await this.UploadFileUseCase.execute(PublicationAttachmentEntity)
|
2024-01-06 20:37:49 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
if(result.isErr()) {
|
|
|
|
|
reject(false)
|
|
|
|
|
}
|
|
|
|
|
})
|
2024-01-06 20:37:49 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
uploadVideosFiles(): Promise<Boolean> {
|
|
|
|
|
return new Promise((resolve, reject)=> {
|
|
|
|
|
const videosFiles = this.getVideoFiles()
|
|
|
|
|
|
|
|
|
|
const videosFilesToUploads = videosFiles.filter( e => e.toUpload == true)
|
|
|
|
|
|
|
|
|
|
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.');
|
|
|
|
|
resolve(true)
|
|
|
|
|
} else {
|
|
|
|
|
reject(false)
|
|
|
|
|
console.log('Some promises failed to resolve successfully.');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
reject(false)
|
|
|
|
|
console.error('An error occurred while resolving promises:', error);
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
2024-01-06 20:37:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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}) {
|
2024-01-17 10:25:16 +01:00
|
|
|
this.chunkSize = chunkSize * 1024
|
2024-01-09 11:14:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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> {
|
2024-01-17 10:25:16 +01:00
|
|
|
const file = this.file
|
|
|
|
|
|
2024-01-09 11:14:27 +01:00
|
|
|
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"));
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-01-17 10:25:16 +01:00
|
|
|
|
|
|
|
|
reader.readAsArrayBuffer(file.slice(start, end));
|
2024-01-09 11:14:27 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2024-01-29 13:49:53 +01:00
|
|
|
result: Result<any, Error>
|
2024-01-09 11:14:27 +01:00
|
|
|
attemp: number
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ChucksManager {
|
|
|
|
|
chunks: Chunks
|
2024-01-17 10:25:16 +01:00
|
|
|
uploads: {[key: string]: IUploadResponse } = {}
|
|
|
|
|
path: string = undefined
|
2024-01-29 13:49:53 +01:00
|
|
|
uploadPercentage: string = "0%"
|
|
|
|
|
merging = false
|
|
|
|
|
onSetPath: Function[] = []
|
|
|
|
|
onSetLastChunk: Function[] = []
|
|
|
|
|
contentReady = false
|
|
|
|
|
|
|
|
|
|
getUploadPercentage() {
|
|
|
|
|
return this.uploadPercentage
|
|
|
|
|
}
|
2024-01-09 11:14:27 +01:00
|
|
|
|
|
|
|
|
get uploadsCount() {
|
2024-01-17 10:25:16 +01:00
|
|
|
return Object.entries(this.uploads).length
|
2024-01-09 11:14:27 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
get uploadWithSuccessCount() {
|
|
|
|
|
const uploadWithSuccess = Object.entries(this.uploads).filter(([index, data])=> data.result.isOk())
|
|
|
|
|
return uploadWithSuccess.length
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get doneUpload() {
|
|
|
|
|
return this.chunks.totalChunks == this.uploadWithSuccessCount
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-09 11:14:27 +01:00
|
|
|
uploadFunc: Function
|
|
|
|
|
constructor({chunks}) {
|
|
|
|
|
this.chunks = chunks
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
calculatePercentage(): number {
|
|
|
|
|
/**
|
|
|
|
|
* Calculate the percentage based on the total and current values.
|
|
|
|
|
*
|
|
|
|
|
* @param total - The total value.
|
|
|
|
|
* @param current - The current value.
|
|
|
|
|
* @returns The percentage calculated as (current / total) * 100.
|
|
|
|
|
*/
|
|
|
|
|
const total = this.chunks.totalChunks
|
|
|
|
|
const current = this.uploadWithSuccessCount
|
|
|
|
|
|
|
|
|
|
if (total === 0) {
|
|
|
|
|
return 0; // To avoid division by zero error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const percentage: number = (current / total) * 100;
|
|
|
|
|
return percentage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setPercentage() {
|
|
|
|
|
const percentage: number = this.calculatePercentage()
|
|
|
|
|
console.log({percentage})
|
|
|
|
|
this.uploadPercentage = percentage.toString()+"%"
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-09 11:14:27 +01:00
|
|
|
setPath(path: string) {
|
|
|
|
|
this.path = path
|
2024-01-29 13:49:53 +01:00
|
|
|
this.onSetPath.forEach(callback => callback());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnSetPath(a: Function) {
|
|
|
|
|
this.onSetPath.push(a)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnLastChunk(a: Function) {
|
|
|
|
|
this.onSetPath.push(a)
|
2024-01-09 11:14:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasPath() {
|
|
|
|
|
return this.path != undefined
|
|
|
|
|
}
|
2024-01-17 10:25:16 +01:00
|
|
|
|
2024-01-29 13:49:53 +01:00
|
|
|
isIndexRegistered(index) {
|
|
|
|
|
if(!this.uploads[index]) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
needToUploadChunkIndex(index) {
|
|
|
|
|
return !this.uploads?.[index]?.result?.isOk()
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 10:25:16 +01:00
|
|
|
setResponse(index, UploadResponse) {
|
2024-01-29 13:49:53 +01:00
|
|
|
|
|
|
|
|
if(!this.isIndexRegistered(index)) {
|
|
|
|
|
this.uploads[index] = {
|
|
|
|
|
attemp: 1,
|
|
|
|
|
result: UploadResponse
|
|
|
|
|
}
|
|
|
|
|
console.log({UploadResponse})
|
|
|
|
|
} else {
|
|
|
|
|
this.uploads[index].attemp++;
|
|
|
|
|
this.uploads[index].result = UploadResponse
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setPercentage()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
doneChunkUpload() {
|
|
|
|
|
this.merging = true
|
|
|
|
|
this.onSetLastChunk.forEach(callback => callback());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contentSetReady() {
|
|
|
|
|
this.merging = false
|
|
|
|
|
this.contentReady = true
|
2024-01-17 10:25:16 +01:00
|
|
|
}
|
2024-01-09 11:14:27 +01:00
|
|
|
}
|