mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
update
This commit is contained in:
@@ -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}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user