Skip to content

DownloadController

The DownloadController class in the ByteCobra Updater Server is responsible for handling download requests for game or application packages. It's part of the server's controller layer and extends the Controller class from ASP.NET MVC.

Constructor

The constructor takes two parameters:

  • Storage storage: An instance of the Storage class, which handles the storage and retrieval of packages.
  • WebClientAuthenticator? clientAuthenticator: An optional instance of WebClientAuthenticator for authenticating download requests. If null, no authentication is performed.

Properties

  • protected Storage Storage: Holds the storage instance used for accessing package files.
  • protected WebClientAuthenticator? ClientAuthenticator: Holds the authenticator instance for handling client authentication.

Methods

Authenticate

  • protected virtual bool Authenticate(): A method that can be overridden to implement custom authentication logic. By default, it returns true, allowing all requests.

Download

  • [HttpGet("Download")]: Marks the method as a GET endpoint at the route /Download.
  • [RateLimit(Limit = 10, Seconds = 120)]: Applies a rate limit to the endpoint, allowing up to 10 requests per IP every 120 seconds.
  • [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(FileStreamResult))]: Indicates that on successful execution, the method returns a status code 200 with a file stream result.
  • public async Task Download(string name, Platform platform, ushort major, ushort minor): Asynchronously handles download requests. Accepts parameters for the package's name, platform, major version, and minor version.

Download Method Workflow

  1. Validation: Checks if the name parameter is valid. Returns a 400 Bad Request if invalid.
  2. Authentication: Calls the Authenticate method. If authentication fails, returns a 401 Unauthorized.
  3. Package Retrieval: Tries to retrieve the package file path from storage. If not found, returns a 404 Not Found.
  4. File Existence Check: Verifies the existence of the file. If the file doesn't exist, returns a 404 Not Found.
  5. File Download: Returns the file to the client using PhysicalFile, setting the content type to application/octet-stream.

Usage

The DownloadController is used in the server application to provide an endpoint for clients to download game or application packages. It can be customized for different authentication strategies and integrated with various storage solutions.