upload profile picture

This commit is contained in:
Eudes Inácio
2023-09-09 14:40:08 +01:00
parent fb1bd07ad0
commit 42c2fc92ff
5 changed files with 94 additions and 47 deletions
@@ -10,6 +10,7 @@ import { Camera, CameraResultType, CameraSource, Photo } from '@capacitor/camera
import { Filesystem, Directory } from '@capacitor/filesystem';
import { File } from '@awesome-cordova-plugins/file/ngx';
import { StorageService } from 'src/app/services/storage.service';
import { AttachmentsService } from 'src/app/services/attachments.service';
@Component({
@@ -33,20 +34,42 @@ export class EditProfilePage implements OnInit {
public ThemeService: ThemeService,
private file: File,
private storageService: StorageService,
private attachmentService: AttachmentsService
) { }
ngOnInit() {
this.getProfilpicture()
ngOnInit() {
this.getProfilpictureFromStorage()
}
getProfilpicture() {
getProfilpictureFromStorage() {
this.storageService.get(this.SessionStore.user.RoleID.toString()).then((picture) => {
console.log(picture)
this.profilePicture = picture
}).catch((error ) => {
}).catch((error) => {
this.profilePicture = "";
})
}
getProfilpicture(guid) {
console.log('Get picture ', guid.path)
this.attachmentService.downloadFile(guid.path).subscribe(async (picture: any) => {
let downloadFile = 'data:image/jpeg;base64,' + btoa(new Uint8Array(picture.body).reduce((data, byte) => data + String.fromCharCode(byte), ''));
console.log('Get picture ', downloadFile)
this.storageService.store(this.SessionStore.user.RoleID.toString()+"guid", guid.path)
this.storageService.store(this.SessionStore.user.RoleID.toString(), downloadFile).then((value) => {
console.log('picture saved')
}).catch((error) => {
console.log('picture not saved')
});
}, ((error) => {
console.log('Error get profile picture: ', error)
}))
}
close() {
this.modalController.dismiss();
@@ -146,50 +169,32 @@ export class EditProfilePage implements OnInit {
async takePicture() {
const capturedImage = await Camera.getPhoto({
width: 188,
height: 188,
width: 250,
height: 250,
quality: 100,
// allowEditing: true,
resultType: CameraResultType.Base64,
source: CameraSource.Camera
});
this.capturedImage = 'data:image/jpeg;base64,' + capturedImage.base64String;
console.log(this.SessionStore.user.RoleID.toString())
this.storageService.store(this.SessionStore.user.RoleID.toString(),this.capturedImage).then((value) => {
console.log('picture saved')
}).catch((error) => {
console.log('picture not saved')
})
this.getProfilpicture()
this.capturedImageTitle = SessionStore.user.Profile;
/* this.saveFile(this.capturedImage,this.capturedImageTitle,'application/img'); */
this.capturedImage = 'data:image/jpeg;base64,' + capturedImage.base64String;
const blob = this.dataURItoBlob(this.capturedImage)
const formData = new FormData();
formData.append("blobFile", blob);
this.attachmentService.uploadFile(formData).subscribe((guid) => {
console.log('GUID ', guid)
console.log(this.SessionStore.user.RoleID.toString())
this.getProfilpicture(guid);
}, ((error) => {
console.log('Erro Upload profile picture ',error)
}))
}
/* async saveFile(pdfString, filename, type) {
const blob = this.b64toBlob(pdfString, type)
console.log(blob)
let pathFile = ''
const fileName = filename
const contentFile = blob
if (this.platform.is('ios')) {
pathFile = this.file.documentsDirectory
} else {
pathFile = this.file.externalRootDirectory
}
console.log(pathFile)
await Filesystem.writeFile({
path: fileName,
data: pdfString,
directory: Directory.Documents,
}).then((dir) => {
console.log('DIR ', dir)
});
} */
b64toBlob(b64Data, contentType) {
contentType = contentType || '';
@@ -216,5 +221,30 @@ export class EditProfilePage implements OnInit {
return blob;
}
dataURItoBlob(dataURI) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
var byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
// create a view into the buffer
var ia = new Uint8Array(ab);
// set the bytes of the buffer to the correct values
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
var blob = new Blob([ab], { type: mimeString });
return blob;
}
}
+13
View File
@@ -253,6 +253,19 @@ export class ProfilePage implements OnInit {
else if (Service === "gabinete-digital" && Object === "despachos-pr") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/despachos-pr', IdObject, 'gabinete-digital']));
}else if (Service === "gabinete-digital" && Object === "diplomas") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/diplomas', IdObject, 'gabinete-digital']));
}
else if (Service === "gabinete-digital" && Object === "diplomas-assinar") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/diplomas-assinar', IdObject, 'gabinete-digital']));
}
else if (Service === "gabinete-digital" && Object === "diploma-revisao") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/diplomas', IdObject, 'gabinete-digital']));
}
else if (Service === "gabinete-digital" && Object === "expedientes-pr") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/expedientes-pr', IdObject, 'gabinete-digital']));
}
this.deleteNotification(index);