Files
doneit-web/src/app/services/functions/file.service.ts
T
tiago.kayaya b0c9c27890 save
2021-10-09 20:29:52 +01:00

329 lines
8.9 KiB
TypeScript

import { Injectable } from '@angular/core';
import { FileLoaderService } from '../file/file-loader.service';
import { FileToBase64Service } from '../file/file-to-base64.service';
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';
//Cordova
import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
import { ChatService } from '../chat.service';
import { ModalController } 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';
@Injectable({
providedIn: 'root'
})
export class FileService {
capturedImage:any;
capturedImageTitle:any;
documents:SearchList[] = [];
showLoader: boolean;
files: Set<File>;
constructor(
private camera: Camera,
private fileLoaderService: FileLoaderService,
private fileToBase64Service: FileToBase64Service,
private iab: InAppBrowser,
private chatService: ChatService,
private modalController: ModalController,
private processesService: ProcessesService,
private toastService: ToastService,
) { }
takePicture() {
const options: CameraOptions = {
quality: 50,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
targetWidth: 720,
targetHeight: 720,
}
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64 (DATA_URL): m
//let base64Image = 'data:image/jpeg;base64,' + imageData;
this.capturedImage = 'data:image/png;base64,'+imageData;
this.capturedImageTitle = new Date().getTime() + '.jpeg';
}, (err) => {
/* console.log(err); */
});
let data = {
image:this.capturedImage,
name: this.capturedImageTitle
}
return data;
}
loadPicture() {
const input = this.fileLoaderService.createInput({
accept: ['image/apng', 'image/jpeg', 'image/png']
})
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;
};
}
addCameraPictureToChat(roomId){
const options: CameraOptions = {
quality: 50,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
targetWidth: 720,
targetHeight: 720,
}
this.camera.getPicture(options).then((imageData) => {
this.capturedImage = 'data:image/png;base64,'+imageData;
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!");
});
}, (err) => {
this.toastService.badRequest("Não foi possível adicionar a fotografia!");
});
}
addPictureToChatMobile(roomId) {
alert('Here')
const options: CameraOptions = {
quality: 90,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
targetWidth: 720,
targetHeight: 720,
correctOrientation: true
}
this.camera.getPicture(options).then((imageData) => {
let base64Image = 'data:image/jpeg;base64,' + imageData;
this.capturedImage = imageData;
this.capturedImageTitle = new Date().getTime() + '.jpeg';
//const loader = this.toastService.loading();
let body = {
"message":
{
"rid": roomId,
"msg": "",
"attachments": [{
//"title": this.capturedImageTitle ,
//"text": "description",
"title_link_download": false,
"image_url": this.capturedImage,
}]
}
}
console.log(this.capturedImage)
this.chatService.sendMessage(body).subscribe(res=> {
//loader.remove();
//console.log(res);
},(error) => {
//loader.remove();
});
}, (err) => {
//console.log(err);
});
}
addPictureToChat(roomId) {
const input = this.fileLoaderService.createInput({
accept: ['image/apng', 'image/jpeg', 'image/png']
})
setInterval(()=>{
console.log(input.value)
}, 550)
input.onchange = async () => {
alert('Onchange AQUI')
const file = this.fileLoaderService.getFirstFile(input)
console.log(file);
const loader = this.toastService.loading();
const imageData = await this.fileToBase64Service.convert(file)
this.capturedImage = imageData;
this.capturedImageTitle = file.name;
let body = {
"message":
{
"rid": roomId,
"msg": "",
"attachments": [{
//"title": this.capturedImageTitle ,
//"text": "description",
"title_link_download": false,
"image_url": this.capturedImage,
}]
}
}
console.log(this.capturedImage)
this.chatService.sendMessage(body).subscribe(res=> {
loader.remove();
//console.log(res);
},(error) => {
loader.remove();
});
//console.log(this.capturedImage)
};
}
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)
};
}
async addDocGestaoDocumentalToChat(roomId:string){
const modal = await this.modalController.create({
component: SearchPage,
cssClass: 'group-messages modal-desktop search-modal search-modal-to-desktop',
componentProps: {
type: 'AccoesPresidenciais & ArquivoDespachoElect',
select: true,
showSearchInput: true,
}
});
await modal.present();
modal.onDidDismiss().then(async res=>{
const data = res.data;
alert('HERE')
if(data.selected){
const loader = this.toastService.loading();
this.documents.push(data.selected);
console.log(res.data.selected);
console.log(res.data.selected.Id);
console.log(res.data.selected.ApplicationType);
console.log('AQUIIIII');
alert('HERE 2')
let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise();
let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1");
console.log(url_no_options);
console.log('Oie');
let body = {
"message":
{
"rid": roomId,
"msg": "",
"attachments": [{
"title": res.data.selected.Assunto,
"description": res.data.selected.DocTypeDesc,
"title_link": url_no_options,
"title_link_download": true,
//"thumb_url": "assets/images/webtrix-logo.png",
"message_link": url_no_options,
"type": "webtrix"
}],
"file":{
"name": res.data.selected.Assunto,
"type": "application/webtrix",
"ApplicationId": res.data.selected.ApplicationType,
"DocId": res.data.selected.Id,
"Assunto": res.data.selected.Assunto,
"title_link": url_no_options,
}
}
}
this.chatService.sendMessage(body).subscribe(res=> {
loader.remove();
console.log(res);
},(error) => {
loader.remove();
});
loader.remove();
}
});
}
viewDocumentByUrl(url) {
const browser = this.iab.create(url,"_blank");
browser.show();
}
}