public async Task <TLMessage25> GetPhotoMessage(StorageFile file) { //var stopwatch = Stopwatch.StartNew(); var threadId = Thread.CurrentThread.ManagedThreadId; var volumeId = TLLong.Random(); var localId = TLInt.Random(); var secret = TLLong.Random(); var fileLocation = new TLFileLocation { VolumeId = volumeId, LocalId = localId, Secret = secret, DCId = new TLInt(0), //TODO: remove from here, replace with FileLocationUnavailable //Buffer = p.Bytes }; var fileName = String.Format("{0}_{1}_{2}.jpg", fileLocation.VolumeId, fileLocation.LocalId, fileLocation.Secret); var stream = await file.OpenReadAsync(); //System.Diagnostics.Debug.WriteLine(threadId + " " + "GetPhotoMessage OpenRead " + stopwatch.Elapsed); var resizedPhoto = await ResizeJpeg(stream, Constants.DefaultImageSize, file.DisplayName, fileName); //System.Diagnostics.Debug.WriteLine(threadId + " " + "GetPhotoMessage ResizeJpeg " + stopwatch.Elapsed); var photoSize = new TLPhotoSize { Type = TLString.Empty, W = new TLInt(resizedPhoto.Width), H = new TLInt(resizedPhoto.Height), Location = fileLocation, Size = new TLInt(resizedPhoto.Bytes.Length) }; volumeId = TLLong.Random(); localId = TLInt.Random(); secret = TLLong.Random(); var previewFileLocation = new TLFileLocation { VolumeId = volumeId, LocalId = localId, Secret = secret, DCId = new TLInt(0), //TODO: remove from here, replace with FileLocationUnavailable //Buffer = p.Bytes }; var previewFileName = String.Format("{0}_{1}_{2}.jpg", previewFileLocation.VolumeId, previewFileLocation.LocalId, previewFileLocation.Secret); var photo = new TLPhoto56 { Flags = new TLInt(0), Id = new TLLong(0), AccessHash = new TLLong(0), Date = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now), Sizes = new TLVector <TLPhotoSizeBase> { photoSize }, }; // to improve performance for bulk photo preview generation Execute.BeginOnThreadPool(async() => { stream = await file.OpenReadAsync(); var previewPhoto = await ResizeJpeg(stream, 90, file.DisplayName, previewFileName); var previewPhotoSize = new TLPhotoSize { Type = new TLString("s"), W = new TLInt(previewPhoto.Width), H = new TLInt(previewPhoto.Height), Location = previewFileLocation, Size = new TLInt(previewPhoto.Bytes.Length) }; Execute.BeginOnUIThread(() => { photo.Sizes.Add(previewPhotoSize); }); }); var media = new TLMessageMediaPhoto75 { Flags = new TLInt(0), Photo = photo, Caption = TLString.Empty, File = resizedPhoto.File }; return(GetMessage(TLString.Empty, media)); }
private void SendPhoto(Photo p) { var volumeId = TLLong.Random(); var localId = TLInt.Random(); var secret = TLLong.Random(); var fileLocation = new TLFileLocation { VolumeId = volumeId, LocalId = localId, Secret = secret, DCId = new TLInt(0), //TODO: remove from here, replace with FileLocationUnavailable //Buffer = p.Bytes }; var fileName = String.Format("{0}_{1}_{2}.jpg", fileLocation.VolumeId, fileLocation.LocalId, fileLocation.Secret); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (var fileStream = store.CreateFile(fileName)) { fileStream.Write(p.Bytes, 0, p.Bytes.Length); } } var photoSize = new TLPhotoSize { Type = TLString.Empty, W = new TLInt(p.Width), H = new TLInt(p.Height), Location = fileLocation, Size = new TLInt(p.Bytes.Length) }; var photo = new TLPhoto56 { Flags = new TLInt(0), Id = new TLLong(0), AccessHash = new TLLong(0), Date = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now), Sizes = new TLVector <TLPhotoSizeBase> { photoSize }, }; var media = new TLMessageMediaPhoto75 { Flags = new TLInt(0), Photo = photo, Caption = TLString.Empty }; var message = GetMessage(TLString.Empty, media); if (ImageEditor == null) { ImageEditor = new ImageEditorViewModel { CurrentItem = message, ContinueAction = ContinueSendPhoto }; NotifyOfPropertyChange(() => ImageEditor); } else { ImageEditor.CurrentItem = message; } BeginOnUIThread(() => ImageEditor.OpenEditor()); }
private void ProcessBotInlineResult(TLMessage45 message, TLBotInlineResultBase resultBase, TLInt botId) { message.InlineBotResultId = resultBase.Id; message.InlineBotResultQueryId = resultBase.QueryId; message.ViaBotId = botId; var botInlineMessageMediaVenue = resultBase.SendMessage as TLBotInlineMessageMediaVenue; var botInlineMessageMediaGeo = resultBase.SendMessage as TLBotInlineMessageMediaGeo; if (botInlineMessageMediaVenue != null) { message._media = new TLMessageMediaVenue72 { Title = botInlineMessageMediaVenue.Title, Address = botInlineMessageMediaVenue.Address, Provider = botInlineMessageMediaVenue.Provider, VenueId = botInlineMessageMediaVenue.VenueId, VenueType = TLString.Empty, Geo = botInlineMessageMediaVenue.Geo }; } else if (botInlineMessageMediaGeo != null) { message._media = new TLMessageMediaGeo { Geo = botInlineMessageMediaGeo.Geo }; } var botInlineMessageMediaContact = resultBase.SendMessage as TLBotInlineMessageMediaContact; if (botInlineMessageMediaContact != null) { message._media = new TLMessageMediaContact82 { PhoneNumber = botInlineMessageMediaContact.PhoneNumber, FirstName = botInlineMessageMediaContact.FirstName, LastName = botInlineMessageMediaContact.LastName, UserId = new TLInt(0), VCard = TLString.Empty }; } var mediaResult = resultBase as TLBotInlineMediaResult; if (mediaResult != null) { if (TLString.Equals(mediaResult.Type, new TLString("voice"), StringComparison.OrdinalIgnoreCase) && mediaResult.Document != null) { message._media = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty, NotListened = !(With is TLChannel) }; message.NotListened = !(With is TLChannel); } else if (TLString.Equals(mediaResult.Type, new TLString("audio"), StringComparison.OrdinalIgnoreCase) && mediaResult.Document != null) { message._media = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty }; } else if (TLString.Equals(mediaResult.Type, new TLString("sticker"), StringComparison.OrdinalIgnoreCase) && mediaResult.Document != null) { message._media = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty }; } else if (TLString.Equals(mediaResult.Type, new TLString("file"), StringComparison.OrdinalIgnoreCase) && mediaResult.Document != null) { message._media = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty }; } else if (TLString.Equals(mediaResult.Type, new TLString("gif"), StringComparison.OrdinalIgnoreCase) && mediaResult.Document != null) { message._media = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty }; } else if (TLString.Equals(mediaResult.Type, new TLString("photo"), StringComparison.OrdinalIgnoreCase) && mediaResult.Photo != null) { message._media = new TLMessageMediaPhoto75 { Flags = new TLInt(0), Photo = mediaResult.Photo, Caption = TLString.Empty }; } else if (TLString.Equals(mediaResult.Type, new TLString("game"), StringComparison.OrdinalIgnoreCase)) { var game = new TLGame { Flags = new TLInt(0), Id = new TLLong(0), AccessHash = new TLLong(0), ShortName = mediaResult.Id, Title = mediaResult.Title ?? TLString.Empty, Description = mediaResult.Description ?? TLString.Empty, Photo = mediaResult.Photo ?? new TLPhotoEmpty { Id = new TLLong(0) }, Document = mediaResult.Document }; message._media = new TLMessageMediaGame { Game = game, SourceMessage = message }; } } var result = resultBase as TLBotInlineResult; if (result != null) { var isVoice = TLString.Equals(result.Type, new TLString("voice"), StringComparison.OrdinalIgnoreCase); var isAudio = TLString.Equals(result.Type, new TLString("audio"), StringComparison.OrdinalIgnoreCase); var isFile = TLString.Equals(result.Type, new TLString("file"), StringComparison.OrdinalIgnoreCase); if (isFile || isAudio || isVoice) { var document = result.Document as TLDocument22; if (document == null) { string fileName = null; if (result.ContentUrl != null) { var fileUri = new Uri(result.ContentUrlString); try { fileName = Path.GetFileName(fileUri.LocalPath); } catch (Exception ex) { } if (fileName == null) { fileName = "file.ext"; } } document = new TLDocument54 { Id = new TLLong(0), AccessHash = new TLLong(0), Date = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now), FileName = new TLString(fileName), MimeType = result.ContentType ?? TLString.Empty, Size = new TLInt(0), Thumb = new TLPhotoSizeEmpty { Type = TLString.Empty }, DCId = new TLInt(0), Version = new TLInt(0) }; if (isVoice || isAudio) { var documentAttributeAudio = new TLDocumentAttributeAudio46 { Duration = result.Duration ?? new TLInt(0), Title = result.Title ?? TLString.Empty, Performer = null, Voice = isVoice }; document.Attributes.Add(documentAttributeAudio); } //message._status = MessageStatus.Failed; } var mediaDocument = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = document, Caption = TLString.Empty }; message._media = mediaDocument; mediaDocument.NotListened = isVoice && !(With is TLChannel); message.NotListened = isVoice && !(With is TLChannel); } else if (TLString.Equals(result.Type, new TLString("gif"), StringComparison.OrdinalIgnoreCase)) { var document = result.Document; if (document != null) { var mediaDocument = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = document, Caption = TLString.Empty }; message._media = mediaDocument; } } else if (TLString.Equals(result.Type, new TLString("photo"), StringComparison.OrdinalIgnoreCase)) { Telegram.Api.Helpers.Execute.ShowDebugMessage(string.Format("w={0} h={1}\nthumb_url={2}\ncontent_url={3}", result.W, result.H, result.ThumbUrl, result.ContentUrl)); var location = new TLFileLocation { DCId = new TLInt(1), VolumeId = TLLong.Random(), LocalId = TLInt.Random(), Secret = TLLong.Random() }; var cachedSize = new TLPhotoCachedSize { Type = new TLString("s"), W = result.W ?? new TLInt(90), H = result.H ?? new TLInt(90), Location = location, Bytes = TLString.Empty, TempUrl = result.ThumbUrlString ?? result.ContentUrlString //Size = new TLInt(0) }; var size = new TLPhotoSize { Type = new TLString("m"), W = result.W ?? new TLInt(90), H = result.H ?? new TLInt(90), Location = location, TempUrl = result.ContentUrlString, Size = new TLInt(0) }; if (!string.IsNullOrEmpty(result.ThumbUrlString)) { //ServicePointManager.ServerCertificateValidationCallback += new var webClient = new WebClient(); webClient.OpenReadAsync(new Uri(result.ThumbUrlString, UriKind.Absolute)); webClient.OpenReadCompleted += (sender, args) => { if (args.Cancelled) { return; } if (args.Error != null) { return; } var fileName = String.Format("{0}_{1}_{2}.jpg", location.VolumeId, location.LocalId, location.Secret); using (var stream = args.Result) { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { if (store.FileExists(fileName)) { return; } using (var file = store.OpenFile(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) { const int BUFFER_SIZE = 128 * 1024; var buf = new byte[BUFFER_SIZE]; var bytesread = 0; while ((bytesread = stream.Read(buf, 0, BUFFER_SIZE)) > 0) { var position = stream.Position; stream.Position = position - 10; var tempBuffer = new byte[10]; var resultOk = stream.Read(tempBuffer, 0, tempBuffer.Length); file.Write(buf, 0, bytesread); } } } } if (!string.IsNullOrEmpty(result.ContentUrlString)) { webClient.OpenReadAsync(new Uri(result.ContentUrlString, UriKind.Absolute)); webClient.OpenReadCompleted += (sender2, args2) => { if (args2.Cancelled) { return; } if (args2.Error != null) { return; } using (var stream = args2.Result) { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { if (store.FileExists(fileName)) { return; } using (var file = store.OpenFile(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) { const int BUFFER_SIZE = 128 * 1024; var buf = new byte[BUFFER_SIZE]; int bytesread = 0; while ((bytesread = stream.Read(buf, 0, BUFFER_SIZE)) > 0) { file.Write(buf, 0, bytesread); } } } } }; } }; } var photo = new TLPhoto56 { Flags = new TLInt(0), Id = TLLong.Random(), AccessHash = TLLong.Random(), Date = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now), Sizes = new TLVector <TLPhotoSizeBase> { cachedSize, size } }; var mediaPhoto = new TLMessageMediaPhoto75 { Flags = new TLInt(0), Photo = photo, Caption = TLString.Empty }; message._media = mediaPhoto; } } var messageText = resultBase.SendMessage as TLBotInlineMessageText; if (messageText != null) { message.Message = messageText.Message; message.Entities = messageText.Entities; if (messageText.NoWebpage) { } } var mediaAuto = resultBase.SendMessage as TLBotInlineMessageMediaAuto75; if (mediaAuto != null) { message.Message = mediaAuto.Caption; message.Entities = mediaAuto.Entities; } if (resultBase.SendMessage != null && resultBase.SendMessage.ReplyMarkup != null) { message.ReplyMarkup = resultBase.SendMessage.ReplyMarkup; } }