Skip to content

UploadController

The UploadController class in the ByteCobra Updater Server framework is a critical component designed for handling the upload of new versions of games or applications. Extending from the Controller class of ASP.NET MVC, this controller manages the process of receiving and storing package files from authenticated administrators.

The UploadController is critical for systems where administrators need to securely upload new versions of software or updates. Proper configuration and integration with an authenticator and storage system are essential for secure and efficient functionality.

Class Description

  • Responsibility: The primary responsibility of the UploadController is to facilitate the secure and efficient upload of game or application packages to the server. It includes authenticating the upload requests and storing the received files in a specified storage system.

Constructor

The constructor of UploadController accepts two parameters:

  • WebAdminAuthenticator adminAuthenticator: An instance of WebAdminAuthenticator for authenticating administrators.
  • Storage fileStorage: An instance of the Storage class responsible for handling the storage of uploaded files.

Properties

  • protected Storage Storage: Holds the reference to the Storage instance used for storing uploaded files.
  • protected WebAdminAuthenticator AdminAuthenticator: Holds the reference to the WebAdminAuthenticator used for authenticating admin users.

Methods

Upload

  • [HttpPost("Upload")]: Marks the method as a POST endpoint at the route /Upload.
  • public async Task Upload(): Handles the upload of files. It performs admin authentication, validates the uploaded file, and stores it using the Storage service.

Upload Method Workflow

  1. Admin Authentication: Utilizes AdminAuthenticator to authenticate the admin making the upload request. Returns a 401 Unauthorized response if authentication fails.
  2. File Validation: Checks for the presence and validity of the uploaded file. Returns a 400 Bad Request response for invalid or missing files.
  3. File Storage: Efficiently handles the uploaded file by streaming it directly to the Storage service for processing and safekeeping. This streaming approach is crucial as it avoids loading the entire game into the server's RAM, thereby optimizing memory usage and enhancing performance, especially for large game uploads.
  4. Response: Returns an Ok (200) status upon successful upload.

Example Usage

To use UploadController, it must be properly configured within an ASP.NET MVC application. The WebAdminAuthenticator and Storage instances should be provided, typically through dependency injection.

public class MyUploadController : UploadController
{
    public MyUploadController(
        WebAdminAuthenticator adminAuthenticator, 
        Storage fileStorage)
        : base(adminAuthenticator, fileStorage) { }

    // Optional: Override methods to provide custom behavior if needed
}

In this example, MyUploadController extends UploadController and uses dependency injection to provide necessary instances of WebAdminAuthenticator and Storage. This setup allows for custom implementations or extensions of the uploading logic if needed.