Files
doneit-web/src/app/services/functions/file.service.ts
T

159 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-09-13 12:37:58 +01:00
import { Injectable } from '@angular/core';
import { FileLoaderService } from '../file/file-loader.service';
import { FileToBase64Service } from '../file/file-to-base64.service';
2021-09-14 15:57:24 +01:00
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';
2021-09-21 14:05:59 +01:00
import { ChatService } from '../chat.service';
2022-02-03 21:01:53 +01:00
import { ModalController } from '@ionic/angular';
2021-09-21 14:05:59 +01:00
import { SearchPage } from 'src/app/pages/search/search.page';
import { ProcessesService } from '../processes.service';
import { ToastService } from '../toast.service';
2022-02-03 21:01:53 +01:00
import { Camera, CameraResultType, CameraSource} from '@capacitor/camera';
import { FileType } from 'src/app/models/fileType';
import { FileSystemService } from 'src/app/services/file-system.service';
2021-11-21 19:49:59 +01:00
const IMAGE_DIR = 'stored-images';
2021-12-03 17:27:10 +01:00
2021-11-21 19:49:59 +01:00
interface LocalFile {
name: string;
path: string;
data: string;
}
2021-09-13 12:37:58 +01:00
@Injectable({
providedIn: 'root'
})
export class FileService {
2021-11-21 19:49:59 +01:00
images: LocalFile[] = [];
2021-09-13 12:37:58 +01:00
capturedImage:any;
capturedImageTitle:any;
2021-09-21 14:05:59 +01:00
showLoader: boolean;
2021-09-13 12:37:58 +01:00
files: Set<File>;
2021-11-09 15:28:40 +01:00
photos: any[] = [];
2021-11-21 19:49:59 +01:00
idroom: any;
2021-12-16 16:36:39 +01:00
downloadProgess = 0;
downloadFilename: any;
2021-12-06 16:00:57 +01:00
2021-09-13 12:37:58 +01:00
constructor(
private fileLoaderService: FileLoaderService,
private fileToBase64Service: FileToBase64Service,
2021-09-14 15:57:24 +01:00
private iab: InAppBrowser,
2021-09-21 14:05:59 +01:00
private chatService: ChatService,
private modalController: ModalController,
private processesService: ProcessesService,
private toastService: ToastService,
2022-02-03 21:01:53 +01:00
private FileSystemService: FileSystemService
) {}
2021-12-14 14:58:34 +01:00
2021-12-16 16:36:39 +01:00
_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 );
}
2021-09-14 10:30:13 +01:00
2021-11-09 15:28:40 +01:00
convertBlobToBase64 = (blob: Blob) => new Promise((resolve, reject) => {
const reader = new FileReader;
reader.onerror = reject;
reader.onload = () => {
resolve(reader.result);
};
reader.readAsDataURL(blob);
});
2021-12-06 16:00:57 +01:00
async loadPicture() {
2021-09-13 12:37:58 +01:00
const input = this.fileLoaderService.createInput({
2021-12-06 16:00:57 +01:00
accept: ['image/apng', 'image/jpeg', 'image/png', '.pdf']
2021-09-13 12:37:58 +01:00
})
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;
};
}
2021-09-14 15:57:24 +01:00
2021-10-08 15:37:24 +01:00
2021-11-09 15:28:40 +01:00
async addPictureToChatMobile(roomId) {
2021-10-08 15:37:24 +01:00
2021-11-09 15:28:40 +01:00
const capturedImage = await Camera.getPhoto({
2021-11-21 19:49:59 +01:00
quality: 50,
2021-11-09 15:28:40 +01:00
// allowEditing: true,
resultType: CameraResultType.Uri,
2021-11-23 10:57:07 +01:00
source: CameraSource.Camera
2021-10-08 15:37:24 +01:00
2021-11-09 15:28:40 +01:00
});
2021-11-21 19:49:59 +01:00
if (capturedImage) {
2022-02-03 21:01:53 +01:00
await this.FileSystemService.saveImage(capturedImage,roomId)
2021-11-21 19:49:59 +01:00
}
2021-12-03 17:27:10 +01:00
2022-02-03 21:01:53 +01:00
}
2021-12-03 17:27:10 +01:00
2021-09-21 14:05:59 +01:00
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);
2021-09-21 14:05:59 +01:00
this.chatService.uploadFile(formData, roomId).subscribe(res=> {
console.log(res);
2021-09-21 14:05:59 +01:00
loader.remove();
},(error) => {
loader.remove();
});
//console.log(this.capturedImage)
};
}
2022-02-03 21:01:53 +01:00
getFileFromDevice(types: typeof FileType[]) {
return new Promise<any>((resolve, reject)=>{
const input = this.fileLoaderService.createInput({
accept: types
})
input.onchange = async () => {
const file = this.fileLoaderService.getFirstFile(input)
resolve(file)
};
})
2021-09-21 14:05:59 +01:00
}
2022-02-03 21:01:53 +01:00
2021-09-14 15:57:24 +01:00
viewDocumentByUrl(url) {
2021-10-18 17:10:33 +01:00
const browser = this.iab.create(url,"_parent");
2021-09-14 15:57:24 +01:00
browser.show();
}
2021-09-13 12:37:58 +01:00
}