some changes

This commit is contained in:
Eudes Inácio
2023-11-10 15:37:12 +01:00
parent d864c70b9f
commit 28432cc874
10 changed files with 382 additions and 240 deletions
@@ -27,6 +27,8 @@ import { FilePicker } from '@capawesome/capacitor-file-picker';
import { CapacitorVideoPlayer } from 'capacitor-video-player';
import { CaptureError, CaptureImageOptions, MediaCapture, MediaFile } from '@awesome-cordova-plugins/media-capture/ngx';
import { Capacitor } from '@capacitor/core';
import { File } from '@ionic-native/file/ngx';
import { Media } from '@ionic-native/media/ngx';
const config = {
quality: 0.5,
@@ -207,48 +209,47 @@ export class NewPublicationPage implements OnInit {
let options: CaptureImageOptions = { limit: 1 }
const data = await this.mediaCapture.captureVideo(options)
this.video = data[0];
/* console.log(this.video)
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);
}
} */
console.log(data)
const originalFilePath = 'file:///storage/emulated/0/DCIM/Camera/20231110_125118.mp4';
const mediaFile = new Media();
mediaFile.create(data[0].fullPath)
console.log(mediaFile)
this.mediaFileToBase64(data[0])
.then((base64Data) => {
console.log('Base64 data:', base64Data);
// Now you can use the base64 data as needed
})
.catch((error) => {
console.error('Error converting to base64:', error);
// Handle errors
});
/* this.convertVideoToBase64(data[0].fullPath) */
/* this.fileType = "video/mp4"
await Filesystem.writeFile({
path: data[0].name,
data: data[0].fullPath,
directory: FilesystemDirectory.Data,
}).then(async (dir) => {
console.log('DIR ', dir)
const base64Video = await this.convertVideoToBase64(dir.uri); */
let resultUrl = decodeURIComponent('documents://' + data[0].fullPath);
Filesystem.readFile({ path: resultUrl, encoding: Encoding.UTF8, })
.then(async (content) => {
console.log(JSON.stringify(content))
const base64Data = btoa(content.data);
console.log(base64Data);
this.filecontent = true;
this.capturedVideo = "data:video/mp4;base64," + content.data;
this.photoOrVideo = false;
})
.catch((err) => console.error(err));
/* }); */
} catch (error) {
console.log('record video error: ', error)
}
}
mediaFileToBase64(mediaFile) {
return new Promise((resolve, reject) => {
// Create a FileReader
const reader = new FileReader();
// Set the callback for when the file is loaded
reader.onloadend = () => {
resolve(reader.result);
};
// Set the callback for errors
reader.onerror = (error) => {
reject(error);
};
// Read the file as Data URL (base64)
reader.readAsDataURL(mediaFile);
});
}
async loadVideo() {
@@ -588,25 +589,25 @@ export class NewPublicationPage implements OnInit {
})
}
async copyVideoToDataDirectory(videoFile: any): Promise<string | null> {
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<string | null> {
try {
const file = await Filesystem.readFile({ path: videoPath, directory: FilesystemDirectory.Data });
@@ -620,6 +621,34 @@ export class NewPublicationPage implements OnInit {
return null;
}
}
async copyAndReadFile(filePath: string): Promise<string> {
try {
// Copy the file to the app's data directory
const copyResult = await Filesystem.copy({
from: filePath,
to: '20231110_125118.mp4', // or any desired name
directory: Directory.Data,
});
const copiedFilePath = copyResult.uri; // Use 'uri' instead of 'to'
// Read the copied file as base64
const readResult = await Filesystem.readFile({
path: copiedFilePath,
directory: Directory.Data,
encoding: Encoding.UTF8,
});
const base64Data = readResult.data;
console.log('Base64 data:', base64Data);
return base64Data;
} catch (error) {
console.error('Error copying/reading file:', error);
throw error;
}
}
}