This commit is contained in:
Peter Maquiran
2022-02-04 07:42:43 +01:00
parent 7ee2289ca0
commit 1ffeb3df5c
7 changed files with 135 additions and 272 deletions
@@ -29,7 +29,7 @@ import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.serv
import { AttachmentsService } from 'src/app/services/attachments.service';
import { FileType } from 'src/app/models/fileType';
import { Storage } from '@ionic/storage';
import { FileSystemService } from 'src/app/services/file-system.service';
import { CameraService } from 'src/app/services/camera.service';
import { SearchPage } from 'src/app/pages/search/search.page';
import { ProcessesService } from 'src/app/services/processes.service';
@@ -98,7 +98,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy {
private AttachmentsService: AttachmentsService,
private storage: Storage,
private processesService: ProcessesService,
private FileSystemService: FileSystemService,
private CameraService: CameraService,
) {
this.loggedUserChat = authService.ValidatedUserChat['data'];
@@ -499,9 +499,9 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy {
const roomId = this.roomId
const image = await this.CameraService.takePicture();
await this.FileSystemService.saveImage(image, roomId)
const lastphoto: any = await this.FileSystemService.loadFiles(roomId);
const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId);
await this.fileService.saveImage(image)
const lastphoto: any = await this.fileService.loadFiles();
const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto);
const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({
file: {
@@ -689,9 +689,9 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy {
}
else if(res['data'] == 'take-picture') {
const image = await this.CameraService.takePicture();
await this.FileSystemService.saveImage(image, roomId)
const lastphoto: any = await this.FileSystemService.loadFiles(roomId);
const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId);
await this.fileService.saveImage(image)
const lastphoto: any = await this.fileService.loadFiles();
const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto);
const base64 = await fetch(capturedImage);
const blob = await base64.blob();
+8 -8
View File
@@ -32,7 +32,7 @@ import { ViewEventPage } from 'src/app/modals/view-event/view-event.page';
import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.service'
import { MessageService } from 'src/app/services/chat/message.service';
import { AttachmentsService } from 'src/app/services/attachments.service';
import { FileSystemService } from 'src/app/services/file-system.service';
import { CameraService } from 'src/app/services/camera.service';
import { element } from 'protractor';
import { FileType } from 'src/app/models/fileType';
@@ -108,7 +108,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
private sqlservice: SqliteService,
public wsChatMethodsService: WsChatMethodsService,
private AttachmentsService: AttachmentsService,
private FileSystemService: FileSystemService,
private CameraService: CameraService,
private processesService: ProcessesService,
private storage: Storage,
@@ -522,9 +522,9 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
const roomId = this.roomId
const image = await this.CameraService.takePicture();
await this.FileSystemService.saveImage(image, roomId)
const lastphoto: any = await this.FileSystemService.loadFiles(roomId);
const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId);
await this.fileService.saveImage(image)
const lastphoto: any = await this.fileService.loadFiles();
const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto);
const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({
file: {
@@ -712,9 +712,9 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
}
else if (res['data'] == 'take-picture') {
const image = await this.CameraService.takePicture();
await this.FileSystemService.saveImage(image, roomId)
const lastphoto: any = await this.FileSystemService.loadFiles(roomId);
const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId);
await this.fileService.saveImage(image)
const lastphoto: any = await this.fileService.loadFiles();
const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto);
const base64 = await fetch(capturedImage);
const blob = await base64.blob();
@@ -1,16 +0,0 @@
import { TestBed } from '@angular/core/testing';
import { FileSystemService } from './file-system.service';
describe('FileSystemService', () => {
let service: FileSystemService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(FileSystemService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
-145
View File
@@ -1,145 +0,0 @@
import { Injectable } from '@angular/core';
import { ModalController, Platform,LoadingController } from '@ionic/angular';
import { Filesystem, Directory } from '@capacitor/filesystem';
import { Camera, CameraResultType, CameraSource, Photo} from '@capacitor/camera';
import { ChatService } from './chat.service';
import { AttachmentsService } from 'src/app/services/attachments.service';
import { ToastService } from './toast.service';
import { environment } from 'src/environments/environment';
import { HttpClient, HttpEventType, HttpHeaders, HttpParams } from '@angular/common/http';
import { Storage } from '@ionic/storage';
const IMAGE_DIR = 'stored-images';
interface LocalFile {
name: string;
path: string;
data: string;
}
@Injectable({
providedIn: 'root'
})
export class FileSystemService {
images: LocalFile[] = [];
constructor(
private platform: Platform,
private loadingCtrl: LoadingController,
private chatService: ChatService,
private AttachmentsService: AttachmentsService,
private toastService: ToastService,
private storage: Storage,) { }
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
});
}
//new method 3
async loadFiles(roomid) {
this.images = [];
const loading = await this.loadingCtrl.create({
message: 'Loading data...',
});
await loading.present();
return new Promise((resolve, reject)=>{
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]
resolve(lastphoto)
},
async (err) => {
console.log('ERROR FILE DOSENT EXIST', err)
reject('ERROR FILE DOSENT EXIST')
// Folder does not yet exists!
await Filesystem.mkdir({
path: IMAGE_DIR,
directory: Directory.Data,
recursive: true
});
}
).then(_ => {
loading.dismiss();
});
})
}
//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;
}
}
convertBlobToBase64 = (blob: Blob) => new Promise((resolve, reject) => {
const reader = new FileReader;
reader.onerror = reject;
reader.onload = () => {
resolve(reader.result);
};
reader.readAsDataURL(blob);
});
async readFile(filePath) {
return await Filesystem.readFile({
path: filePath,
directory: Directory.Data,
});
}
//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 this.readFile(filePath)
this.images.push({
name: fileName,
path: filePath,
data: `data:image/jpeg;base64,${readFile.data}`,
});
console.log('ALL IMAGE', this.images)
const capturedImage = this.images[0].data
const capturedImageTitle = new Date().getTime() + '.jpeg';
return { capturedImage, capturedImageTitle}
}
}
+106 -82
View File
@@ -3,13 +3,14 @@ import { FileLoaderService } from '../file/file-loader.service';
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 { LoadingController, ModalController, Platform } from '@ionic/angular';
import { SearchPage } from 'src/app/pages/search/search.page';
import { ProcessesService } from '../processes.service';
import { ToastService } from '../toast.service';
import { Camera, CameraResultType, CameraSource} from '@capacitor/camera';
import { Camera, CameraResultType, CameraSource, Photo} from '@capacitor/camera';
import { FileType } from 'src/app/models/fileType';
import { FileSystemService } from 'src/app/services/file-system.service';
import { Filesystem, Directory } from '@capacitor/filesystem';
const IMAGE_DIR = 'stored-images';
@@ -44,22 +45,12 @@ export class FileService {
private modalController: ModalController,
private processesService: ProcessesService,
private toastService: ToastService,
private FileSystemService: FileSystemService
private loadingCtrl: LoadingController,
private platform: Platform,
) {}
_arrayBufferToBase64( buffer ) {
var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
}
convertBlobToBase64 = (blob: Blob) => new Promise((resolve, reject) => {
const reader = new FileReader;
reader.onerror = reject;
@@ -69,73 +60,7 @@ export class FileService {
reader.readAsDataURL(blob);
});
async loadPicture() {
const input = this.fileLoaderService.createInput({
accept: ['image/apng', 'image/jpeg', 'image/png', '.pdf']
})
input.onchange = async () => {
const file = this.fileLoaderService.getFirstFile(input)
console.log(file);
const imageData = await this.fileToBase64Service.convert(file)
this.capturedImage = imageData;
this.capturedImageTitle = file.name;
let data = {
image:this.capturedImage,
name: this.capturedImageTitle
}
return data;
};
}
async addPictureToChatMobile(roomId) {
const capturedImage = await Camera.getPhoto({
quality: 50,
// allowEditing: true,
resultType: CameraResultType.Uri,
source: CameraSource.Camera
});
if (capturedImage) {
await this.FileSystemService.saveImage(capturedImage,roomId)
}
}
addDocumentToChat(roomId:string) {
const input = this.fileLoaderService.createInput({
accept: ['.doc', '.docx', '.pdf']
})
input.onchange = async () => {
const loader = this.toastService.loading();
const file = this.fileLoaderService.getFirstFile(input)
console.log(file);
const formData = new FormData();
formData.append('file', file, file.name);
this.chatService.uploadFile(formData, roomId).subscribe(res=> {
console.log(res);
loader.remove();
},(error) => {
loader.remove();
});
//console.log(this.capturedImage)
};
}
getFileFromDevice(types: typeof FileType[]) {
return new Promise<any>((resolve, reject)=>{
const input = this.fileLoaderService.createInput({
@@ -155,4 +80,103 @@ export class FileService {
const browser = this.iab.create(url,"_parent");
browser.show();
}
async saveImage(photo: Photo) {
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
});
}
//new method 3
async loadFiles() {
const loading = await this.loadingCtrl.create({
message: 'Loading data...',
});
await loading.present();
return new Promise((resolve, reject)=>{
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]
resolve(lastphoto)
},
async (err) => {
console.log('ERROR FILE DOSENT EXIST', err)
reject('ERROR FILE DOSENT EXIST')
// Folder does not yet exists!
await Filesystem.mkdir({
path: IMAGE_DIR,
directory: Directory.Data,
recursive: true
});
}
).then(_ => {
loading.dismiss();
});
})
}
//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;
}
}
async readFile(filePath) {
return await Filesystem.readFile({
path: filePath,
directory: Directory.Data,
});
}
//new method 4
async loadFileData(fileName: string) {
console.log('ALL PHOTOT FILE', fileName)
// for (let f of fileNames) {
const filePath = `${IMAGE_DIR}/${fileName}`;
const readFile = await this.readFile(filePath)
const image ={
name: fileName,
path: filePath,
data: `data:image/jpeg;base64,${readFile.data}`,
};
console.log('ALL IMAGE', image)
const capturedImage = image.data
const capturedImageTitle = new Date().getTime() + '.jpeg';
return { capturedImage, capturedImageTitle}
}
}
@@ -27,7 +27,7 @@ import { Storage } from '@ionic/storage';
import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.service';
import { MessageService } from 'src/app/services/chat/message.service';
import { AttachmentsService } from 'src/app/services/attachments.service';
import { FileSystemService } from 'src/app/services/file-system.service';
import { CameraService } from 'src/app/services/camera.service';
import { element } from 'protractor';
import { FileType } from 'src/app/models/fileType';
@@ -103,7 +103,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
private changeDetectorRef: ChangeDetectorRef,
private storage: Storage,
private AttachmentsService: AttachmentsService,
private FileSystemService: FileSystemService,
private CameraService: CameraService,
private toastService: ToastService,
@@ -670,9 +670,9 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
const roomId = this.roomId
const image = await this.CameraService.takePicture();
await this.FileSystemService.saveImage(image, roomId)
const lastphoto: any = await this.FileSystemService.loadFiles(roomId);
const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId);
await this.fileService.saveImage(image)
const lastphoto: any = await this.fileService.loadFiles();
const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto);
const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({
file: {
@@ -896,9 +896,9 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
else if (res['data'] == 'take-picture') {
const image = await this.CameraService.takePicture();
await this.FileSystemService.saveImage(image, roomId)
const lastphoto: any = await this.FileSystemService.loadFiles(roomId);
const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId);
await this.fileService.saveImage(image)
const lastphoto: any = await this.fileService.loadFiles();
const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto);
const base64 = await fetch(capturedImage);
const blob = await base64.blob();
@@ -27,7 +27,7 @@ import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.serv
import { WsChatService } from 'src/app/services/chat/ws-chat.service'
import { MessageService } from 'src/app/services/chat/message.service';
import { AttachmentsService } from 'src/app/services/attachments.service';
import { FileSystemService } from 'src/app/services/file-system.service';
import { CameraService } from 'src/app/services/camera.service';
import { FileType } from 'src/app/models/fileType';
import { SearchPage } from 'src/app/pages/search/search.page';
@@ -100,7 +100,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
public wsChatMethodsService: WsChatMethodsService,
public WsChatService: WsChatService,
private AttachmentsService: AttachmentsService,
private FileSystemService: FileSystemService,
private CameraService: CameraService,
private processesService: ProcessesService,
private fileToBase64Service: FileToBase64Service,
@@ -446,9 +446,9 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
const roomId = this.roomId
const image = await this.CameraService.takePicture();
await this.FileSystemService.saveImage(image, roomId)
const lastphoto: any = await this.FileSystemService.loadFiles(roomId);
const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId);
await this.fileService.saveImage(image)
const lastphoto: any = await this.fileService.loadFiles();
const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto);
const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({
file: {