FubarDevelopment / FtpServer

Portable FTP server written in .NET
http://fubardevelopment.github.io/FtpServer/
MIT License
472 stars 161 forks source link

InMemoryFileSystem documentation #160

Open piersdeseilligny opened 7 months ago

piersdeseilligny commented 7 months ago

I'm just creating this issue because as far as I can tell, there's no documentation or examples anywhere of how to use the InMemoryFileSystem, and it's not extremely clear how to do so - would it be possible to get some sample code of a very simple setup, just to get an idea of what's required to provide a "virtual" file system to a client, and to receive incoming requests and files?

Thanks for such a great extension! Piers

dingetje commented 2 months ago

I second this request

ChairmanMawh commented 1 month ago

There didn't seem to be much to it when I gave it a go. My Main looked like the one from the DotNetFileSystem example, except with swapped names for InMemory. I chose to allow an anonymous access:

static async Task Main(string[] args)
{
    // Setup dependency injection
    var services = new ServiceCollection();

    // Add FTP server services
    // AnonymousMembershipProvider = allow only anonymous logins
    services.AddFtpServer(builder => builder
        .UseInMemoryFileSystem() 
        .EnableAnonymousAuthentication()); // allow anonymous logins

    // Configure the FTP server
    services.Configure<FtpServerOptions>(opt => opt.ServerAddress = "127.0.0.1");

    // Build the service provider
    using (var serviceProvider = services.BuildServiceProvider())
    {
        // Initialize the FTP server
        var ftpServerHost = serviceProvider.GetRequiredService<IFtpServerHost>();

        // Start the FTP server
        await ftpServerHost.StartAsync();

        Console.WriteLine("Press ENTER/RETURN to close the test application.");
        Console.ReadLine();

        // Stop the FTP server
        await ftpServerHost.StopAsync();
    }
}

That was pretty much it, I ran it, connected with filezilla and uploaded stuff, i could see it, when I restarted the server it was gone (as you might expect!)

I took a quick look into the code (download it from this repo and have a read of the comments in InMemory project) to work out e.g. that

services.Configure<InMemoryFileSystemOptions>(opt => opt.KeepAnonymousFileSystem = false);

Means the server will throw away uploaded files when the anonymous user disconnects/reconnects, whereas setting it to true means the files will be kept across client disconnect/reconnect cycles (but not server restarts)