public ChatHubPhoto AddChatHubPhoto(ChatHubPhoto ChatHubPhoto) { try { db.ChatHubMessage.Attach(ChatHubPhoto.Message); db.Entry(ChatHubPhoto.Message).State = EntityState.Modified; db.ChatHubPhoto.Add(ChatHubPhoto); db.SaveChanges(); return(ChatHubPhoto); } catch { throw; } }
public ChatHubPhoto CreateChatHubPhotoClientModel(ChatHubPhoto photo) { return(new ChatHubPhoto() { ChatHubMessageId = photo.ChatHubMessageId, Source = photo.Source, Thumb = photo.Thumb, Caption = photo.Caption, Size = photo.Size, Width = photo.Width, Height = photo.Height, CreatedOn = photo.CreatedOn, CreatedBy = photo.CreatedBy, ModifiedOn = photo.ModifiedOn, ModifiedBy = photo.ModifiedBy }); }
public async Task <IActionResult> PostImageUpload() { string connectionId = null; if (Request.Headers.ContainsKey("connectionId")) { connectionId = Request.Headers["connectionId"]; if (string.IsNullOrEmpty(connectionId)) { return(new BadRequestObjectResult(new { Message = "No connection id." })); } } ChatHubUser user = await this.chatHubService.IdentifyGuest(connectionId); if (user == null) { return(new BadRequestObjectResult(new { Message = "No user found." })); } string displayName = string.Empty; if (Request.Headers.ContainsKey("displayName")) { displayName = Request.Headers["displayName"]; if (string.IsNullOrEmpty(displayName)) { return(new BadRequestObjectResult(new { Message = "No display name." })); } } string roomId = string.Empty; if (Request.Headers.ContainsKey("roomId")) { roomId = Request.Headers["roomId"]; if (string.IsNullOrEmpty(roomId)) { return(new BadRequestObjectResult(new { Message = "No room id." })); } } string moduleId = string.Empty; if (Request.Headers.ContainsKey("moduleId")) { moduleId = Request.Headers["moduleId"]; if (string.IsNullOrEmpty(moduleId)) { return(new BadRequestObjectResult(new { Message = "No module id." })); } } IFormFileCollection files = Request.Form.Files; if (files == null || files.Count <= 0) { return(new BadRequestObjectResult(new { Message = "No files." })); } string content = string.Concat(files.Count, " ", "Photo(s)"); ChatHubRoom chatHubRoom = this.chatHubRepository.GetChatHubRoom(Int32.Parse(roomId)); if (chatHubRoom == null) { return(new BadRequestObjectResult(new { Message = "No room found." })); } ChatHubMessage chatHubMessage = new ChatHubMessage() { ChatHubRoomId = chatHubRoom.Id, ChatHubUserId = user.UserId, Type = Enum.GetName(typeof(ChatHubMessageType), ChatHubMessageType.Image), Content = content, User = user }; chatHubMessage = this.chatHubRepository.AddChatHubMessage(chatHubMessage); try { var maxFileSize = 10; var maxFileCount = 3; string folderName = "modules/oqtane.chathubs/images/selfies"; string webRootPath = string.Concat(this.webHostEnvironment.ContentRootPath, "\\wwwroot"); string newPath = Path.Combine(webRootPath, folderName); if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } if (files.Count > maxFileCount) { return(new BadRequestObjectResult(new { Message = "Maximum number of files exceeded." })); } foreach (IFormFile file in files) { if (file.Length > (maxFileSize * 1024 * 1024)) { return(new BadRequestObjectResult(new { Message = "File size Should Be UpTo " + maxFileSize + "MB" })); } var supportedFileExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif" }; string fileExtension = Path.GetExtension(file.FileName); if (!supportedFileExtensions.Contains(fileExtension)) { return(new BadRequestObjectResult(new { Message = "Unknown file type(s)." })); } /* * ObjectResult result = await this.PostAsync(file); * dynamic obj = result.Value; * string imageClassification = string.Empty; * if (result.StatusCode.Value == 200) * { * if (obj.predictedLabel == "dickpic") * { * var percent = string.Format("{0:P2}", Math.Round((float)obj.probability, 3)); * if ((float)obj.probability >= 0.99) * { * imageClassification = string.Concat(" | ", "(", "most likely identified as ", obj.predictedLabel, ": ", percent, ")"); * } * } * } */ int imageWidth, imageHeight; string fileName = string.Concat(Guid.NewGuid().ToString(), fileExtension); string fullPath = Path.Combine(newPath, fileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); FileInfo fileInfo = new FileInfo(file.FileName); var sizeInBytes = file.Length; Bitmap img = new Bitmap(stream); imageWidth = img.Width; imageHeight = img.Height; } ChatHubPhoto chatHubPhoto = new ChatHubPhoto() { ChatHubMessageId = chatHubMessage.Id, Source = fileName, Size = file.Length, Thumb = fileName, Caption = string.Concat(user.DisplayName, " | ", Math.Round(Convert.ToDecimal(file.Length / (1024.0m * 1024.0m)), 2), "MB" /*, imageClassification*/), Message = chatHubMessage, Width = imageWidth, Height = imageHeight }; chatHubPhoto = this.chatHubRepository.AddChatHubPhoto(chatHubPhoto); } chatHubMessage = this.chatHubService.CreateChatHubMessageClientModel(chatHubMessage); await this.chatHub.Clients.Group(chatHubMessage.ChatHubRoomId.ToString()).SendAsync("AddMessage", chatHubMessage); return(new OkObjectResult(new { Message = "Successfully Uploaded Files." })); } catch (Exception ex) { return(new BadRequestObjectResult(new { Message = "Error Uploading Files." })); } }