fineftp-server icon indicating copy to clipboard operation
fineftp-server copied to clipboard

Feature: Callbacks for file operations

Open Tinker-zzz opened this issue 1 year ago • 10 comments

I hope that this library can monitor the FTP folder, such as add, update, delete files can have similar callback interface can be provided to need the function of the third party What do you think of this feature?

Tinker-zzz avatar Jun 26 '24 08:06 Tinker-zzz

hm, this is supposed to be an FTP Server, not a filesystem watcher. What is your use case?

FlorianReimold avatar Oct 10 '24 12:10 FlorianReimold

@Tinker-zzz you mean for the library to raise events based on FTP actions performed on the FTP server? like start of upload, finish of upload, etc ?

ShaharHD avatar Jan 16 '25 14:01 ShaharHD

@ShaharHD Yes, that's exactly what I mean

Tinker-zzz avatar Jan 17 '25 01:01 Tinker-zzz

@FlorianReimold I can add the ability to map async lambda function for events if you see it as a valid option.

I can make it as a method called after construction and before start (to map the lambda)

ShaharHD avatar Jan 30 '25 18:01 ShaharHD

Reopeing this issue, as there seems to be some interest

@ShaharHD: What do you mean by async lambda function? Which thread will be executing them? Do you have a similar use case or are you interested in the implementation alone?

FlorianReimold avatar Feb 05 '25 08:02 FlorianReimold

Reopeing this issue, as there seems to be some interest

@ShaharHD: What do you mean by async lambda function? Which thread will be executing them? Do you have a similar use case or are you interested in the implementation alone?

An automatictly created short lived thread for the async. The actual implementation is not affected by it.

My use case for it is to get events on the FTP server (which I can then act upon in my main application, in my case mailny progressing a FSM and updating statistics)

as far as async lambda - like so:

#include <iostream>
#include <future>

int main() {
    auto async_lambda = std::async(std::launch::async, []() {
        // Code to be executed asynchronously
        std::cout << "Async lambda execution" << std::endl;
        return 42;
    });

    // Perform other tasks while the lambda executes

    // Get the result from the async lambda (this will block until the lambda is done)
    int result = async_lambda.get();
    std::cout << "Result from async lambda: " << result << std::endl;

    return 0;
}

But it'll be a std::function in our case and you can set it before you start the service (like username and password)

ShaharHD avatar Feb 10 '25 23:02 ShaharHD

OK, sounds reasonable. Can you make a proposal?

FlorianReimold avatar Feb 12 '25 12:02 FlorianReimold

OK, sounds reasonable. Can you make a proposal?

@Tinker-zzz I was thinking on "download" and "upload" events - but I can go "full monty" here. @FlorianReimold any reservations on this? I can match each "method" you have in the command_map with a proper event enum or select just specific commands for now.

For example: RETR, STOR and APPE with the filename or also to include PASS with relevant std::shared_ptr<fineftp::FtpUser> or nullptr on failure ?

ShaharHD avatar Feb 13 '25 23:02 ShaharHD

Hm, if you cover download and upload events, you also have to cover renaming, appending, deleting, creating directories etc., so that all filesystem operations can perform callbacks.

I think a signature like Callback(enum callback_type, ????) would simplify this a lot, so the user doesn't have to set dozens of callbacks. You cannot pass std::shared_ptr<fineftp::FtpUser> to a callback, as that class is internal and must not be exposed to the public API.

FlorianReimold avatar Feb 17 '25 16:02 FlorianReimold

I'll do a PR with initial offer next week - see how and if it fits your project design.

ShaharHD avatar Feb 18 '25 15:02 ShaharHD