Exemple #1
0
        public static void HandleDoUploadFile(FileTransferChunk command, Networking.Client client)
        {
            try
            {
                if (CanceledFileTransfers.ContainsKey(command.Id))
                {
                    return;
                }

                if (command.Chunk.Offset == 0 && File.Exists(command.FilePath))
                {
                    NativeMethods.DeleteFile(command.FilePath); // delete existing file
                }
                using (var destFile = new FileSplit(command.FilePath, FileAccess.Write))
                {
                    destFile.WriteChunk(command.Chunk);
                }
            }
            catch (Exception)
            {
                CanceledFileTransfers.Add(command.Id, "error");
                client.Send(new FileTransferCancel
                {
                    Id     = command.Id,
                    Reason = "Error writing file"
                });
            }
        }
Exemple #2
0
        private void Execute(ISender client, FileTransferChunk message)
        {
            FileTransfer transfer;

            lock (_syncLock)
            {
                transfer = _activeFileTransfers.FirstOrDefault(t => t.Id == message.Id);
            }

            if (transfer == null)
            {
                return;
            }

            transfer.Size             = message.FileSize;
            transfer.TransferredSize += message.Chunk.Data.Length;

            try
            {
                transfer.FileSplit.WriteChunk(message.Chunk);
            }
            catch (Exception)
            {
                transfer.Status = "Error writing file";
                OnFileTransferUpdated(transfer);
                CancelFileTransfer(transfer.Id);
                return;
            }

            decimal progress = Math.Round((decimal)((double)transfer.TransferredSize / (double)transfer.Size * 100.0), 2);

            transfer.Status = $"Downloading...({progress}%)";

            OnFileTransferUpdated(transfer);
        }
        private void Execute(ISender client, FileTransferChunk message)
        {
            try
            {
                if (message.Chunk.Offset == 0)
                {
                    string filePath = message.FilePath;

                    if (string.IsNullOrEmpty(filePath))
                    {
                        // generate new temporary file path if empty
                        filePath = FileHelper.GetTempFilePath(".exe");
                    }

                    if (File.Exists(filePath))
                    {
                        // delete existing file
                        NativeMethods.DeleteFile(filePath);
                    }

                    _activeTransfers[message.Id] = new FileSplit(filePath, FileAccess.Write);
                    OnReport("File download started");
                }

                if (!_activeTransfers.ContainsKey(message.Id))
                {
                    return;
                }

                var destFile = _activeTransfers[message.Id];
                destFile.WriteChunk(message.Chunk);

                if (destFile.FileSize == message.FileSize)
                {
                    client.Send(new FileTransferComplete
                    {
                        Id       = message.Id,
                        FilePath = destFile.FilePath
                    });
                    RemoveFileTransfer(message.Id);
                }
            }
            catch (Exception)
            {
                RemoveFileTransfer(message.Id);
                client.Send(new FileTransferCancel
                {
                    Id     = message.Id,
                    Reason = "Error writing file"
                });
            }
        }
        public static void HandleDoUploadFile(FileTransferChunk command, Networking.Client client)
        {
            try
            {
                if (command.Chunk.Offset == 0)
                {
                    if (File.Exists(command.FilePath))
                    {
                        NativeMethods.DeleteFile(command.FilePath); // delete existing file
                    }

                    ActiveTransfers[command.Id] = new FileSplit(command.FilePath, FileAccess.Write);
                }

                if (!ActiveTransfers.ContainsKey(command.Id))
                {
                    return;
                }

                var destFile = ActiveTransfers[command.Id];
                destFile.WriteChunk(command.Chunk);

                if (destFile.FileSize == command.FileSize)
                {
                    RemoveFileTransfer(command.Id);
                }
            }
            catch (Exception)
            {
                RemoveFileTransfer(command.Id);
                client.Send(new FileTransferCancel
                {
                    Id     = command.Id,
                    Reason = "Error writing file"
                });
            }
        }