Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Let's extend sf::ftp::upload() function with "append" parameter  (Read 2879 times)

0 Members and 1 Guest are viewing this topic.

irrmich

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
I come here to ask for extending the current ftp::upload function which only support overwriting remote file, I suggest to replace it.

So the previous function:

Response sf::Ftp::upload        (       const std::string &     localFile,
const std::string &     remotePath,
TransferMode    mode = Binary
)
{
    // Get the contents of the file to send
    std::ifstream file(localFile.c_str(), std::ios_base::binary);
    if (!file)
        return Response(Response::InvalidFile);

    // Extract the filename from the file path
    std::string filename = localFile;
    std::string::size_type pos = filename.find_last_of("/\\");
    if (pos != std::string::npos)
        filename = filename.substr(pos + 1);

    // Make sure the destination path ends with a slash
    std::string path = remotePath;
    if (!path.empty() && (path[path.size() - 1] != '\\') && (path[path.size() - 1] != '/'))
        path += "/";

    // Open a data channel using the given transfer mode
    DataChannel data(*this);
    Response response = data.open(mode);
    if (response.isOk())
    {
        // Tell the server to start the transfer
        response = sendCommand("STOR", path + filename);
        if (response.isOk())
        {
            // Send the file data
            data.send(file);

            // Get the response from the server
            response = getResponse();
        }
    }

    return response;
}
 

should be replaced by:

Response sf::Ftp::upload        (       const std::string &     localFile,
const std::string &     remotePath,
TransferMode    mode = Binary,
bool append = false
)
{
    // Get the contents of the file to send
    std::ifstream file(localFile.c_str(), std::ios_base::binary);
    if (!file)
        return Response(Response::InvalidFile);

    // Extract the filename from the file path
    std::string filename = localFile;
    std::string::size_type pos = filename.find_last_of("/\\");
    if (pos != std::string::npos)
        filename = filename.substr(pos + 1);

    // Make sure the destination path ends with a slash
    std::string path = remotePath;
    if (!path.empty() && (path[path.size() - 1] != '\\') && (path[path.size() - 1] != '/'))
        path += "/";

    // Open a data channel using the given transfer mode
    DataChannel data(*this);
    Response response = data.open(mode);
    if (response.isOk())
    {
        // Tell the server to start the transfer
        if(append)
        {
                response = sendCommand("APPE", path + filename);
         } else
         {
                response = sendCommand("STOR", path + filename)
         }
       
        if (response.isOk())
        {
            // Send the file data
            data.send(file);

            // Get the response from the server
            response = getResponse();
        }
    }

    return response;
}
 

I searched on google, on stackoverflow and i undestood that:
      _wininet does not support ftp append correctly
      _other ftp libs which support this feature are commercial or outdated

SFML network must implement it so that we attire more network programmers to SFML, and it won't affect client codes which use the current implementation of sf::ftp::upload()  8)
« Last Edit: March 18, 2016, 12:39:45 pm by irrmich »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Let's extend sf::ftp::upload() function with "append" parameter
« Reply #1 on: March 18, 2016, 02:50:24 pm »
If it's really that simple, I guess we can do it. You can add a task (or even better, a PR) to the github tracker so that we don't forget it.
Laurent Gomila - SFML developer

irrmich

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Let's extend sf::ftp::upload() function with "append" parameter
« Reply #2 on: March 18, 2016, 04:29:21 pm »
 ;D okay I've created a PULL REQUEST to do this
I tested it on my machine, it works fine with my real ftp server and on filezilla too :)