Example #1
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)));
            });
        }
        private void SendVideo(RecordedVideo recorderVideo)
        {
            if (recorderVideo == null)
            {
                return;
            }

            long size = 0;

            using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var file = storage.OpenFile(recorderVideo.FileName, FileMode.Open, FileAccess.Read))
                {
                    size = file.Length;
                }
            }

            long photoSize = 0;

            using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var file = storage.OpenFile(recorderVideo.FileName + ".jpg", FileMode.Open, FileAccess.Read))
                {
                    photoSize = file.Length;
                }
            }

            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,
            };

            var fileName = String.Format("{0}_{1}_{2}.jpg",
                                         thumbLocation.VolumeId,
                                         thumbLocation.LocalId,
                                         thumbLocation.Secret);

            // заменяем имя на стандартное для всех каритинок
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                store.CopyFile(recorderVideo.FileName + ".jpg", fileName, true);
                store.DeleteFile(recorderVideo.FileName + ".jpg");
            }

            var thumbSize = new TLPhotoSize
            {
                W        = new TLInt(640),
                H        = new TLInt(480),
                Size     = new TLInt((int)photoSize),
                Type     = new TLString(""),
                Location = thumbLocation,
            };

            var documentAttributeVideo = new TLDocumentAttributeVideo
            {
                Duration = new TLInt((int)recorderVideo.Duration),
                W        = new TLInt(640),
                H        = new TLInt(480)
            };

            var document = new TLDocument54
            {
                Id         = new TLLong(0),
                AccessHash = new TLLong(0),
                Date       = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                MimeType   = new TLString("video/mp4"),
                Size       = new TLInt((int)size),
                Thumb      = thumbSize,
                DCId       = new TLInt(0),
                Version    = new TLInt(0),
                Attributes = new TLVector <TLDocumentAttributeBase> {
                    documentAttributeVideo
                }
            };

            var media = new TLMessageMediaDocument75
            {
                Flags       = new TLInt(0),
                FileId      = recorderVideo.FileId ?? TLLong.Random(),
                Document    = document,
                IsoFileName = recorderVideo.FileName,
                Caption     = TLString.Empty
            };

            var message = GetMessage(TLString.Empty, media);

            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, null)));
            });
        }