camera bug fix no chat

This commit is contained in:
Eudes Inácio
2021-11-21 19:49:59 +01:00
parent 6db78785e6
commit 526b31cd02
3 changed files with 156 additions and 36 deletions
+148 -35
View File
@@ -4,18 +4,29 @@ import { FileToBase64Service } from '../file/file-to-base64.service';
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';
import { ChatService } from '../chat.service';
import { ModalController } from '@ionic/angular';
import { ModalController, Platform,LoadingController } from '@ionic/angular';
import { SearchPage } from 'src/app/pages/search/search.page';
import { SearchList } from 'src/app/models/search-document';
import { ProcessesService } from '../processes.service';
import { ToastService } from '../toast.service';
import { Camera, CameraResultType, CameraSource, Photo} from '@capacitor/camera';
import { Filesystem, Directory } from '@capacitor/filesystem';
const IMAGE_DIR = 'stored-images';
interface LocalFile {
name: string;
path: string;
data: string;
}
@Injectable({
providedIn: 'root'
})
export class FileService {
images: LocalFile[] = [];
capturedImage:any;
capturedImageTitle:any;
documents:SearchList[] = [];
@@ -23,6 +34,7 @@ export class FileService {
files: Set<File>;
photos: any[] = [];
idroom: any;
constructor(
private fileLoaderService: FileLoaderService,
@@ -32,6 +44,8 @@ export class FileService {
private modalController: ModalController,
private processesService: ProcessesService,
private toastService: ToastService,
private platform: Platform,
private loadingCtrl: LoadingController
) { }
async takePicture() {
@@ -93,59 +107,157 @@ export class FileService {
};
}
//new method1
async saveImage(photo: Photo, roomid: any) {
const base64Data = await this.readAsBase64(photo);
const fileName = new Date().getTime() + '.jpeg';
const savedFile = await Filesystem.writeFile({
path: `${IMAGE_DIR}/${fileName}`,
data: base64Data,
directory: Directory.Data
});
this.loadFiles(roomid);
}
//new method 2
private async readAsBase64(photo: Photo) {
if (this.platform.is('hybrid')) {
const file = await Filesystem.readFile({
path: photo.path
});
return file.data;
}
else {
// Fetch the photo, read as a blob, then convert to base64 format
const response = await fetch(photo.webPath);
const blob = await response.blob();
return await this.convertBlobToBase64(blob) as string;
}
}
//new method 3
async loadFiles(roomid) {
this.images = [];
const loading = await this.loadingCtrl.create({
message: 'Loading data...',
});
await loading.present();
Filesystem.readdir({
path: IMAGE_DIR,
directory: Directory.Data,
}).then(result => {
console.log('ALL RESULTS', result.files[0])
let lastphoto = result.files[result.files.length - 1]
this.loadFileData(lastphoto,roomid);
},
async (err) => {
console.log('ERROR FILE DOSENT EXIST', err)
// Folder does not yet exists!
await Filesystem.mkdir({
path: IMAGE_DIR,
directory: Directory.Data,
recursive: true
});
}
).then(_ => {
loading.dismiss();
});
}
//new method 4
async loadFileData(fileName: string, roomid: any) {
console.log('ALL PHOTOT FILE', fileName)
// for (let f of fileNames) {
const filePath = `${IMAGE_DIR}/${fileName}`;
const readFile = await Filesystem.readFile({
path: filePath,
directory: Directory.Data,
});
this.images.push({
name: fileName,
path: filePath,
data: `data:image/jpeg;base64,${readFile.data}`,
});
console.log('ALL IMAGE', this.images)
this.capturedImage = this.images[0].data
this.capturedImageTitle = new Date().getTime() + '.jpeg';
let body = {
"message":
{
"rid": roomid,
"msg": "",
"attachments": [{
"title": this.capturedImageTitle,
"title_link_download": false,
"image_url": this.capturedImage,
}]
}
}
console.log('BODY TAKE PICTURE CHAT', body)
const loader = this.toastService.loading();
this.chatService.sendMessage(body).subscribe(res=> {
console.log(res);
loader.remove();
},(error) => {
loader.remove();
this.toastService.badRequest("Não foi possível adicionar a fotografia!");
});
}
async addCameraPictureToChat(roomId){
const capturedImage = await Camera.getPhoto({
quality: 90,
// allowEditing: true,
const image = await Camera.getPhoto({
quality: 50,
allowEditing: false,
resultType: CameraResultType.Uri,
source: CameraSource.Camera
source: CameraSource.Camera // Camera, Photos or Prompt!
});
const response = await fetch(capturedImage.webPath!);
if (image) {
await this.saveImage(image,roomId)
}
/* const response = await fetch(capturedImage.webPath!);
const blob = await response.blob();
this.photos.unshift({
filepath: "soon...",
webviewPath: capturedImage.webPath
});
}); */
this.capturedImage = await this.convertBlobToBase64(blob);
this.capturedImageTitle = new Date().getTime() + '.jpeg';
let body = {
"message":
{
"rid": roomId,
"msg": "",
"attachments": [{
"title": this.capturedImageTitle,
"title_link_download": false,
"image_url": this.capturedImage,
}]
}
}
const loader = this.toastService.loading();
this.chatService.sendMessage(body).subscribe(res=> {
console.log(res);
loader.remove();
},(error) => {
loader.remove();
this.toastService.badRequest("Não foi possível adicionar a fotografia!");
});
//this.capturedImage = this.capturedImage;
}
async addPictureToChatMobile(roomId) {
const capturedImage = await Camera.getPhoto({
quality: 90,
quality: 50,
// allowEditing: true,
resultType: CameraResultType.Uri,
source: CameraSource.Photos
});
const response = await fetch(capturedImage.webPath!);
if (capturedImage) {
await this.saveImage(capturedImage,roomId)
}
/* const response = await fetch(capturedImage.webPath!);
const blob = await response.blob();
this.photos.unshift({
@@ -177,7 +289,7 @@ export class FileService {
},(error) => {
//loader.remove();
});
}
*/ }
addPictureToChat(roomId) {
@@ -193,7 +305,7 @@ export class FileService {
input.onchange = async () => {
alert('Onchange AQUI')
//alert('Onchange AQUI')
const file = this.fileLoaderService.getFirstFile(input)
@@ -218,6 +330,7 @@ export class FileService {
}
}
console.log('SELECT PICTURE GALLERY', body)
console.log(this.capturedImage)
this.chatService.sendMessage(body).subscribe(res=> {