NetCoreServer icon indicating copy to clipboard operation
NetCoreServer copied to clipboard

Simple file server?

Open derekantrican opened this issue 3 years ago • 3 comments

How would I use NetCoreServer to serve a "directory browser"/file server like https://ostechnix.com/how-to-setup-a-file-server-in-minutes-using-python/?

Another example: https://github.com/natemcmaster/dotnet-serve

dotnet tool install --global dotnet-serve
dotnet serve -p 8000

derekantrican avatar Jul 08 '21 18:07 derekantrican

I know this is a late reply, but in case you still need help...

          string wroot = @"www";
          // Process HTTP request methods
          if (request.Method == "HEAD")
          {
              UIupdate.UpdateNetworkOutput("https", "Request HEAD", Color.LimeGreen);
              SendResponseAsync(Response.MakeHeadResponse());
          }
          else if (request.Method == "GET")
          {
              string path = wroot + request.Url.Replace("/", @"\");
              UIupdate.UpdateNetworkOutput("https", $"Request Path: {path}", Color.LimeGreen);

              // Begin https://github.com/chronoxor Contribution
              string content;
              try { content = File.ReadAllText(path); }
              catch { content = "Invalid Request\n\nAre you lost?"; }

              Response.Clear();
              Response.SetBegin(200);
              Response.SetContentType(Path.GetExtension(path));
              Response.SetHeader("Cache-Control", $"max-age=3600");
              Response.SetBody(content);

              SendResponseAsync(Response);
              // End https://github.com/chronoxor Contribution
          }

Something like this above will serve files using either the HttpServer example or the HttpsServer example.

I hope this helps.

thewhiterabbit avatar Feb 23 '22 19:02 thewhiterabbit

@thewhiterabbit Thanks. Looking at that, it looks like it only works for files, not folders - is that correct?

derekantrican avatar Feb 23 '22 19:02 derekantrican

Currently it would only see a file and return the text.

You can add in some statement to check for folders and return default files within them based on regex or string comparison.

thewhiterabbit avatar Feb 23 '22 20:02 thewhiterabbit