Beispiel #1
0
        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 async void SendDocument(StorageFile file)
        {
            var chat = Chat as TLEncryptedChat;

            if (chat == null)
            {
                return;
            }

            if (file == null)
            {
                return;
            }

            var properties = await file.GetBasicPropertiesAsync();

            var size = properties.Size;

            if (!DialogDetailsViewModel.CheckDocumentSize(size))
            {
                MessageBox.Show(string.Format(AppResources.MaximumFileSizeExceeded, MediaSizeConverter.Convert((int)Telegram.Api.Constants.MaximumUploadedFileSize)), AppResources.Error, MessageBoxButton.OK);
                return;
            }

            DialogDetailsViewModel.AddFileToFutureAccessList(file);

            var thumb = await DialogDetailsViewModel.GetFileThumbAsync(file) as TLPhotoSize;

            var dcId       = TLInt.Random();
            var id         = TLLong.Random();
            var accessHash = TLLong.Random();

            var fileLocation = new TLEncryptedFile
            {
                Id             = id,
                AccessHash     = accessHash,
                DCId           = dcId,
                Size           = new TLInt((int)size),
                KeyFingerprint = new TLInt(0),
                FileName       = new TLString(Path.GetFileName(file.Name))
            };

            var keyIV = GenerateKeyIV();

            var decryptedMediaDocumentBase = GetDecryptedMediaDocument(file, chat, thumb, size, keyIV, fileLocation);
            var decryptedMediaDocument45   = decryptedMediaDocumentBase as TLDecryptedMessageMediaDocument45;

            if (decryptedMediaDocument45 != null)
            {
                if (string.Equals(decryptedMediaDocument45.FileExt, "webp", StringComparison.OrdinalIgnoreCase) &&
                    decryptedMediaDocument45.DocumentSize < Telegram.Api.Constants.StickerMaxSize)
                {
                    decryptedMediaDocument45.MimeType = new TLString("image/webp");
                    decryptedMediaDocument45.Attributes.Add(new TLDocumentAttributeSticker29 {
                        Alt = TLString.Empty, Stickerset = new TLInputStickerSetEmpty()
                    });

                    var fileName = decryptedMediaDocument45.GetFileName();
                    await file.CopyAsync(ApplicationData.Current.LocalFolder, fileName, NameCollisionOption.ReplaceExisting);
                }
                else if (string.Equals(decryptedMediaDocument45.FileExt, "mp3", StringComparison.OrdinalIgnoreCase))
                {
                    var musicProperties = await file.Properties.GetMusicPropertiesAsync();

                    if (musicProperties != null)
                    {
                        var documentAttributeAudio = new TLDocumentAttributeAudio32 {
                            Duration = new TLInt(0), Title = TLString.Empty, Performer = TLString.Empty
                        };
                        documentAttributeAudio.Duration = new TLInt((int)musicProperties.Duration.TotalSeconds);
                        if (!string.IsNullOrEmpty(musicProperties.Title))
                        {
                            documentAttributeAudio.Title = new TLString(musicProperties.Title);
                        }
                        if (!string.IsNullOrEmpty(musicProperties.Artist))
                        {
                            documentAttributeAudio.Performer = new TLString(musicProperties.Artist);
                        }
                        decryptedMediaDocument45.Attributes.Add(documentAttributeAudio);
                    }
                }
            }
            var decryptedTuple = GetDecryptedMessageAndObject(TLString.Empty, decryptedMediaDocumentBase, chat, true);

            BeginOnUIThread(() =>
            {
                InsertSendingMessage(decryptedTuple.Item1);
                RaiseScrollToBottom();
                NotifyOfPropertyChange(() => DescriptionVisibility);

                BeginOnThreadPool(() =>
                                  CacheService.SyncDecryptedMessage(decryptedTuple.Item1, chat,
                                                                    cachedMessage => SendDocumentInternal(file, decryptedTuple.Item2)));
            });
        }
Beispiel #3
0
        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)));
            });
        }
Beispiel #4
0
        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 void SendDocument(StorageFile file)
        {
            var chat = Chat as TLEncryptedChat;

            if (chat == null)
            {
                return;
            }

            if (file == null)
            {
                return;
            }

            var properties = await file.GetBasicPropertiesAsync();

            var size = properties.Size;

            if (!DialogDetailsViewModel.CheckDocumentSize(size))
            {
                MessageBox.Show(string.Format(AppResources.MaximumFileSizeExceeded, MediaSizeConverter.Convert((int)Telegram.Api.Constants.MaximumUploadedFileSize)), AppResources.Error, MessageBoxButton.OK);
                return;
            }

            DialogDetailsViewModel.AddFileToFutureAccessList(file);

            var thumb = await DialogDetailsViewModel.GetFileThumbAsync(file) as TLPhotoSize;

            var dcId       = TLInt.Random();
            var id         = TLLong.Random();
            var accessHash = TLLong.Random();

            var fileLocation = new TLEncryptedFile
            {
                Id             = id,
                AccessHash     = accessHash,
                DCId           = dcId,
                Size           = new TLInt((int)size),
                KeyFingerprint = new TLInt(0),
                FileName       = new TLString(Path.GetFileName(file.Name))
            };

            var keyIV = GenerateKeyIV();

            var decryptedMediaDocument = new TLDecryptedMessageMediaDocument
            {
                Thumb    = thumb != null? thumb.Bytes : TLString.Empty,
                ThumbW   = thumb != null? thumb.W : new TLInt(0),
                ThumbH   = thumb != null? thumb.H : new TLInt(0),
                FileName = new TLString(Path.GetFileName(file.Name)),
                MimeType = new TLString(file.ContentType),
                Size     = new TLInt((int)size),
                Key      = keyIV.Item1,
                IV       = keyIV.Item2,

                File        = fileLocation,
                StorageFile = file,

                UploadingProgress = 0.001
            };

            var decryptedTuple = GetDecryptedMessageAndObject(TLString.Empty, decryptedMediaDocument, chat, true);

            BeginOnUIThread(() =>
            {
                Items.Insert(0, decryptedTuple.Item1);
                RaiseScrollToBottom();
                NotifyOfPropertyChange(() => DescriptionVisibility);

                BeginOnThreadPool(() =>
                                  CacheService.SyncDecryptedMessage(decryptedTuple.Item1, chat,
                                                                    cachedMessage => SendDocumentInternal(file, decryptedTuple.Item2)));
            });
        }
        public void ContinueSendVideo(CompressingVideoFile videoFile)
        {
            if (videoFile == null)
            {
                return;
            }

            var file = videoFile.Source;

            if (file == null)
            {
                return;
            }

            if (!CheckDocumentSize(videoFile.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 documentAttributeVideo = new TLDocumentAttributeVideo
            {
                Duration = new TLInt((int)videoFile.Duration),
                W        = new TLInt((int)videoFile.Width),
                H        = new TLInt((int)videoFile.Height)
            };

            var documentAttributeFileName = new TLDocumentAttributeFileName
            {
                FileName = new TLString(file.Name)
            };

            var document = new TLDocument54
            {
                Id         = TLLong.Random(),
                AccessHash = new TLLong(0),
                Date       = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                MimeType   = new TLString(file.ContentType),
                Size       = new TLInt((int)videoFile.Size),
                Thumb      = videoFile.ThumbPhoto ?? new TLPhotoSizeEmpty {
                    Type = TLString.Empty
                },
                DCId       = new TLInt(0),
                Version    = new TLInt(0),
                Attributes = new TLVector <TLDocumentAttributeBase> {
                    documentAttributeFileName, documentAttributeVideo
                }
            };

            if (videoFile.EncodingProfile != null &&
                videoFile.EncodingProfile.Audio == null &&
                videoFile.Size < Telegram.Api.Constants.GifMaxSize &&
                TLString.Equals(document.MimeType, new TLString("video/mp4"), StringComparison.OrdinalIgnoreCase))
            {
                document.Attributes.Add(new TLDocumentAttributeAnimated());
            }

            var media = new TLMessageMediaDocument75 {
                Flags = new TLInt(0), Document = document, IsoFileName = file.Path, File = file, Caption = TLString.Empty
            };

            var caption = string.IsNullOrEmpty(videoFile.Caption) ? TLString.Empty : new TLString(videoFile.Caption);
            var message = GetMessage(caption, media);

            if (videoFile.TimerSpan != null && videoFile.TimerSpan.Seconds > 0)
            {
                message.NotListened = true;
                var ttlMessageMedia = message.Media as ITTLMessageMedia;
                if (ttlMessageMedia != null)
                {
                    ttlMessageMedia.TTLSeconds = new TLInt(videoFile.TimerSpan.Seconds);
                }
            }

            _mentions = videoFile.Mentions;
            var processedText = string.Empty;
            var entities      = GetEntities(message.Message.ToString(), out processedText);

            _mentions = null;

            if (entities.Count > 0)
            {
                message.Message  = new TLString(processedText);
                message.Entities = new TLVector <TLMessageEntityBase>(entities);
            }

            BeginOnUIThread(() =>
            {
                var previousMessage = InsertSendingMessage(message);
                IsEmptyDialog       = Items.Count == 0 && (_messages == null || _messages.Count == 0) && LazyItems.Count == 0;

                BeginOnThreadPool(() =>
                                  CacheService.SyncSendingMessage(
                                      message, previousMessage,
                                      m => SendVideoInternal(message, videoFile)));
            });
        }
        public void SendVideo(CompressingVideoFile videoFile)
        {
            if (videoFile == null)
            {
                return;
            }

            var file = videoFile.Source;

            if (file == null)
            {
                return;
            }

            if (!CheckDocumentSize(videoFile.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 video = new TLVideo
            {
                Id         = new TLLong(0),
                Caption    = new TLString(Path.GetFileName(file.Name)),
                AccessHash = new TLLong(0),
                Date       = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                UserId     = new TLInt(StateService.CurrentUserId),
                Duration   = new TLInt((int)videoFile.Duration),
                MimeType   = new TLString(file.ContentType),
                Size       = new TLInt((int)videoFile.Size),
                Thumb      = videoFile.ThumbPhoto ?? new TLPhotoSizeEmpty {
                    Type = TLString.Empty
                },
                DCId = new TLInt(0),
                W    = new TLInt((int)videoFile.Width),
                H    = new TLInt((int)videoFile.Height)
            };

            var media = new TLMessageMediaVideo28 {
                Video = video, IsoFileName = file.Path, File = file, Caption = TLString.Empty
            };

            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 => SendVideoInternal(message, videoFile)));
            });
        }