Exemplo n.º 1
0
        private async void FileSendRequestReceivedHandler(object sender,
                                                          ToxEventArgs.FileSendRequestEventArgs e)
        {
            if (e.FileKind != ToxFileKind.Data || e.FriendNumber != _friendNumber)
            {
                return;
            }

            var fileId     = _toxModel.FileGetId(e.FriendNumber, e.FileNumber);
            var resumeData = await _fileTransferResumer.GetDownloadData(fileId);

            OneFileTransferModel oneFileTransferModel;

            // If we could find the resume data for this download, we resume it instead of handling it as a new transfer.
            if (resumeData != null)
            {
                oneFileTransferModel =
                    await
                    OneBrokenFileTransferModel.CreateInstance(_dataService, e.FriendNumber, e.FileNumber,
                                                              resumeData.File.Name,
                                                              e.FileSize, TransferDirection.Down, resumeData.File, resumeData.TransferredBytes);
            }
            else
            {
                // We add a transfer with a null value instead of an actual stream here. We will replace it with an actual file stream
                // in OneFileTransferModel.AcceptTransfer() when the user accepts the request and chooses a location to save the file to.
                oneFileTransferModel =
                    await
                    OneFileTransferModel.CreateInstance(_dataService, e.FriendNumber, e.FileNumber, e.FileName,
                                                        e.FileSize,
                                                        TransferDirection.Down, null);
            }

            FileTransferAdded?.Invoke(this, oneFileTransferModel);
        }
Exemplo n.º 2
0
        private async Task ResumeBrokenUploadsForFriend(int friendNumber)
        {
            var resumeDataOfBrokenUploads = await _fileTransferResumer.GetUploadData(friendNumber);

            foreach (var resumeData in resumeDataOfBrokenUploads)
            {
                var fileSizeInBytes = await GetFileSizeInBytes(resumeData.File);

                bool successfulFileSend;
                var  fileInfo = _toxModel.FileSend(resumeData.FriendNumber, ToxFileKind.Data,
                                                   fileSizeInBytes, resumeData.File.Name, resumeData.FileId, out successfulFileSend);

                if (successfulFileSend)
                {
                    var oneFileTransferModel =
                        await
                        OneBrokenFileTransferModel.CreateInstance(_dataService, resumeData.FriendNumber,
                                                                  fileInfo.Number,
                                                                  resumeData.File.Name, fileSizeInBytes, TransferDirection.Up, resumeData.File,
                                                                  resumeData.TransferredBytes);

                    FileTransferAdded?.Invoke(this, oneFileTransferModel);
                }
            }
        }
        public new static async Task<OneFileTransferModel> CreateInstance(int friendNumber, int fileNumber, string name,
            long fileSizeInBytes, TransferDirection direction, StorageFile file, long transferredBytes = 0)
        {
            if (file != null)
                FileTransferResumer.Instance.RecordTransfer(file, friendNumber, fileNumber, direction);

            var fileStream = file == null ? null : await GetStreamBasedOnDirection(file, direction);

            var oneBrokenFileDownloadModel = new OneBrokenFileTransferModel(friendNumber, fileNumber, name,
                fileSizeInBytes, direction, fileStream, transferredBytes);

            if (direction == TransferDirection.Down)
            {
                ToxModel.Instance.FileSeek(friendNumber, fileNumber, transferredBytes);
                ToxModel.Instance.FileControl(friendNumber, fileNumber, ToxFileControl.Resume);
            }

            return oneBrokenFileDownloadModel;
        }
        public new static async Task <OneFileTransferModel> CreateInstance(IDataService dataService, int friendNumber,
                                                                           int fileNumber, string name,
                                                                           long fileSizeInBytes, TransferDirection direction, StorageFile file, long transferredBytes = 0)
        {
            if (file != null)
            {
                dataService.FileTransferResumer.RecordTransfer(file, friendNumber, fileNumber, direction);
            }

            var fileStream = file == null ? null : await GetStreamBasedOnDirection(file, direction);

            var oneBrokenFileDownloadModel = new OneBrokenFileTransferModel(dataService, friendNumber, fileNumber, name,
                                                                            fileSizeInBytes, direction, fileStream, transferredBytes);

            if (direction == TransferDirection.Down)
            {
                dataService.ToxModel.FileSeek(friendNumber, fileNumber, transferredBytes);
                dataService.ToxModel.FileControl(friendNumber, fileNumber, ToxFileControl.Resume);
            }

            return(oneBrokenFileDownloadModel);
        }