private async Task CreatePostAsync(Guid id, CreatePostRequest createPostRequest) { using (_logger.BeginScope($"Creating post for day {createPostRequest.DayNumber}")) { var sw = Stopwatch.StartNew(); var nextPostIdTask = _postsRepository.GetNextPostIdAsync(); var timeStamp = (int)(DateTime.UtcNow - DateTime.UnixEpoch).TotalSeconds; _logger.LogInformation($"timestamp for post = {timeStamp}"); var photoAttachments = new List <PhotoAttachment>(); var photoTasks = new List <Task>(); _logger.LogInformation($"# of photos to process = {createPostRequest.Photos.Count}"); for (var photoIndex = 0; photoIndex < createPostRequest.Photos.Count; photoIndex++) { var originalKey = $"posts/{timeStamp}/pa-original-{photoIndex}.jpg"; var mobileKey = $"posts/{timeStamp}/pa-mobile-{photoIndex}.jpg"; var previewKey = $"posts/{timeStamp}/pa-preview-{photoIndex}.jpg"; var photoAttachment = new PhotoAttachment(); var photoBytes = createPostRequest.Photos[photoIndex]; var originalResizeTask = ResizeAndUploadPhotoAsync(photoBytes, 2560, originalKey).ContinueWith(task => { photoAttachment.Width = task.Result.width; photoAttachment.Height = task.Result.height; photoAttachment.Src = task.Result.location; }); var mobileResizeTask = ResizeAndUploadPhotoAsync(photoBytes, 1280, mobileKey).ContinueWith(task => { photoAttachment.MobileSrc = task.Result.location; }); var previewResizeTask = ResizeAndUploadPhotoAsync(photoBytes, 640, previewKey).ContinueWith(task => { photoAttachment.PreviewSrc = task.Result.location; }); var index = photoIndex; var batchTask = Task.WhenAll(originalResizeTask, mobileResizeTask, previewResizeTask).ContinueWith(t => { _eventsMap[id].AddEvent($"Photo attachment #{index + 1} was processed."); }); photoTasks.Add(batchTask); photoAttachments.Add(photoAttachment); } await Task.WhenAll(photoTasks.Append(nextPostIdTask)); var post = new Post(nextPostIdTask.Result, createPostRequest.Text, createPostRequest.Hashtag, timeStamp, createPostRequest.DayNumber, createPostRequest.VideoUrl, photoAttachments); await _postsRepository.PutItemAsync(post); _logger.LogInformation($"post created within {(int) sw.ElapsedMilliseconds} ms"); _eventsMap[id].Complete(); } }
private PhotoAttachment ParsePhotoAttachment(JObject _jPhoto) { try { if (_jPhoto[PAttachmentsPhoto] is JObject photoJObj) { var photoAttachment = new PhotoAttachment(); photoAttachment.Id = photoJObj[PId].Value <int>(); photoAttachment.AlbumId = photoJObj[PPhotoAlbumId].Value <int>(); photoAttachment.OwnerId = photoJObj[PAttachmentOwnerId].Value <int>(); photoAttachment.UserId = photoJObj[PPhotoUserId]?.Value <int>(); photoAttachment.Text = photoJObj[PPhotoText]?.Value <string>(); photoAttachment.Date = EpochTimeConverter.ConvertToDateTime(photoJObj[PDate].Value <long>()); photoAttachment.AccessKey = photoJObj[PAttachmentAccessKey]?.Value <string>(); var sizes = new List <PhotoSizeInfo>(); if (photoJObj[PPhotoSizes] is JArray jSizes) { foreach (var jSize in jSizes) { var type = (PhotoSizeType)Enum.Parse(typeof(PhotoSizeType), jSize[PSizesType].Value <string>()); var url = jSize[PUrl].Value <string>(); var width = jSize[PSizesWidth].Value <int>(); var height = jSize[PSizesHeight].Value <int>(); var sizeInfo = new PhotoSizeInfo(type, url, width, height); sizes.Add(sizeInfo); } } photoAttachment.Sizes = sizes.ToArray(); return(photoAttachment); } } catch (Exception ex) { throw new InvalidOperationException($"Failed to parse photo attachment \n {_jPhoto.ToString()}", ex); } throw new ArgumentException($"Failed recognize jObject as photo attachment \n {_jPhoto?.ToString()}"); }
public void FromService(object obj) { CasheStatus = CasheStatus.Sync; Attachment attachment = (Attachment) obj; Type = attachment.Type; switch (AttachmentType) { case AttachmentType.Photo: if (Photo == null) Photo = new PhotoAttachment(); Photo.FromService(attachment.Photo); break; case AttachmentType.Doc: if (Document == null) Document = new DocumentAttachment(); Document.FromService(attachment.Document); break; case AttachmentType.Video: break; case AttachmentType.Audio: break; case AttachmentType.Map: break; default: throw new ArgumentOutOfRangeException(); } }
public void FromCashe(XElement element) { CasheStatus = CasheStatus.FromCashe; XAttribute attr; if ((attr = element.Attribute(TypePropertyName)) != null) Type = attr.Value; XElement attachmentElement; switch (AttachmentType) { case AttachmentType.Photo: if ((attachmentElement = element.Element(PhotoAttachment.ElementNameStatic)) != null) { if (Photo == null) Photo = new PhotoAttachment(); Photo.FromCashe(attachmentElement); } break; case AttachmentType.Doc: if ((attachmentElement = element.Element(DocumentAttachment.ElementNameStatic)) != null) { if (Document == null) Document = new DocumentAttachment(); Document.FromCashe(attachmentElement); } break; case AttachmentType.Video: break; case AttachmentType.Audio: break; case AttachmentType.Map: break; default: throw new ArgumentOutOfRangeException(); } }
internal static ChatMessage ParseSingleMessage(string chatMessageJson) { ChatMessage chatMessage = null; try { if (string.IsNullOrEmpty(chatMessageJson) && !Util.IsValidJsonObjectString(chatMessageJson)) { throw new Exception("Invalid JSON object string !"); } //If chat message is a proper json object instead of null, it will definitely have a message content which contain segment and Attachment. if (Util.IsJsonObject((JObject.Parse(chatMessageJson)).GetValue("message_content"))) { JObject jObjChatMessageContent = (JObject)((JObject.Parse(chatMessageJson)).GetValue("message_content")); JArray jArraySegment = null; if (Util.IsJsonArray(jObjChatMessageContent.GetValue("segment"))) { jArraySegment = (JArray)jObjChatMessageContent.GetValue("segment"); } JArray jArrayAttachment = null; if (Util.IsJsonArray(jObjChatMessageContent.GetValue("attachment"))) { jArrayAttachment = (JArray)jObjChatMessageContent.GetValue("attachment"); } string message = ""; if (jArraySegment != null) { foreach (JObject jObj in jArraySegment) { message += jObj.GetValue("text") + Environment.NewLine; } } //Risky part of parsing due to weird JSON structure, i'll just ignore data seemed to be useless. /* { '27639957': [ [ 'https://plus.google.com/photos/albums/p4jmatfi9qm621falthi1lkmblvjmjffi?pid=6218699856246635330&oid=107510317300187854172', , , 'https://lh3.googleusercontent.com/-HE_B_dR6JdY/Vk1Ckjhae0I/AAAAAAAAAAU/_m1Z9KuVZsA/s0/Desert.jpg', , , , , , 1024, 768 ], '107510317300187854172', '6218699858622478097', '6218699856246635330', , 'https://lh3.googleusercontent.com/-HE_B_dR6JdY/Vk1Ckjhae0I/AAAAAAAAAAU/_m1Z9KuVZsA/s0/Desert.jpg', , , , 'https://lh3.googleusercontent.com/Py6fFZ9K0F-Tit2CM2wv6JUfT1Ftqv_7a7vjhi5M_Q', , , 1, [ 'BABEL_STREAM_ID', 'BABEL_UNIQUE_ID_4232c25c-de3a-4750-9c72-f2b2609daec5', 'shared_group_6218699839102330066' ] ] } */ PhotoAttachment photoAttachment = null; if (jArrayAttachment != null) { foreach (JObject jObj in jArrayAttachment) { JArray jArrayData = (JArray)jObj.SelectToken("embed_item.data[\'" + CHAT_MESSAGE_ATTACHMENT_PHOTO + "\']"); photoAttachment = new PhotoAttachment( jArrayData.SelectToken("[9]").ToString(), jArrayData.SelectToken("[0][3]").ToString(), jArrayData.SelectToken("[0][0]").ToString(), int.Parse(jArrayData.SelectToken("[0][9]").ToString()), int.Parse(jArrayData.SelectToken("[0][10]").ToString()) ); } } chatMessage = new ChatMessage(message, photoAttachment); } return chatMessage; } catch (Exception ex) { throw new HangoutsException("ParseSingleMessage : " + ex.Message + Environment.NewLine + ex.StackTrace); } }
public ChatMessage(string message, PhotoAttachment photoAttachment) { this.Message = message; this.Attachment = photoAttachment; }