--- description: Controller for uploading and downloading media files. --- # MediaController ### Controller Description * **Route**: `api/media` * **Authorize**: Requires authenticated user with roles "User" or "Admin" (`[Authorize(Roles = "User,Admin")]`). ### Endpoints #### Upload * **Description**: Uploads a media file with associated metadata. * **Route**: `[POST] api/media/upload` * **HTTP Method**: `POST` * **Request**: * **Content-Type**: `multipart/form-data` * **Request Body**: `MediaUploadRequest` object with the following structure: ```json { "fromFile": "IFormFile", "type": "MediaType", "mimeType": "string", "encryptedKey": "string" } ``` * **Constraints**: File size limit of 20 MB. * **Responses**: * **200 OK**: Returns the upload result. ```json "string" // Media ID or similar result ``` * **400 Bad Request**: * If model validation fails. * If no file is uploaded: `"No file uploaded"` * If file exceeds 20 MB: `"File is too large"` * If MIME type is missing: `"Missing MIME type"` * If operation is invalid: `"string"` * **403 Forbidden**: If user lacks authorization. ```json "string" ``` * **500 Internal Server Error**: Indicates an unexpected error. ```json "Internal server error" ``` #### Download * **Description**: Downloads a media file by its ID. * **Route**: `[GET] api/media/download/{id}` * **HTTP Method**: `GET` * **Request**: * **Path Parameter**: `id` (Guid, required): ID of the media file. * **Responses**: * **200 OK**: Returns the media file as a stream. * **Content-Type**: Matches `MimeType` of the media. * **Content-Disposition**: Includes `filename` from `FileName`. * **403 Forbidden**: If user lacks access. * No body. * **404 Not Found**: If media is not found. ```json "Media not found" ``` * **500 Internal Server Error**: Indicates an unexpected error. ```json "Internal server error" ``` ### Data Models #### MediaUploadRequest * **Description**: Model for media upload request data. * **Structure**: ```json { "fromFile": "IFormFile", "type": "MediaType", "mimeType": "string", "encryptedKey": "string" } ``` ### Error Handling * **Invalid User ID**: Handled by `ICurrentUserService`, aborts if invalid. * **Invalid Operations**: Returns `400 Bad Request` for `InvalidOperationException`. * **Unauthorized Access**: Returns `403 Forbidden` for `UnauthorizedAccessException`. * **Resource Not Found**: Returns `404 Not Found` for `KeyNotFoundException`. * **Unexpected Errors**: Caught and returned as `500 Internal Server Error`. ### Logging * Logs warnings for `UnauthorizedAccessException`, `InvalidOperationException`, and `KeyNotFoundException`. * Logs information for successful file uploads. * Logs errors for unexpected exceptions during upload and download.