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

816 lines
31 KiB
TypeScript
Raw Normal View History

2021-03-15 12:06:06 +01:00
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
2021-07-12 11:13:29 +01:00
import { SafeResourceUrl } from '@angular/platform-browser';
2021-03-15 12:06:06 +01:00
import { PublicationsService } from 'src/app/services/publications.service';
import { Publication } from 'src/app/models/publication';
import { Image } from 'src/app/models/image';
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';
import { Directory, Encoding, Filesystem } from '@capacitor/filesystem';
import { utf8Encode } from '@angular/compiler/src/util';
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"
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;
2023-11-29 12:17:52 +01:00
seletedContent: any[] = []
displayLimit = 4;
2023-12-06 17:01:00 +01:00
filesSizeSum = 0;
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,
private FileValidatorService: FileValidatorService
2021-08-23 16:06:05 +01:00
) {
this.publicationTitle = 'Nova Publicação';
2023-11-29 12:17:52 +01:00
if (this.publication) {
this.seletedContent = this.publication.Files;
this.filecontent = true;
}
2023-11-10 15:37:12 +01:00
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-18 17:37:11 +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)
2023-11-10 15:37:12 +01:00
if (this.publicationType != '2') {
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
}
this.seletedContent.push(fileObject)
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
}
this.seletedContent.push(fileObject)
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
2023-11-29 12:17:52 +01:00
result.files.forEach(async element => {
2023-12-06 17:01:00 +01:00
console.log(element)
2023-12-01 12:19:20 +01:00
if(this.checkFileType.checkFileType(element.mimeType) == 'image' || this.checkFileType.checkFileType(element.mimeType) == 'video') {
2023-11-29 12:17:52 +01:00
this.convertBlobToBase64(element.blob).then((value) => {
2023-12-06 17:01:00 +01:00
this.filesSizeSum = this.filesSizeSum + element.size
if(this.fileSizeToMB(this.filesSizeSum) <= 20) {
2023-11-29 12:17:52 +01:00
let fileObject = {
FileBase64: value,
2023-12-06 17:01:00 +01:00
FileExtension: this.removeTextBeforeSlash(element.mimeType,'/')
2023-11-29 12:17:52 +01:00
}
this.seletedContent.push(fileObject)
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
}
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
2023-11-10 15:37:12 +01:00
if (this.publicationType == '3') {
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 {
}/*
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,iVBORw0KGgoAAAANSUhEUgAAAgMAAAIJCAIAAAC3IqlmAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAABSKSURBVHhe7d1ncuNGo4bRu/8lfh5F5qToBdyWNJbHDYgAmETqPVXnh4PAhKp+iMDu/3v9+28AkikBQDolAEinBADplAAgnRIApFMCgHRKAJBOCQDSKQFAOiUASKcEAOmUACCdEgCkUwKAdEoAkE4JANIpAUA6JQBIpwQA6ZQAIJ0SAKRTAoB0SgCQTgkA0ikBQDolAEinBADplAAgnRIApFMCgHRKAJBOCQDSKQFAOiUASKcEAOmUACCdEgCkUwKAdEoAkE4JANIpAUA6JQBIpwQA6ZQAIJ0SAKRTAoB0SgCQTgkA0ikBQDolAEinBADplAAgnRIApFMCgHRKAJBOCQDSKQFAOiUASKcEAOmUACCdEgCkUwKAdEoAkE4JANIpAUA6JQBIpwQA6ZQAIJ0SAKRTAoB0SgCQTgkA0ikBQDolAEinBADplAAgnRIApFMCgHRKAJBOCQDSKQFAOiUASKcEAOmUACCdEgCkUwKAdEoAkE4JANIpAUA6JQBIpwQA6ZQAIJ0SAKRTAoB0SgCQTgkA0ikBQDolAEinBADplAAgnRIApFMCgHRKAJBOCQDSKQFAOiUASKcEAOmUACCdEgCkUwKAdEoAkE4JANIpAUA6JQBIpwQA6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2s3m3dPybB/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
2023-11-29 12:17:52 +01:00
const loader = this.toastService.loading()
2023-11-10 15:37:12 +01:00
2023-11-29 12:17:52 +01:00
try {
2021-07-12 11:13:29 +01:00
2023-11-29 12:17:52 +01:00
await this.publications.CreatePublication(this.publication.ProcessId, this.publication).toPromise()
if (this.publicationTitle == '1') {
2023-11-10 15:37:12 +01:00
2023-11-29 12:17:52 +01:00
} else if (this.publicationTitle == '2') {
this.httpErroHandle.httpsSucessMessagge('Criar publicação')
} else if (this.publicationTitle == '3') {
this.httpErroHandle.httpsSucessMessagge('Editar publicação')
}
2023-11-10 15:37:12 +01:00
2023-11-29 12:17:52 +01:00
this.goBackToViewPublications.emit();
} catch (error) {
this.httpErroHandle.httpStatusHandle(error)
} finally {
loader.remove()
2023-02-27 09:31:10 +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() {
if (this.publicationType == '1') {
2021-03-15 12:06:06 +01:00
this.publicationTitle = 'Nova Publicação Rápida';
}
2023-11-10 15:37:12 +01:00
else if (this.publicationType == '2') {
2021-03-15 12:06:06 +01:00
this.publicationTitle = 'Nova Publicação';
}
2023-11-10 15:37:12 +01:00
else if (this.publicationType == '3') {
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
2023-11-10 15:37:12 +01:00
if (this.publicationType == '2') {
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, ','));
// Create a Blob from the ArrayBuffer
const videoBlob = new Blob([videoBuffer], { type: 'video/mp4' });
// Create an object URL from the Blob
const videoObjectUrl = URL.createObjectURL(videoBlob);
// Create a video element
const video = document.createElement('video');
video.src = videoObjectUrl;
// Wait for the video to load metadata
video.addEventListener('loadedmetadata', async () => {
const canvas = document.createElement('canvas');
let newWidth = video.videoWidth;
let newHeight = video.videoHeight;
if (newWidth > maxWidth) {
newHeight *= maxWidth / newWidth;
newWidth = maxWidth;
}
if (newHeight > maxHeight) {
newWidth *= maxHeight / newHeight;
newHeight = maxHeight;
}
canvas.width = newWidth;
canvas.height = newHeight;
const context = canvas.getContext('2d');
// Start continuous rendering
function render() {
context?.drawImage(video, 0, 0, newWidth, newHeight);
requestAnimationFrame(render);
}
render();
// Convert the canvas to a Blob
canvas.toBlob(async (blob) => {
if (blob) {
// Read the Blob as an ArrayBuffer
const compressedVideoBuffer = await this.readBlobAsArrayBuffer(blob);
// Convert the ArrayBuffer back to base64
const compressedBase64 = this.arrayBufferToBase64(compressedVideoBuffer);
resolve(compressedBase64);
} else {
reject('Error creating compressed video blob.');
}
}, 'video/mp4', 0.5);
});
} catch (error) {
reject(error);
}
});
}
/* 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,','));
// Create a Blob from the ArrayBuffer
const videoBlob = new Blob([videoBuffer], { type: 'video/mp4' });
// Create an object URL from the Blob
const videoObjectUrl = URL.createObjectURL(videoBlob);
// Create a video element
const video = document.createElement('video');
// Create a source element and set its type attribute
const source = document.createElement('source');
source.type = 'video/mp4';
// Set the source URL
source.src = videoObjectUrl;
// Append the source element to the video element
video.appendChild(source);
// Wait for the video to load
video.addEventListener('loadedmetadata', async () => {
const canvas = document.createElement('canvas');
let newWidth = video.videoWidth;
let newHeight = video.videoHeight;
if (newWidth > maxWidth) {
newHeight *= maxWidth / newWidth;
newWidth = maxWidth;
}
if (newHeight > maxHeight) {
newWidth *= maxHeight / newHeight;
newHeight = maxHeight;
}
canvas.width = newWidth;
canvas.height = newHeight;
const context = canvas.getContext('2d');
// Create a function to draw each video frame onto the canvas
const drawFrame = () => {
context?.drawImage(video, 0, 0, newWidth, newHeight);
// 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);
// Convert the ArrayBuffer back to base64
const compressedBase64 = this.arrayBufferToBase64(compressedVideoBuffer);
resolve(compressedBase64);
} else {
reject('Error creating compressed video blob.');
}
}, 'video/mp4', quality);
// Request the next video frame
requestAnimationFrame(drawFrame);
};
// Start drawing frames
drawFrame();
// Start playing the video
video.play();
});
} catch (error) {
reject(error);
}
});
} */
/* 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,','));
// Create a Blob from the ArrayBuffer
const videoBlob = new Blob([videoBuffer], { type: 'video/mp4' });
// Create an object URL from the Blob
const videoObjectUrl = URL.createObjectURL(videoBlob);
// Create a video element
const video = document.createElement('video');
video.src = videoObjectUrl;
// Wait for the video to load metadata
video.addEventListener('loadedmetadata', async () => {
const canvas = document.createElement('canvas');
let newWidth = video.videoWidth;
let newHeight = video.videoHeight;
if (newWidth > maxWidth) {
newHeight *= maxWidth / newWidth;
newWidth = maxWidth;
}
if (newHeight > maxHeight) {
newWidth *= maxHeight / newHeight;
newHeight = maxHeight;
}
canvas.width = newWidth;
canvas.height = newHeight;
const context = canvas.getContext('2d');
context?.drawImage(video, 0, 0, newWidth, newHeight);
// 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);
// Convert the ArrayBuffer back to base64
const compressedBase64 = this.arrayBufferToBase64(compressedVideoBuffer);
resolve(compressedBase64);
} else {
reject('Error creating compressed video blob.');
}
}, 'video/mp4', quality);
});
} catch (error) {
reject(error);
}
});
} */
/* 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,','));
// Create a Blob from the ArrayBuffer
const videoBlob = new Blob([videoBuffer], { type: 'video/mp4' });
// Create an object URL from the Blob
const videoObjectUrl = URL.createObjectURL(videoBlob);
// Create a video element
const video = document.createElement('video');
video.src = videoObjectUrl;
// Wait for the video to load metadata
video.addEventListener('loadedmetadata', async () => {
const canvas = document.createElement('canvas');
let newWidth = video.videoWidth;
let newHeight = video.videoHeight;
if (newWidth > maxWidth) {
newHeight *= maxWidth / newWidth;
newWidth = maxWidth;
}
if (newHeight > maxHeight) {
newWidth *= maxHeight / newHeight;
newHeight = maxHeight;
}
canvas.width = newWidth;
canvas.height = newHeight;
const context = canvas.getContext('2d');
context?.drawImage(video, 0, 0, newWidth, newHeight);
// Convert the canvas to a Blob
canvas.toBlob(async (blob) => {
if (blob) {
// Read the Blob as an ArrayBuffer
const compressedVideoBuffer = await this.readBlobAsArrayBuffer(blob);
// Convert the ArrayBuffer back to base64
const compressedBase64 = this.arrayBufferToBase64(compressedVideoBuffer);
resolve(compressedBase64);
} else {
reject('Error creating compressed video blob.');
}
}, 'video/mp4', quality);
});
} catch (error) {
reject(error);
}
});
} */
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;
}
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);
});
}
private arrayBufferToBase64(buffer: ArrayBuffer): string {
const binary = String.fromCharCode(...new Uint8Array(buffer));
return btoa(binary);
}
fileSizeToMB(sizeInBytes) {
var sizeInMB = (sizeInBytes / (1024 * 1024)).toFixed(2);
console.log(sizeInMB + 'MB');
return parseInt(sizeInMB)
}
2023-11-10 15:37:12 +01:00
deleteFromSeletedContent(index){
this.seletedContent.splice(index,1)
}
2021-03-15 12:06:06 +01:00
}