private void SendDocument(TLDocument document, string caption) { var media = new TLMessageMediaDocument { Document = document, Caption = caption }; var date = TLUtils.DateToUniversalTimeTLInt(ProtoService.ClientTicksDelta, DateTime.Now); var message = TLUtils.GetMessage(SettingsHelper.UserId, Peer.ToPeer(), TLMessageState.Sending, true, true, date, string.Empty, media, TLLong.Random(), null); if (Reply != null) { message.HasReplyToMsgId = true; message.ReplyToMsgId = Reply.Id; message.Reply = Reply; Reply = null; } var previousMessage = InsertSendingMessage(message, false); CacheService.SyncSendingMessage(message, previousMessage, async (m) => { var input = new TLInputMediaDocument { Caption = caption, Id = new TLInputDocument { Id = document.Id, AccessHash = document.AccessHash, } }; await ProtoService.SendMediaAsync(Peer, input, message); }); }
public TLDocument GetFileData(TLMessagesSlice messages) { TLMessage message = (TLMessage)messages.Messages.AsEnumerable().ElementAt(0); TLMessageMediaDocument mediadata = (TLMessageMediaDocument)message.Media; TLDocument filedata = (TLDocument)mediadata.Document; return(filedata); }
private void SendDocument(string fileName) { //create thumb byte[] bytes; using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (var fileStream = store.OpenFile(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, bytes.Length); } } if (!CheckDocumentSize((ulong)bytes.Length)) { MessageBox.Show(string.Format(AppResources.MaximumFileSizeExceeded, MediaSizeConverter.Convert((int)Telegram.Api.Constants.MaximumUploadedFileSize)), AppResources.Error, MessageBoxButton.OK); return; } //create document var document = new TLDocument22 { Buffer = bytes, Id = new TLLong(0), AccessHash = new TLLong(0), Date = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now), FileName = new TLString(Path.GetFileName(fileName)), MimeType = new TLString("text/plain"), Size = new TLInt(bytes.Length), Thumb = new TLPhotoSizeEmpty { Type = TLString.Empty }, DCId = new TLInt(0) }; var media = new TLMessageMediaDocument { FileId = TLLong.Random(), Document = document }; var message = GetMessage(TLString.Empty, media); BeginOnUIThread(() => { var previousMessage = InsertSendingMessage(message); IsEmptyDialog = Items.Count == 0 && LazyItems.Count == 0; BeginOnThreadPool(() => CacheService.SyncSendingMessage( message, previousMessage, TLUtils.InputPeerToPeer(Peer, StateService.CurrentUserId), m => SendDocumentInternal(message, null))); }); }
public bool IsMedia(TLMessagesSlice messages) { TLMessage message = (TLMessage)messages.Messages.AsEnumerable().ElementAt(0); TLMessageMediaDocument mediadata = (TLMessageMediaDocument)message.Media; if (mediadata == null) { return(false); } else { return(true); } }
public void CancelDocumentDownloading(TLMessageMediaDocument mediaDocument) { BeginOnThreadPool(() => { BeginOnUIThread(() => { var message = Items.FirstOrDefault(x => x.Media == mediaDocument); DownloadDocumentFileManager.CancelDownloadFileAsync(message); mediaDocument.IsCanceled = true; mediaDocument.LastProgress = mediaDocument.DownloadingProgress; mediaDocument.DownloadingProgress = 0.0; }); }); }
public void SendSticker(TLDocument22 document) { if (document == null) { return; } var media = new TLMessageMediaDocument { FileId = TLLong.Random(), Document = document }; var message = GetMessage(TLString.Empty, media); var inputMedia = new TLInputMediaDocument { MD5Hash = new byte[] { }, Id = new TLInputDocument { Id = document.Id, AccessHash = document.AccessHash } }; message.InputMedia = inputMedia; BeginOnUIThread(() => { var previousMessage = InsertSendingMessage(message); IsEmptyDialog = Items.Count == 0 && LazyItems.Count == 0; Text = string.Empty; BeginOnThreadPool(() => CacheService.SyncSendingMessage( message, previousMessage, TLUtils.InputPeerToPeer(Peer, StateService.CurrentUserId), m => ShellViewModel.SendMediaInternal(message, MTProtoService, StateService, CacheService))); }); }
private static async void OnMyTimedEvent(object source, ElapsedEventArgs e) { try { Console.WriteLine("On timer event"); DateTime nowDateTime = DateTime.Now.ToLocalTime(); // Check that we are well connected if (Client != null && Client.IsConnected && UserId != 0) { if (ChannelId != null && ChannelId.Count > 0) { TLAbsDialogs = await Client.GetUserDialogsAsync(); foreach (TLAbsMessage tLAbsMessage in ((TLDialogs)TLAbsDialogs).Messages.Where(x => x is TLMessage message && TimeUnixTOWindows(message.Date, true) >= nowDateTime.AddMilliseconds(-(TimerIntervalInMs - 1)))) { ((TLMessage)tLAbsMessage).Message = CalculOffset(((TLMessage)tLAbsMessage).Message); if (((TLMessage)tLAbsMessage).ToId is TLPeerUser tLPeerUser) { // Personal Chat Do Not Forward! } else if (((TLMessage)tLAbsMessage).ToId is TLPeerChannel channel0 && ((TLMessage)tLAbsMessage).ReplyToMsgId != null) { int crtChannelId = channel0.ChannelId; if (crtChannelId != MyChanId && ChannelId.ContainsKey(crtChannelId)) { Console.WriteLine("ReplyChannelId " + ((TLMessage)tLAbsMessage).ReplyToMsgId); await ReplyMessage((TLMessage)tLAbsMessage); } } else if (((TLMessage)tLAbsMessage).ToId is TLPeerChat chat && ((TLMessage)tLAbsMessage).ReplyToMsgId != null) { Console.WriteLine("ReplyChatId " + ((TLMessage)tLAbsMessage).ReplyToMsgId); await ReplyMessage((TLMessage)tLAbsMessage); } else if (((TLMessage)tLAbsMessage).ToId is TLPeerChannel channel && ((TLMessage)tLAbsMessage).ReplyToMsgId == null) { int crtChannelId = channel.ChannelId; if (crtChannelId != MyChanId && ChannelId.ContainsKey(crtChannelId)) { Console.WriteLine("New Message Channel " + ChannelId[crtChannelId][0] + " \n" + ((TLMessage)tLAbsMessage).Message); if (ChannelId.ContainsKey(crtChannelId)) { if (((TLMessage)tLAbsMessage).Message != "") { if (((TLMessage)tLAbsMessage).Message.ToLower().StartsWith("tp") || ((TLMessage)tLAbsMessage).Message.ToLower().StartsWith("sl")) { TLChannelMessages historyFromSourceCanal = (TLChannelMessages)await Client.GetHistoryAsync(new TLInputPeerChannel() { ChannelId = channel.ChannelId, AccessHash = (long)ChannelId[channel.ChannelId][1] }); List <TLAbsMessage> tLMessageList = historyFromSourceCanal.Messages.ToList().Where(x => x is TLMessage tL).ToList(); List <TLMessage> orderedtLMessageList = tLMessageList.Cast <TLMessage>().OrderByDescending(x => x.Id).ToList(); string newMessage = CalculOffset(orderedtLMessageList[1].Message + "\n" + ((TLMessage)tLAbsMessage).Message); if (orderedtLMessageList[1].Message.ToLower().Contains("sell") && !orderedtLMessageList[1].Message.ToLower().Contains("sl")) { await Client.SendMessageAsync(new TLInputPeerChannel() { ChannelId = MyChanId, AccessHash = AccessHash }, newMessage); } else if (orderedtLMessageList[1].Message.ToLower().Contains("vente") && !orderedtLMessageList[1].Message.ToLower().Contains("sl")) { await Client.SendMessageAsync(new TLInputPeerChannel() { ChannelId = MyChanId, AccessHash = AccessHash }, newMessage); } else if (orderedtLMessageList[1].Message.ToLower().Contains("buy") && !orderedtLMessageList[1].Message.ToLower().Contains("sl")) { await Client.SendMessageAsync(new TLInputPeerChannel() { ChannelId = MyChanId, AccessHash = AccessHash }, newMessage); } else if (orderedtLMessageList[1].Message.ToLower().Contains("achat") && !orderedtLMessageList[1].Message.ToLower().Contains("sl")) { await Client.SendMessageAsync(new TLInputPeerChannel() { ChannelId = MyChanId, AccessHash = AccessHash }, newMessage); } } else { await Client.SendMessageAsync(new TLInputPeerChannel() { ChannelId = MyChanId, AccessHash = AccessHash }, ((TLMessage)tLAbsMessage).Message); } } else if (((TLMessage)tLAbsMessage).Media != null) { if (((TLMessage)tLAbsMessage).Media.GetType().ToString() == "TeleSharp.TL.TLMessageMediaPhoto") { TLMessageMediaPhoto tLMessageMediaPhoto = (TLMessageMediaPhoto)((TLMessage)tLAbsMessage).Media; TLPhoto tLPhoto = (TLPhoto)tLMessageMediaPhoto.Photo; TLPhotoSize tLPhotoSize = tLPhoto.Sizes.ToList().OfType <TLPhotoSize>().Last(); TLFileLocation tLFileLocation = (TLFileLocation)tLPhotoSize.Location; TLAbsInputFileLocation tLAbsInputFileLocation = new TLInputFileLocation() { LocalId = tLFileLocation.LocalId, Secret = tLFileLocation.Secret, VolumeId = tLFileLocation.VolumeId }; TLInputFileLocation TLInputFileLocation = tLAbsInputFileLocation as TLInputFileLocation; TLFile buffer = await Client.GetFile(TLInputFileLocation, 1024 * 512); TLInputFile fileResult = (TLInputFile)await UploadHelper.UploadFile(Client, "", new StreamReader(new MemoryStream(buffer.Bytes))); await Client.SendUploadedPhoto(new TLInputPeerChannel() { ChannelId = MyChanId, AccessHash = AccessHash }, fileResult, tLMessageMediaPhoto.Caption); } else if (((TLMessage)tLAbsMessage).Media.GetType().ToString() == "TeleSharp.TL.TLMessageMediaDocument") { TLMessageMediaDocument tLMessageMediaDocument = (TLMessageMediaDocument)((TLMessage)tLAbsMessage).Media; TLDocument tLDocument = (TLDocument)tLMessageMediaDocument.Document; TLVector <TLAbsDocumentAttribute> tLAbsDocumentAttributes = tLDocument.Attributes; TLInputDocumentFileLocation tLInputDocumentFileLocation = new TLInputDocumentFileLocation() { AccessHash = tLDocument.AccessHash, Id = tLDocument.Id, Version = tLDocument.Version, }; TLFile buffer = await Client.GetFile(tLInputDocumentFileLocation, 1024 * 512); TLInputFile fileResult = (TLInputFile)await UploadHelper.UploadFile(Client, ((TLDocumentAttributeFilename)tLAbsDocumentAttributes[0]).FileName, new StreamReader(new MemoryStream(buffer.Bytes))); await Client.SendUploadedDocument(new TLInputPeerChannel() { ChannelId = MyChanId, AccessHash = AccessHash }, fileResult, tLMessageMediaDocument.Caption, tLDocument.MimeType, tLAbsDocumentAttributes); } } } } } } }
protected string ExportAudio(TLMessageMediaDocument media, TLDocument document) { // Find TLDocumentAttributeFilename from list of attributes TLDocumentAttributeAudio attrAudio = null; TLDocumentAttributeFilename attrFile = null; foreach (TLAbsDocumentAttribute absAttr in document.attributes.lists) { if (absAttr is TLDocumentAttributeAudio) { attrAudio = (TLDocumentAttributeAudio)absAttr; } else if (absAttr is TLDocumentAttributeFilename) { attrFile = (TLDocumentAttributeFilename)absAttr; } } if (attrAudio == null && attrFile == null) { throw new TLCoreException("The TLDocumentAttributeAudio and TLDocumentAttributeFilename have not been found"); } string key; if (attrFile != null) { // Key: YYYY-MM-DD-Caption{0:00}.EXT key = String.Format("{0}-{1}{2}", m_sPrefix, "{0:00}", attrFile.file_name); } else { string sCaption = attrAudio.voice ? c_sVoice : c_sAudio; string ext = document.mime_type.Substring(document.mime_type.Length - 3).Trim(); // Key: YYYY-MM-DD-Caption{0:00}.EXT key = String.Format("{0}-{1}{2}.{3}", m_sPrefix, sCaption, "{0:00}", ext); } string sFileName = GetUniqueFileName(key); // YYYY-MM-DD-AudioXX.EXT if (m_config.ExportVoiceMessages) { // Export the document to a file string sFullFileName = Path.Combine(m_sDialogDirectory, sFileName); using (FileStream f = new FileStream(sFullFileName, FileMode.Create, FileAccess.Write)) foreach (TLFile file in m_archiver.GetDocument(document)) { if (file.type.GetType() == typeof(TLFileUnknown)) { throw new TLCoreException("File unknown: " + sFileName); } f.Write(file.bytes, 0, file.bytes.Length); } } return(sFileName); // YYYY-MM-DD-AudioXX.EXT }
protected string ExportDocument(TLMessageMediaDocument media) { // TLPhoto contains a collection of TLPhotoSize TLDocument document = media.document as TLDocument; if (document == null) { throw new TLCoreException("The document is not an instance of TLDocument"); } if (document.attributes.lists.Count <= 0) { throw new TLCoreException("TLDocument does not have any attributes"); } if (document.mime_type.StartsWith("audio")) { return(ExportAudio(media, document)); } // Find TLDocumentAttributeFilename from list of attributes TLDocumentAttributeFilename attr = null; foreach (TLAbsDocumentAttribute absAttr in document.attributes.lists) { attr = absAttr as TLDocumentAttributeFilename; if (attr != null) { break; } } if (attr == null) { throw new TLCoreException("The TLDocumentAttributeFilename has not been found"); } if (String.IsNullOrEmpty(attr.file_name)) { throw new TLCoreException("The file_name of the document is empty"); } // Key: YYYY-MM-DD-Caption{0:00}.EXT string key = String.Format("{0}-{1}{2}", m_sPrefix, "{0:00}", attr.file_name); string sFileName = GetUniqueFileName(key); // YYYY-MM-DD-CaptionXX.EXT if (m_config.ExportFiles) { // Export the document to a file string sFullFileName = Path.Combine(m_sDialogDirectory, sFileName); using (FileStream f = new FileStream(sFullFileName, FileMode.Create, FileAccess.Write)) foreach (TLFile file in m_archiver.GetDocument(document)) { if (file.type.GetType() == typeof(TLFileUnknown)) { throw new TLCoreException("File unknown: " + sFileName); } f.Write(file.bytes, 0, file.bytes.Length); } } return(sFileName); // YYYY-MM-DD-CaptionXX.EXT }
private void SendDocument(Photo d) { //create thumb var bytes = d.PreviewBytes; if (!CheckDocumentSize((ulong)d.Bytes.Length)) { MessageBox.Show(string.Format(AppResources.MaximumFileSizeExceeded, MediaSizeConverter.Convert((int)Telegram.Api.Constants.MaximumUploadedFileSize)), AppResources.Error, MessageBoxButton.OK); return; } var volumeId = TLLong.Random(); var localId = TLInt.Random(); var secret = TLLong.Random(); var thumbLocation = new TLFileLocation //TODO: replace with TLFileLocationUnavailable { DCId = new TLInt(0), VolumeId = volumeId, LocalId = localId, Secret = secret, //Buffer = bytes }; var fileName = String.Format("{0}_{1}_{2}.jpg", thumbLocation.VolumeId, thumbLocation.LocalId, thumbLocation.Secret); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (var fileStream = store.CreateFile(fileName)) { fileStream.Write(bytes, 0, bytes.Length); } } var thumbSize = new TLPhotoSize { W = new TLInt(d.Width), H = new TLInt(d.Height), Size = new TLInt(bytes.Length), Type = new TLString(""), Location = thumbLocation, }; //create document var document = new TLDocument22 { Buffer = d.Bytes, Id = new TLLong(0), AccessHash = new TLLong(0), Date = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now), FileName = new TLString(Path.GetFileName(d.FileName)), MimeType = new TLString("image/jpeg"), Size = new TLInt(d.Bytes.Length), Thumb = thumbSize, DCId = new TLInt(0) }; var media = new TLMessageMediaDocument { FileId = TLLong.Random(), Document = document }; var message = GetMessage(TLString.Empty, media); BeginOnUIThread(() => { var previousMessage = InsertSendingMessage(message); IsEmptyDialog = Items.Count == 0 && LazyItems.Count == 0; BeginOnThreadPool(() => CacheService.SyncSendingMessage( message, previousMessage, TLUtils.InputPeerToPeer(Peer, StateService.CurrentUserId), m => SendDocumentInternal(message, null))); }); }
public async void SendDocument(StorageFile file) { if (file == null) { return; } var properties = await file.GetBasicPropertiesAsync(); var size = properties.Size; //file.Properties.GetImagePropertiesAsync() if (!CheckDocumentSize(size)) { MessageBox.Show(string.Format(AppResources.MaximumFileSizeExceeded, MediaSizeConverter.Convert((int)Telegram.Api.Constants.MaximumUploadedFileSize)), AppResources.Error, MessageBoxButton.OK); return; } // to get access to the file with StorageFile.GetFileFromPathAsync in future AddFileToFutureAccessList(file); var thumb = await GetFileThumbAsync(file); //create document var document = new TLDocument22 { Id = new TLLong(0), AccessHash = new TLLong(0), Date = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now), FileName = new TLString(Path.GetFileName(file.Name)), MimeType = new TLString(file.ContentType), Size = new TLInt((int)size), Thumb = thumb ?? new TLPhotoSizeEmpty { Type = TLString.Empty }, DCId = new TLInt(0) }; if (string.Equals(document.FileExt, "webp", StringComparison.OrdinalIgnoreCase) && document.DocumentSize < Telegram.Api.Constants.StickerMaxSize) { document.MimeType = new TLString("image/webp"); document.Attributes.Add(new TLDocumentAttributeSticker()); var fileName = document.GetFileName(); await file.CopyAsync(ApplicationData.Current.LocalFolder, fileName, NameCollisionOption.ReplaceExisting); } var media = new TLMessageMediaDocument { FileId = TLLong.Random(), Document = document, IsoFileName = file.Path, File = file }; var message = GetMessage(TLString.Empty, media); BeginOnUIThread(() => { var previousMessage = InsertSendingMessage(message); IsEmptyDialog = Items.Count == 0 && LazyItems.Count == 0; BeginOnThreadPool(() => CacheService.SyncSendingMessage( message, previousMessage, TLUtils.InputPeerToPeer(Peer, StateService.CurrentUserId), m => SendDocumentInternal(message, file))); }); }
public async Task SendAudioAsync(StorageFile file, int duration, bool voice, string title, string performer, string caption) { var fileLocation = new TLFileLocation { VolumeId = TLLong.Random(), LocalId = TLInt.Random(), Secret = TLLong.Random(), DCId = 0 }; var fileName = string.Format("{0}_{1}_{2}.ogg", fileLocation.VolumeId, fileLocation.LocalId, fileLocation.Secret); var fileCache = await FileUtils.CreateTempFileAsync(fileName); await file.CopyAndReplaceAsync(fileCache); var basicProps = await fileCache.GetBasicPropertiesAsync(); var imageProps = await fileCache.Properties.GetImagePropertiesAsync(); var date = TLUtils.DateToUniversalTimeTLInt(ProtoService.ClientTicksDelta, DateTime.Now); var media = new TLMessageMediaDocument { Caption = caption, Document = new TLDocument { Id = TLLong.Random(), AccessHash = TLLong.Random(), Date = date, MimeType = "audio/ogg", Size = (int)basicProps.Size, Thumb = new TLPhotoSizeEmpty { Type = string.Empty }, Version = 0, DCId = 0, Attributes = new TLVector<TLDocumentAttributeBase> { new TLDocumentAttributeAudio { IsVoice = voice, Duration = duration, Title = title, Performer = performer } } } }; var message = TLUtils.GetMessage(SettingsHelper.UserId, Peer.ToPeer(), TLMessageState.Sending, true, true, date, string.Empty, media, TLLong.Random(), null); if (Reply != null) { message.HasReplyToMsgId = true; message.ReplyToMsgId = Reply.Id; message.Reply = Reply; Reply = null; } var previousMessage = InsertSendingMessage(message); CacheService.SyncSendingMessage(message, previousMessage, async (m) => { var fileId = TLLong.Random(); var upload = await _uploadAudioManager.UploadFileAsync(fileId, fileName, false).AsTask(Upload(media.Document as TLDocument, progress => new TLSendMessageUploadAudioAction { Progress = progress })); if (upload != null) { var inputMedia = new TLInputMediaUploadedDocument { File = upload.ToInputFile(), MimeType = "audio/ogg", Caption = media.Caption, Attributes = new TLVector<TLDocumentAttributeBase> { new TLDocumentAttributeAudio { IsVoice = voice, Duration = duration, Title = title, Performer = performer } } }; var result = await ProtoService.SendMediaAsync(Peer, inputMedia, message); } }); }
private async Task SendGifAsync(StorageFile file, string caption) { var fileLocation = new TLFileLocation { VolumeId = TLLong.Random(), LocalId = TLInt.Random(), Secret = TLLong.Random(), DCId = 0 }; var fileName = string.Format("{0}_{1}_{2}.gif", fileLocation.VolumeId, fileLocation.LocalId, fileLocation.Secret); var fileCache = await FileUtils.CreateTempFileAsync(fileName); await file.CopyAndReplaceAsync(fileCache); var basicProps = await fileCache.GetBasicPropertiesAsync(); var imageProps = await fileCache.Properties.GetImagePropertiesAsync(); var thumbnailBase = await FileUtils.GetFileThumbnailAsync(file); var thumbnail = thumbnailBase as TLPhotoSize; var desiredName = string.Format("{0}_{1}_{2}.jpg", thumbnail.Location.VolumeId, thumbnail.Location.LocalId, thumbnail.Location.Secret); var date = TLUtils.DateToUniversalTimeTLInt(ProtoService.ClientTicksDelta, DateTime.Now); var document = new TLDocument { Id = 0, AccessHash = 0, Date = date, Size = (int)basicProps.Size, MimeType = fileCache.ContentType, Thumb = thumbnail, Attributes = new TLVector<TLDocumentAttributeBase> { new TLDocumentAttributeAnimated(), new TLDocumentAttributeFilename { FileName = file.Name }, new TLDocumentAttributeImageSize { W = (int)imageProps.Width, H = (int)imageProps.Height }, new TLDocumentAttributeVideo { W = (int)imageProps.Width, H = (int)imageProps.Height, } } }; var media = new TLMessageMediaDocument { Caption = caption, Document = document }; var message = TLUtils.GetMessage(SettingsHelper.UserId, Peer.ToPeer(), TLMessageState.Sending, true, true, date, string.Empty, media, TLLong.Random(), null); if (Reply != null) { message.HasReplyToMsgId = true; message.ReplyToMsgId = Reply.Id; message.Reply = Reply; Reply = null; } var previousMessage = InsertSendingMessage(message); CacheService.SyncSendingMessage(message, previousMessage, async (m) => { var fileId = TLLong.Random(); var upload = await _uploadDocumentManager.UploadFileAsync(fileId, fileName, false).AsTask(media.Document.Upload()); if (upload != null) { var thumbFileId = TLLong.Random(); var thumbUpload = await _uploadDocumentManager.UploadFileAsync(thumbFileId, desiredName); if (thumbUpload != null) { var inputMedia = new TLInputMediaUploadedThumbDocument { File = upload.ToInputFile(), Thumb = thumbUpload.ToInputFile(), MimeType = document.MimeType, Caption = media.Caption, Attributes = document.Attributes }; var result = await ProtoService.SendMediaAsync(Peer, inputMedia, message); } } }); }
public async Task SendVideoAsync(StorageFile file, string caption, bool round, VideoTransformEffectDefinition transform = null, MediaEncodingProfile profile = null) { if (_peer == null) { return; } var fileLocation = new TLFileLocation { VolumeId = TLLong.Random(), LocalId = TLInt.Random(), Secret = TLLong.Random(), DCId = 0 }; var fileName = string.Format("{0}_{1}_{2}.mp4", fileLocation.VolumeId, fileLocation.LocalId, fileLocation.Secret); var fileCache = await FileUtils.CreateTempFileAsync(fileName); await file.CopyAndReplaceAsync(fileCache); var basicProps = await fileCache.GetBasicPropertiesAsync(); var videoProps = await fileCache.Properties.GetVideoPropertiesAsync(); var thumbnailBase = await FileUtils.GetFileThumbnailAsync(file); var thumbnail = thumbnailBase as TLPhotoSize; if (thumbnail == null) { return; } var desiredName = string.Format("{0}_{1}_{2}.jpg", thumbnail.Location.VolumeId, thumbnail.Location.LocalId, thumbnail.Location.Secret); var date = TLUtils.DateToUniversalTimeTLInt(ProtoService.ClientTicksDelta, DateTime.Now); var videoWidth = (int)videoProps.Width; var videoHeight = (int)videoProps.Height; if (profile != null) { videoWidth = (int)profile.Video.Width; videoHeight = (int)profile.Video.Height; } var document = new TLDocument { Id = 0, AccessHash = 0, Date = date, Size = (int)basicProps.Size, MimeType = fileCache.ContentType, Thumb = thumbnail, Attributes = new TLVector<TLDocumentAttributeBase> { new TLDocumentAttributeFilename { FileName = file.Name }, new TLDocumentAttributeVideo { Duration = (int)videoProps.Duration.TotalSeconds, W = videoWidth, H = videoHeight, IsRoundMessage = round } } }; var media = new TLMessageMediaDocument { Document = document, Caption = caption }; var message = TLUtils.GetMessage(SettingsHelper.UserId, _peer.ToPeer(), TLMessageState.Sending, true, true, date, string.Empty, media, TLLong.Random(), null); if (Reply != null) { message.HasReplyToMsgId = true; message.ReplyToMsgId = Reply.Id; message.Reply = Reply; Reply = null; } var previousMessage = InsertSendingMessage(message); CacheService.SyncSendingMessage(message, previousMessage, async (m) => { if (transform != null && profile != null) { await fileCache.RenameAsync(fileName + ".temp.mp4"); var fileResult = await FileUtils.CreateTempFileAsync(fileName); var transcoder = new MediaTranscoder(); transcoder.AddVideoEffect(transform.ActivatableClassId, true, transform.Properties); var prepare = await transcoder.PrepareFileTranscodeAsync(fileCache, fileResult, profile); await prepare.TranscodeAsync().AsTask(Upload(media.Document as TLDocument, progress => new TLSendMessageUploadDocumentAction { Progress = progress }, 0, 200.0)); //await fileCache.DeleteAsync(); fileCache = fileResult; thumbnailBase = await FileUtils.GetFileThumbnailAsync(fileCache); thumbnail = thumbnailBase as TLPhotoSize; desiredName = string.Format("{0}_{1}_{2}.jpg", thumbnail.Location.VolumeId, thumbnail.Location.LocalId, thumbnail.Location.Secret); document.Thumb = thumbnail; } var fileId = TLLong.Random(); var upload = await _uploadVideoManager.UploadFileAsync(fileId, fileCache.Name, false).AsTask(Upload(media.Document as TLDocument, progress => new TLSendMessageUploadDocumentAction { Progress = progress }, 0.5, 2.0)); if (upload != null) { var thumbFileId = TLLong.Random(); var thumbUpload = await _uploadDocumentManager.UploadFileAsync(thumbFileId, desiredName); if (thumbUpload != null) { var inputMedia = new TLInputMediaUploadedThumbDocument { File = upload.ToInputFile(), Thumb = thumbUpload.ToInputFile(), MimeType = document.MimeType, Caption = media.Caption, Attributes = document.Attributes }; var result = await ProtoService.SendMediaAsync(_peer, inputMedia, message); } //if (result.IsSucceeded) //{ // var update = result.Result as TLUpdates; // if (update != null) // { // var newMessage = update.Updates.OfType<TLUpdateNewMessage>().FirstOrDefault(); // if (newMessage != null) // { // var newM = newMessage.Message as TLMessage; // if (newM != null) // { // message.Media = newM.Media; // message.RaisePropertyChanged(() => message.Media); // } // } // } //} } }); }
private async Task SendThumbnailFileAsync(StorageFile file, TLFileLocation fileLocation, string fileName, BasicProperties basicProps, TLPhotoSize thumbnail, StorageFile fileCache, string caption) { if (_peer == null) { return; } var desiredName = string.Format("{0}_{1}_{2}.jpg", thumbnail.Location.VolumeId, thumbnail.Location.LocalId, thumbnail.Location.Secret); var date = TLUtils.DateToUniversalTimeTLInt(ProtoService.ClientTicksDelta, DateTime.Now); var document = new TLDocument { Id = 0, AccessHash = 0, Date = date, Size = (int)basicProps.Size, MimeType = fileCache.ContentType, Thumb = thumbnail, Attributes = new TLVector<TLDocumentAttributeBase> { new TLDocumentAttributeFilename { FileName = file.Name } } }; var media = new TLMessageMediaDocument { Document = document, Caption = caption }; var message = TLUtils.GetMessage(SettingsHelper.UserId, _peer.ToPeer(), TLMessageState.Sending, true, true, date, string.Empty, media, TLLong.Random(), null); if (Reply != null) { message.HasReplyToMsgId = true; message.ReplyToMsgId = Reply.Id; message.Reply = Reply; Reply = null; } var previousMessage = InsertSendingMessage(message); CacheService.SyncSendingMessage(message, previousMessage, async (m) => { var fileId = TLLong.Random(); var upload = await _uploadDocumentManager.UploadFileAsync(fileId, fileCache.Name, false).AsTask(Upload(media.Document as TLDocument, progress => new TLSendMessageUploadDocumentAction { Progress = progress })); if (upload != null) { var thumbFileId = TLLong.Random(); var thumbUpload = await _uploadDocumentManager.UploadFileAsync(thumbFileId, desiredName); if (thumbUpload != null) { var inputMedia = new TLInputMediaUploadedThumbDocument { File = upload.ToInputFile(), Thumb = thumbUpload.ToInputFile(), MimeType = document.MimeType, Caption = media.Caption, Attributes = document.Attributes }; var result = await ProtoService.SendMediaAsync(_peer, inputMedia, message); } //if (result.IsSucceeded) //{ // var update = result.Result as TLUpdates; // if (update != null) // { // var newMessage = update.Updates.OfType<TLUpdateNewMessage>().FirstOrDefault(); // if (newMessage != null) // { // var newM = newMessage.Message as TLMessage; // if (newM != null) // { // message.Media = newM.Media; // message.RaisePropertyChanged(() => message.Media); // } // } // } //} } }); }
private async Task SendFileAsync(StorageFile file, string caption = null) { if (_peer == null) { return; } var fileLocation = new TLFileLocation { VolumeId = TLLong.Random(), LocalId = TLInt.Random(), Secret = TLLong.Random(), DCId = 0 }; var fileName = string.Format("{0}_{1}_{2}.dat", fileLocation.VolumeId, fileLocation.LocalId, fileLocation.Secret); var fileCache = await FileUtils.CreateTempFileAsync(fileName); await file.CopyAndReplaceAsync(fileCache); var basicProps = await fileCache.GetBasicPropertiesAsync(); var thumbnail = await FileUtils.GetFileThumbnailAsync(file); if (thumbnail as TLPhotoSize != null) { await SendThumbnailFileAsync(file, fileLocation, fileName, basicProps, thumbnail as TLPhotoSize, fileCache, caption); } else { var date = TLUtils.DateToUniversalTimeTLInt(ProtoService.ClientTicksDelta, DateTime.Now); var document = new TLDocument { Id = 0, AccessHash = 0, Date = date, Size = (int)basicProps.Size, MimeType = file.ContentType, Attributes = new TLVector <TLDocumentAttributeBase> { new TLDocumentAttributeFilename { FileName = file.Name } } }; var musicProps = await file.Properties.GetMusicPropertiesAsync(); if (musicProps.Duration > TimeSpan.Zero) { document.Attributes.Add(new TLDocumentAttributeAudio { Duration = (int)musicProps.Duration.TotalSeconds, Title = musicProps.Title, Performer = musicProps.Artist, IsVoice = false, HasTitle = musicProps.Title != null, HasPerformer = musicProps.Artist != null, HasWaveform = false }); } var media = new TLMessageMediaDocument { Document = document, Caption = caption }; var message = TLUtils.GetMessage(SettingsHelper.UserId, _peer.ToPeer(), TLMessageState.Sending, true, true, date, string.Empty, media, TLLong.Random(), null); if (Reply != null) { message.HasReplyToMsgId = true; message.ReplyToMsgId = Reply.Id; message.Reply = Reply; Reply = null; } var previousMessage = InsertSendingMessage(message); CacheService.SyncSendingMessage(message, previousMessage, async(m) => { var fileId = TLLong.Random(); var upload = await _uploadDocumentManager.UploadFileAsync(fileId, fileName, false).AsTask(Upload(media.Document as TLDocument, progress => new TLSendMessageUploadDocumentAction { Progress = progress })); if (upload != null) { var inputMedia = new TLInputMediaUploadedDocument { File = upload.ToInputFile(), MimeType = document.MimeType, Caption = media.Caption, Attributes = document.Attributes }; var result = await ProtoService.SendMediaAsync(_peer, inputMedia, message); //if (result.IsSucceeded) //{ // var update = result.Result as TLUpdates; // if (update != null) // { // var newMessage = update.Updates.OfType<TLUpdateNewMessage>().FirstOrDefault(); // if (newMessage != null) // { // var newM = newMessage.Message as TLMessage; // if (newM != null) // { // message.Media = newM.Media; // message.RaisePropertyChanged(() => message.Media); // } // } // } //} } }); } }