示例#1
0
        public async Task <byte[]> DownloadAllFilePartsAsync(IInputFileLocation location, CancellationToken cancellationToken)
        {
            var filePartSize = location is TInputDocumentFileLocation
                                   ? DownloadDocumentPartSize
                                   : DownloadPhotoPartSize;

            var offset = 0;
            var bytes  = new List <byte>();

            while (true)
            {
                var file = await DownloadFilePartAsync(location, offset, cancellationToken).ConfigureAwait(false);

                if (file == null)
                {
                    return(null);
                }

                bytes.AddRange(file.Bytes);
                if (file.Bytes.Length < filePartSize)
                {
                    return(bytes.ToArray());
                }

                offset += file.Bytes.Length;
            }
        }
        public async Task <IFile> GetFile(IInputFileLocation location, int offset = 0)
        {
            int filePartSize;

            if (location is TInputDocumentFileLocation)
            {
                filePartSize = DownloadDocumentPartSize;
            }
            else
            {
                filePartSize = DownloadPhotoPartSize;
            }

            try
            {
                return(await SenderService.SendRequestAsync(
                           new RequestGetFile
                {
                    Location = location,
                    Limit = filePartSize,
                    Offset = offset
                }).ConfigureAwait(false));
            }
            catch (FileMigrationException ex)
            {
                var exportedAuth = (TExportedAuthorization)await SenderService.SendRequestAsync(
                    new RequestExportAuthorization
                {
                    DcId = ex.Dc
                }).ConfigureAwait(false);

                var authKey       = ClientSettings.Session.AuthKey;
                var timeOffset    = ClientSettings.Session.TimeOffset;
                var serverAddress = ClientSettings.Session.ServerAddress;
                var serverPort    = ClientSettings.Session.Port;

                await ConnectApiService.ReconnectToDcAsync(ex.Dc).ConfigureAwait(false);

                await SenderService.SendRequestAsync(
                    new RequestImportAuthorization
                {
                    Bytes = exportedAuth.Bytes,
                    Id    = exportedAuth.Id
                }).ConfigureAwait(false);

                var result = await GetFile(location, offset).ConfigureAwait(false);

                ClientSettings.Session.AuthKey       = authKey;
                ClientSettings.Session.TimeOffset    = timeOffset;
                ClientSettings.Session.ServerAddress = serverAddress;
                ClientSettings.Session.Port          = serverPort;
                await ConnectApiService.ConnectAsync().ConfigureAwait(false);

                return(result);
            }
        }
示例#3
0
        public async Task <byte[]> DownloadFullFileAsync(IInputFileLocation location, CancellationToken cancellationToken = default(CancellationToken))
        {
            ClientSettings.EnsureUserAuthorized();

            try
            {
                return(await DownloadAllFilePartsAsync(location, cancellationToken).ConfigureAwait(false));
            }
            catch (FileMigrationException ex)
            {
                return(await RequestService.SendRequestToOtherDcAsync(ex.Dc, api => api.FileService.DownloadAllFilePartsAsync(location, cancellationToken), cancellationToken)
                       .ConfigureAwait(false));
            }
        }
示例#4
0
        private async Task <TFile> DownloadFilePartAsync(IInputFileLocation location, int offset, CancellationToken cancellationToken)
        {
            var filePartSize = location is TInputDocumentFileLocation
                                   ? DownloadDocumentPartSize
                                   : DownloadPhotoPartSize;

            var requestGetFile = new RequestGetFile
            {
                Location = location,
                Limit    = filePartSize,
                Offset   = offset
            };

            return((TFile)await RequestService.SendRequestAsync(requestGetFile, cancellationToken).ConfigureAwait(false));
        }