This commit is contained in:
Peter Maquiran
2023-08-28 13:00:51 +01:00
parent 774178108c
commit fbc02a0585
3 changed files with 132 additions and 52 deletions
@@ -435,15 +435,14 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
if (recordData?.value?.recordDataBase64.includes('data:audio')) { if (recordData?.value?.recordDataBase64.includes('data:audio')) {
this.audioRecorded = recordData?.value?.recordDataBase64; this.audioRecorded = recordData?.value?.recordDataBase64;
} }
else if(recordData?.value?.mimeType && recordData?.value?.recordDataBase64) { else if (recordData?.value?.mimeType && recordData?.value?.recordDataBase64) {
this.audioRecorded = `data:${recordData.value.mimeType};base64,${recordData?.value?.recordDataBase64}`; this.audioRecorded = `data:${recordData.value.mimeType};base64,${recordData?.value?.recordDataBase64}`;
} }
//Converting base64 to blob //Converting base64 to blob
const encodedData = btoa(this.audioRecorded); const encodedData = btoa(this.audioRecorded);
const blob = this.fileService.base64toBlob(encodedData, recordData.value.mimeType) const blob = this.fileService.base64toBlob(encodedData, recordData.value.mimeType)
const formData = new FormData(); const formData = new FormData();
formData.append("blobFile", blob); formData.append("blobFile", blob);
@@ -460,7 +459,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
}], }],
temporaryData: formData, temporaryData: formData,
attachmentsModelData: { attachmentsModelData: {
fileBase64: await this.fileService.blobToBase64(blob), fileBase64: encodedData,
} }
}) })
@@ -715,12 +714,11 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
resultType: CameraResultType.Base64, resultType: CameraResultType.Base64,
source: CameraSource.Camera source: CameraSource.Camera
}); });
//const imageData = await this.fileToBase64Service.convert(file)
//
const response = await fetch('data:image/jpeg;base64,' + file.base64String!); const imageBase64 = 'data:image/jpeg;base64,' + file.base64String
const blob = await response.blob(); const blob = this.dataURItoBlob(imageBase64)
console.log(imageBase64)
const formData = new FormData(); const formData = new FormData();
formData.append("blobFile", blob); formData.append("blobFile", blob);
@@ -738,22 +736,39 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
"title_link_download": false, "title_link_download": false,
}], }],
attachmentsModelData: { attachmentsModelData: {
fileBase64: await this.fileService.blobToBase64(blob), fileBase64: imageBase64,
} }
}) })
} }
async takePicture() { async takePicture() {
// const roomId = this.roomId
// const image = await this.CameraService.takePicture();
// 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();
// const formData = new FormData();
// formData.append("blobFile", blob);
const roomId = this.roomId const roomId = this.roomId
const image = await this.CameraService.takePicture(); const file = await Camera.getPhoto({
await this.fileService.saveImage(image) quality: 90,
const lastphoto: any = await this.fileService.loadFiles(); // allowEditing: true,
const { capturedImage, capturedImageTitle } = await this.fileService.loadFileData(lastphoto); resultType: CameraResultType.Base64,
source: CameraSource.Camera
});
const imageBase64 = 'data:image/jpeg;base64,' + file.base64String
const blob = this.dataURItoBlob(imageBase64)
console.log(imageBase64)
const base64 = await fetch(capturedImage);
const blob = await base64.blob();
const formData = new FormData(); const formData = new FormData();
formData.append("blobFile", blob); formData.append("blobFile", blob);
@@ -763,13 +778,13 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
"guid": '' "guid": ''
}, },
attachments: [{ attachments: [{
"title": capturedImageTitle, "title": 'file.jpg',
"text": "description", "text": "description",
"title_link_download": false, "title_link_download": false,
}], }],
temporaryData: formData, temporaryData: formData,
attachmentsModelData: { attachmentsModelData: {
fileBase64: await this.fileService.blobToBase64(blob), fileBase64: imageBase64,
} }
}) })
@@ -837,14 +852,13 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
quality: 90, quality: 90,
// allowEditing: true, // allowEditing: true,
resultType: CameraResultType.Base64, resultType: CameraResultType.Base64,
source: CameraSource.Photos source: CameraSource.Camera
}); });
//const imageData = await this.fileToBase64Service.convert(file)
//
const response = await fetch('data:image/jpeg;base64,' + file.base64String!); const imageBase64 = 'data:image/jpeg;base64,' + file.base64String
const blob = await response.blob(); const blob = this.dataURItoBlob(imageBase64)
console.log(imageBase64)
const formData = new FormData(); const formData = new FormData();
formData.append("blobFile", blob); formData.append("blobFile", blob);
@@ -862,7 +876,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
"title_link_download": false, "title_link_download": false,
}], }],
attachmentsModelData: { attachmentsModelData: {
fileBase64: await this.fileService.blobToBase64(blob), fileBase64: imageBase64,
} }
}) })
@@ -874,6 +888,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
const roomId = this.roomId const roomId = this.roomId
const file: any = await this.fileService.getFileFromDevice(types); const file: any = await this.fileService.getFileFromDevice(types);
if (file.type != "application/img" && file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/gif") { if (file.type != "application/img" && file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/gif") {
@@ -884,8 +899,10 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
const blob = this.fileService.base64toBlob(encodedData, file.type) const blob = this.fileService.base64toBlob(encodedData, file.type)
const fileBase64 = await this._getBase64(file)
const formData = new FormData(); const formData = new FormData();
formData.append("blobFile", blob); formData.append('blobFile', blob);
this.ChatSystemService.getGroupRoom(roomId).send({ this.ChatSystemService.getGroupRoom(roomId).send({
file: { file: {
@@ -901,7 +918,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
}], }],
temporaryData: formData, temporaryData: formData,
attachmentsModelData: { attachmentsModelData: {
fileBase64: await this.fileService.blobToBase64(blob), fileBase64: fileBase64,
} }
}) })
@@ -1123,5 +1140,45 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
} else { } } else { }
} }
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;
}
_getBase64(file) {
return new Promise((resolve, reject)=>{
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
resolve(reader.result)
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
})
}
} }
+40 -17
View File
@@ -645,8 +645,10 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
source: CameraSource.Camera source: CameraSource.Camera
}); });
const base64 = 'data:image/jpeg;base64,' + file.base64String const imageBase64 = 'data:image/jpeg;base64,' + file.base64String
const blob = this.dataURItoBlob(base64) const blob = this.dataURItoBlob(imageBase64)
console.log(imageBase64)
const formData = new FormData(); const formData = new FormData();
formData.append("blobFile", blob); formData.append("blobFile", blob);
@@ -665,7 +667,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
"title_link_download": false, "title_link_download": false,
}], }],
attachmentsModelData: { attachmentsModelData: {
fileBase64: await this.fileService.blobToBase64(blob), fileBase64: imageBase64,
} }
}) })
@@ -675,13 +677,18 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
const roomId = this.roomId const roomId = this.roomId
const image = await this.CameraService.takePicture(); const file = await Camera.getPhoto({
await this.fileService.saveImage(image); quality: 90,
const lastphoto: any = await this.fileService.loadFiles(); // allowEditing: true,
const { capturedImage, capturedImageTitle } = await this.fileService.loadFileData(lastphoto); resultType: CameraResultType.Base64,
const base64 = await fetch(capturedImage); source: CameraSource.Camera
});
const imageBase64 = 'data:image/jpeg;base64,' + file.base64String
const blob = this.dataURItoBlob(imageBase64)
console.log(imageBase64)
const blob = await base64.blob();
const formData = new FormData(); const formData = new FormData();
formData.append("blobFile", blob); formData.append("blobFile", blob);
@@ -693,13 +700,12 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
}, },
temporaryData: formData, temporaryData: formData,
attachments: [{ attachments: [{
"title": capturedImageTitle, "title": "file.jpg",
"image_url": capturedImage,
"text": "description", "text": "description",
"title_link_download": false, "title_link_download": false,
}], }],
attachmentsModelData: { attachmentsModelData: {
fileBase64: await this.fileService.blobToBase64(blob), fileBase64: imageBase64,
} }
}) })
@@ -770,10 +776,12 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
//const imageData = await this.fileToBase64Service.convert(file) //const imageData = await this.fileToBase64Service.convert(file)
// //
const imageBase64 = 'data:image/jpeg;base64,' + file.base64String
const response = await fetch('data:image/jpeg;base64,' + file.base64String!); const response = await fetch(imageBase64);
const blob = await response.blob(); const blob = await response.blob();
console.log(imageBase64)
const formData = new FormData(); const formData = new FormData();
formData.append("blobFile", blob); formData.append("blobFile", blob);
@@ -790,7 +798,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
"title_link_download": false, "title_link_download": false,
}], }],
attachmentsModelData: { attachmentsModelData: {
fileBase64: await this.fileService.blobToBase64(blob), fileBase64: imageBase64,
} }
}) })
@@ -805,6 +813,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
const file: any = await this.fileService.getFileFromDevice(types); const file: any = await this.fileService.getFileFromDevice(types);
if (file.type != "application/img" && file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/gif") { if (file.type != "application/img" && file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/gif") {
const encodedData = btoa(JSON.stringify(await this.getBase64(file).catch ((error) => { const encodedData = btoa(JSON.stringify(await this.getBase64(file).catch ((error) => {
@@ -812,9 +821,11 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
}))); })));
const blob = this.fileService.base64toBlob(encodedData, file.type) const blob = this.fileService.base64toBlob(encodedData, file.type)
const fileBase64 = await this._getBase64(file)
const formData = new FormData(); const formData = new FormData();
formData.append("blobFile", blob); formData.append('blobFile', blob);
this.ChatSystemService.getDmRoom(roomId).send({ this.ChatSystemService.getDmRoom(roomId).send({
file: { file: {
@@ -830,7 +841,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
}], }],
temporaryData: formData, temporaryData: formData,
attachmentsModelData: { attachmentsModelData: {
fileBase64: await this.fileService.blobToBase64(blob), fileBase64: fileBase64,
} }
}) })
@@ -841,6 +852,18 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
} }
_getBase64(file) {
return new Promise((resolve, reject)=>{
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
resolve(reader.result)
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
})
}
getFileReader(): FileReader { getFileReader(): FileReader {
const fileReader = new FileReader(); const fileReader = new FileReader();
const zoneOriginalInstance = (fileReader as any)["__zone_symbol__originalInstance"]; const zoneOriginalInstance = (fileReader as any)["__zone_symbol__originalInstance"];
File diff suppressed because one or more lines are too long