diff --git a/src/app/pages/chat/group-messages/group-messages.page.ts b/src/app/pages/chat/group-messages/group-messages.page.ts index b89e7e8b1..8f51010bb 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -464,7 +464,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { //this.loadPicture(); } else if(res['data'] == 'add-picture'){ - this.fileService.addPictureToChat(this.roomId); + this.fileService.addPictureToChatMobile(this.roomId); //this.loadPicture(); } else if(res['data'] == 'add-document'){ diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index 7d14e8fcd..e4f933f12 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -24,6 +24,8 @@ import { Filesystem, Directory, Encoding } from '@capacitor/filesystem'; import { VoiceRecorder, VoiceRecorderPlugin, RecordingData, GenericResponse, CurrentRecordingStatus } from 'capacitor-voice-recorder'; import { Haptics, ImpactStyle } from '@capacitor/haptics'; +const IMAGE_DIR = 'stored-images'; + @Component({ selector: 'app-messages', templateUrl: './messages.page.html', @@ -101,6 +103,11 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { this.loadFiles(); VoiceRecorder.requestAudioRecordingPermission(); + Filesystem.mkdir({ + path: IMAGE_DIR, + directory: Directory.Data, + recursive: true + }); } ngAfterViewInit() { diff --git a/src/app/services/functions/file.service.ts b/src/app/services/functions/file.service.ts index 040f13324..843b12bb1 100644 --- a/src/app/services/functions/file.service.ts +++ b/src/app/services/functions/file.service.ts @@ -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; 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=> {