mirror of
				https://github.com/pierre42100/ComunicWeb
				synced 2025-11-04 12:14:12 +00:00 
			
		
		
		
	Conversation system send images through FormData
This commit is contained in:
		@@ -654,36 +654,13 @@ ComunicWeb.components.conversations.chatWindows = {
 | 
			
		||||
			ComunicWeb.components.conversations.chatWindows.resetCreateMessageForm(convInfos);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//Check if an image is included with the message or not
 | 
			
		||||
		if(form.inputImage.files[0]){
 | 
			
		||||
			//Include the image with the request (export the image as URL)
 | 
			
		||||
			var reader = new FileReader();
 | 
			
		||||
			reader.readAsDataURL(form.inputImage.files[0]);
 | 
			
		||||
			var sendImage = reader.result;
 | 
			
		||||
 | 
			
		||||
			//The function will resume once the image is fully converted
 | 
			
		||||
			reader.addEventListener("load", function() {
 | 
			
		||||
 | 
			
		||||
				//Send the message throught the interface
 | 
			
		||||
				ComunicWeb.components.conversations.interface.sendMessage({
 | 
			
		||||
					conversationID: convInfos.infos.ID,
 | 
			
		||||
					message: form.inputText.value,
 | 
			
		||||
					image: reader.result,
 | 
			
		||||
					callback: onceSent
 | 
			
		||||
				});
 | 
			
		||||
 | 
			
		||||
			}, false);
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			//Send the message throught the interface
 | 
			
		||||
			ComunicWeb.components.conversations.interface.sendMessage({
 | 
			
		||||
				conversationID: convInfos.infos.ID,
 | 
			
		||||
				message: form.inputText.value,
 | 
			
		||||
				callback: onceSent
 | 
			
		||||
			});
 | 
			
		||||
		}
 | 
			
		||||
			
 | 
			
		||||
		//Send the message throught the interface
 | 
			
		||||
		ComunicWeb.components.conversations.interface.sendMessage({
 | 
			
		||||
			conversationID: convInfos.infos.ID,
 | 
			
		||||
			message: form.inputText.value,
 | 
			
		||||
			image: form.inputImage,
 | 
			
		||||
			callback: onceSent
 | 
			
		||||
		});
 | 
			
		||||
		
 | 
			
		||||
		//Success
 | 
			
		||||
		return true;
 | 
			
		||||
 
 | 
			
		||||
@@ -242,7 +242,7 @@ ComunicWeb.components.conversations.interface = {
 | 
			
		||||
	 * @param {Object} infos Informations about the message to send
 | 
			
		||||
	 * @info {Integer} conversationID The ID of the conversation
 | 
			
		||||
	 * @info {String} message The message to send
 | 
			
		||||
	 * @info {String} image Optionnal, base64-encoded image
 | 
			
		||||
	 * @info {HTMLElement} image Optionnal, input field with an image
 | 
			
		||||
	 * @info {function} callback What to do once the image was successfully sent
 | 
			
		||||
	 * @return {Boolean} true for a success
 | 
			
		||||
	 */
 | 
			
		||||
@@ -250,18 +250,37 @@ ComunicWeb.components.conversations.interface = {
 | 
			
		||||
 | 
			
		||||
		//Perform an API request
 | 
			
		||||
		var apiURI = "conversations/sendMessage";
 | 
			
		||||
		var params = {
 | 
			
		||||
			message: infos.message,
 | 
			
		||||
			conversationID: infos.conversationID,
 | 
			
		||||
 | 
			
		||||
		var hasImage = false;
 | 
			
		||||
		if(infos.image){
 | 
			
		||||
			if(infos.image.files[0])
 | 
			
		||||
				hasImage = true;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//Add an image (if any specified)
 | 
			
		||||
		if(infos.image)
 | 
			
		||||
			params.image = infos.image;
 | 
			
		||||
		
 | 
			
		||||
		//Perform an API request
 | 
			
		||||
		ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, infos.callback);
 | 
			
		||||
		//Check wether an image has to be included or not
 | 
			
		||||
		if(!hasImage){
 | 
			
		||||
 | 
			
		||||
			//Prepare request
 | 
			
		||||
			var params = {
 | 
			
		||||
				message: infos.message,
 | 
			
		||||
				conversationID: infos.conversationID,
 | 
			
		||||
			};
 | 
			
		||||
 | 
			
		||||
			//Perform an API request
 | 
			
		||||
			ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, infos.callback);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//If we have an image, we must do a formdata request
 | 
			
		||||
		else {
 | 
			
		||||
 | 
			
		||||
			var fd = new FormData();
 | 
			
		||||
			fd.append("message", infos.message);
 | 
			
		||||
			fd.append("conversationID", infos.conversationID);
 | 
			
		||||
			fd.append("image", infos.image.files[0], infos.image.files[0].name);
 | 
			
		||||
 | 
			
		||||
			//Perform an API request
 | 
			
		||||
			ComunicWeb.common.api.makeFormDatarequest(apiURI, fd, true, infos.callback);
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user