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 void SendSticker(TLDocument22 document) { var chat = Chat as TLEncryptedChat; if (chat == null) { return; } var decryptedMediaExternalDocument = new TLDecryptedMessageMediaExternalDocument { Id = document.Id, AccessHash = document.AccessHash, Date = document.Date, MimeType = document.MimeType, Size = document.Size, Thumb = document.Thumb, DCId = document.DCId, Attributes = new TLVector <TLDocumentAttributeBase> { new TLDocumentAttributeImageSize { H = document.ImageSizeH, W = document.ImageSizeW }, new TLDocumentAttributeFileName { FileName = new TLString("sticker.webp") }, new TLDocumentAttributeSticker() } }; var decryptedTuple = GetDecryptedMessageAndObject(TLString.Empty, decryptedMediaExternalDocument, chat); BeginOnUIThread(() => { Items.Insert(0, decryptedTuple.Item1); RaiseScrollToBottom(); NotifyOfPropertyChange(() => DescriptionVisibility); SendEncrypted(chat, decryptedTuple.Item2, MTProtoService, CacheService); }); }
public void SendSticker(TLDocument22 document) { if (document == null) { return; } var media = new TLMessageMediaDocument75 { Flags = new TLInt(0), FileId = TLLong.Random(), Document = document, Caption = TLString.Empty }; var message = GetMessage(TLString.Empty, media); var inputMedia = new TLInputMediaDocument75 { Flags = new TLInt(0), MD5Hash = new byte[] { }, Id = new TLInputDocument { Id = document.Id, AccessHash = document.AccessHash }, Caption = TLString.Empty }; message.InputMedia = inputMedia; BeginOnUIThread(() => { var previousMessage = InsertSendingMessage(message); IsEmptyDialog = Items.Count == 0 && (_messages == null || _messages.Count == 0) && LazyItems.Count == 0; Text = string.Empty; BeginOnThreadPool(() => CacheService.SyncSendingMessage( message, previousMessage, m => UploadService.SendMediaInternal(message, MTProtoService, StateService, CacheService))); }); }
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))); }); }
public void SendSticker(TLDocument22 document) { var chat = Chat as TLEncryptedChat; if (chat == null) { return; } TLDocumentAttributeSticker attributeSticker = null; var chat17 = Chat as TLEncryptedChat17; if (chat17 != null) { if (chat17.Layer.Value >= Constants.MinSecretChatWithCaptionsLayer) { var attributeSticker29 = new TLDocumentAttributeSticker29 { Alt = TLString.Empty, Stickerset = new TLInputStickerSetEmpty() }; var attributes = document.Attributes; if (attributes != null) { var attribute = document.Attributes.FirstOrDefault(x => x is TLDocumentAttributeSticker) as TLDocumentAttributeSticker29; if (attribute != null) { attributeSticker29.Alt = attribute.Alt; var stickerSet = attribute.Stickerset as TLInputStickerSetShortName; if (stickerSet == null) { var stickerSetId = attribute.Stickerset as TLInputStickerSetId; if (stickerSetId != null) { var allStickers = StateService.GetAllStickers() as TLAllStickers29; if (allStickers != null) { var set = allStickers.Sets.FirstOrDefault(x => x.Id.Value == stickerSetId.Id.Value); if (set != null && !TLString.IsNullOrEmpty(set.ShortName)) { attributeSticker29.Stickerset = new TLInputStickerSetShortName { ShortName = set.ShortName }; } } } } } } attributeSticker = attributeSticker29; } else if (chat17.Layer.Value >= Constants.MinSecretChatWithStickersLayer) { attributeSticker = new TLDocumentAttributeSticker(); } } var stickerAttributes = new TLVector <TLDocumentAttributeBase> { new TLDocumentAttributeImageSize { H = document.ImageSizeH, W = document.ImageSizeW }, new TLDocumentAttributeFileName { FileName = new TLString("sticker.webp") } }; if (attributeSticker != null) { stickerAttributes.Add(attributeSticker); } var decryptedMediaExternalDocument = new TLDecryptedMessageMediaExternalDocument { Id = document.Id, AccessHash = document.AccessHash, Date = document.Date, MimeType = document.MimeType, Size = document.Size, Thumb = document.Thumb, DCId = document.DCId, Attributes = stickerAttributes }; var decryptedTuple = GetDecryptedMessageAndObject(TLString.Empty, decryptedMediaExternalDocument, chat); BeginOnUIThread(() => { InsertSendingMessage(decryptedTuple.Item1); RaiseScrollToBottom(); NotifyOfPropertyChange(() => DescriptionVisibility); Text = string.Empty; BeginOnThreadPool(() => { SendEncrypted(chat, decryptedTuple.Item2, MTProtoService, CacheService); }); }); }
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))); }); }
private static ImageSource ReturnOrEnqueueSticker(TLDocument22 document, TLStickerItem sticker) { if (document == null) { return(null); } var documentLocalFileName = document.GetFileName(); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { if (!store.FileExists(documentLocalFileName)) { TLObject owner = document; if (sticker != null) { owner = sticker; } // 1. download full size IoC.Get <IDocumentFileManager>().DownloadFileAsync(document.FileName, document.DCId, document.ToInputFileLocation(), owner, document.Size, progress => { }); // 2. download preview var thumbCachedSize = document.Thumb as TLPhotoCachedSize; if (thumbCachedSize != null) { var fileName = "cached" + document.GetFileName(); var buffer = thumbCachedSize.Bytes.Data; if (buffer == null) { return(null); } return(DecodeWebPImage(fileName, buffer, () => { })); } var thumbPhotoSize = document.Thumb as TLPhotoSize; if (thumbPhotoSize != null) { var location = thumbPhotoSize.Location as TLFileLocation; if (location != null) { return(ReturnOrEnqueueStickerPreview(location, sticker, thumbPhotoSize.Size)); } } } else { if (document.DocumentSize > 0 && document.DocumentSize < Telegram.Api.Constants.StickerMaxSize) { byte[] buffer; using (var file = store.OpenFile(documentLocalFileName, FileMode.Open)) { buffer = new byte[file.Length]; file.Read(buffer, 0, buffer.Length); } return(DecodeWebPImage(documentLocalFileName, buffer, () => { using (var localStore = IsolatedStorageFile.GetUserStoreForApplication()) { localStore.DeleteFile(documentLocalFileName); } })); } } } return(null); }