Ejemplo n.º 1
0
        FtpStream FileToFtpStream(string source)
        {
            try
            {
                FtpStream writeStream;
#pragma warning disable SCS0018 // Path traversal: injection possible in {1} argument passed to '{0}'
                using (FileStream readStream = new FileStream(source, FileMode.Open, FileAccess.Read))
#pragma warning restore SCS0018 // Path traversal: injection possible in {1} argument passed to '{0}'
                {
                    using (writeStream = new FtpStream(new MemoryStream(), false, true, false))
                    {
                        int    Length    = 256;
                        Byte[] buffer    = new Byte[Length];
                        int    bytesread = readStream.Read(buffer, 0, Length);

                        while (bytesread > 0)
                        {
                            writeStream.Write(buffer, 0, bytesread);
                            bytesread = readStream.Read(buffer, 0, Length);
                        }
                    }
                }
                return(writeStream);
            }
            catch (Exception e)
            {
                ProcessApplicationError("", e);
                return(null);
            }
        }
Ejemplo n.º 2
0
        private int SendData(Stream requestStream, Socket Accept)
        {
            if (Accept == null)
            {
                throw new ArgumentNullException(nameof(Accept));
            }
            FtpStream ftpStream = requestStream as FtpStream;

            ftpStream.InternalPosition = 0;

            int Length = (int)ftpStream.InternalLength;

            Byte [] sendbuffer = new Byte[Length];

            ftpStream.InternalRead(sendbuffer, 0, Length);
            int cbReturn = Accept.Send(sendbuffer, Length, 0);

            ftpStream.InternalClose();

            return(cbReturn);
        }
Ejemplo n.º 3
0
        public void Put(string source, string target, string transferMode)
        {
            string targetPath, sourcePath, targetName, sourceName;

            ResetError();

            pathAndName("", source, out sourcePath, out sourceName);
            pathAndName(sourceName, target, out targetPath, out targetName);

            FtpStream writeStream = FileToFtpStream(sourcePath + "\\" + sourceName);

            if (_status != 0)
            {
                return;
            }

            if (_bPassiveMode)
            {
                OpenPassiveDataConnection();
            }
            else
            {
                OpenDataConnection();
            }

            string sztype = "I";

            if (transferMode == "A")
            {
                sztype = "A";
            }

            SendCommand("TYPE", sztype);

            ResponseDescription resp_desc = ReceiveCommandResponse();

            if (!resp_desc.PositiveCompletion)
            {
                ProcessApplicationError("Data negotiation failed:\n" + _sbControlSocketLog.ToString());
                CloseDataConnection();
                return;
            }

            setFtpDir(targetPath);

            SendCommand("STOR", targetName);
            resp_desc = ReceiveCommandResponse();

            if (resp_desc.PositivePreliminary)
            {
                if (writeStream != null)
                {
                    Socket DataConnection;
                    if (_bPassiveMode)
                    {
                        DataConnection = _DataSocket;
                    }
                    else
                    {
                        DataConnection = _DataSocket.Accept();
                    }
                    if (DataConnection == null)
                    {
                        ProcessProtocolViolationError("Accept failed ");
                    }

                    SendData(writeStream, DataConnection);
                    DataConnection.Close();
                    ResponseDescription resp = ReceiveCommandResponse();
                    if (!resp.PositiveCompletion)
                    {
                        _status = resp.Status;
                    }
                    else
                    {
                        _status = 0;
                    }
                    _statusDescription = resp.StatusDescription;
                }
                else
                {
                    ProcessApplicationError("Data to be uploaded not specified");
                    CloseDataConnection();
                    return;
                }
            }
            else
            {
                ProcessApplicationError(ComposeExceptionMessage(resp_desc, _sbControlSocketLog.ToString()));
                CloseDataConnection();
                return;
            }
            CloseDataConnection();

            return;
        }