From 448092677b95e8bf8e28b60b92421bf2cd5941bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eudes=20In=C3=A1cio?= Date: Mon, 6 Nov 2023 13:33:35 +0100 Subject: [PATCH] add some code --- .../new-publication/new-publication.page.ts | 80 ++++++++++++++++--- 1 file changed, 70 insertions(+), 10 deletions(-) diff --git a/src/app/pages/publications/new-publication/new-publication.page.ts b/src/app/pages/publications/new-publication/new-publication.page.ts index 56b938ccd..28790939e 100644 --- a/src/app/pages/publications/new-publication/new-publication.page.ts +++ b/src/app/pages/publications/new-publication/new-publication.page.ts @@ -16,7 +16,7 @@ import { formatDate } from 'src/plugin/momentG.js' import { ThemeService } from 'src/app/services/theme.service'; import { Camera, CameraResultType, CameraSource, Photo } from '@capacitor/camera'; -import { Filesystem, Directory, Encoding } from '@capacitor/filesystem'; +import { Filesystem, Directory, Encoding, FilesystemDirectory } from '@capacitor/filesystem'; import { NgxImageCompressService } from "ngx-image-compress"; import { HttpErrorHandle } from 'src/app/services/http-error-handle.service'; import { PublicationFolderService } from 'src/app/store/publication-folder.service'; @@ -25,8 +25,7 @@ import { FileService } from 'src/app/services/functions/file.service'; import { readAndCompressImage } from 'browser-image-resizer'; import { FilePicker } from '@capawesome/capacitor-file-picker'; import { CapacitorVideoPlayer } from 'capacitor-video-player'; -import { VideoRecorder, VideoRecorderCamera, VideoRecorderPreviewFrame } from '@teamhive/capacitor-video-recorder'; -import { CaptureImageOptions, MediaCapture } from '@awesome-cordova-plugins/media-capture/ngx'; +import { CaptureError, CaptureImageOptions, MediaCapture, MediaFile } from '@awesome-cordova-plugins/media-capture/ngx'; import { Capacitor } from '@capacitor/core'; const config = { @@ -44,7 +43,7 @@ interface LocalFile { data: string; } -const config_video: VideoRecorderPreviewFrame = { +/* const config_video: VideoRecorderPreviewFrame = { id: 'video-record', stackPosition: 'front', // 'front' overlays your app', 'back' places behind your app. width: 'fill', @@ -52,7 +51,7 @@ const config_video: VideoRecorderPreviewFrame = { x: 0, y: 0, borderRadius: 0 -}; +}; */ @Component({ selector: 'app-new-publication', templateUrl: './new-publication.page.html', @@ -206,10 +205,24 @@ export class NewPublicationPage implements OnInit { async startVideoRecording() { try { let options: CaptureImageOptions = { limit: 1 } - const data = await this.mediaCapture.captureVideo(options) - this.video = data[0]; + let result: any + result = await this.mediaCapture.captureVideo(options) + this.video = result[0]; console.log(this.video) - this.fileType = "video/mp4" + if (result && result.length > 0) { + const videoFile = result[0]; + const copiedVideoPath = await this.copyVideoToDataDirectory(videoFile); + + if (copiedVideoPath) { + const base64Video = await this.convertVideoToBase64(copiedVideoPath); + console.log(base64Video); + } + } + + + + /* this.convertVideoToBase64(data[0].fullPath) */ + /* this.fileType = "video/mp4" await Filesystem.writeFile({ path: data[0].name, data: data[0].fullPath, @@ -226,10 +239,10 @@ export class NewPublicationPage implements OnInit { console.log(base64Data); /* this.filecontent = true; this.capturedVideo = "data:video/mp4;base64," + content.data; - this.photoOrVideo = false; */ + this.photoOrVideo = false; }) .catch((err) => console.error(err)); - }); + }); */ } catch (error) { console.log('record video error: ', error) @@ -562,4 +575,51 @@ export class NewPublicationPage implements OnInit { }); } + _getBase64(file) { + return new Promise((resolve, reject) => { + var reader = new FileReader(); + reader.readAsDataURL(file); + reader.onload = function () { + resolve(reader.result) + }; + reader.onerror = function (error) { + console.log('Error: ', error); + }; + }) + } + + + async copyVideoToDataDirectory(videoFile: any): Promise { + try { + const { uri } = videoFile; + const fileName = uri.substring(uri.lastIndexOf('/') + 1); + const targetPath = Directory.Data + fileName; // Set your target directory path + + await Filesystem.copy({ + from: uri, + to: targetPath, + }); + + return targetPath; + } catch (error) { + console.error(error); + return null; + } + } + + async convertVideoToBase64(videoPath: string): Promise { + try { + const file = await Filesystem.readFile({ path: videoPath, directory: FilesystemDirectory.Data }); + if (file.data) { + return 'data:video/mp4;base64,' + file.data; + } else { + throw new Error('Failed to read the video file.'); + } + } catch (error) { + console.error(error); + return null; + } + } + + }