LANCommander/LANCommander.Server/Controllers/UploadController.cs

56 lines
1.7 KiB
C#
Raw Permalink Normal View History

using HarfBuzzSharp;
using LANCommander.Server.Data.Models;
using LANCommander.Server.Models;
2024-08-04 18:44:33 -05:00
using LANCommander.Server.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ZiggyCreatures.Caching.Fusion;
2024-08-04 18:44:33 -05:00
namespace LANCommander.Server.Controllers
{
[Authorize(Roles = RoleService.AdministratorRoleName)]
public class UploadController : BaseController
{
private readonly StorageLocationService StorageLocationService;
private readonly ArchiveService ArchiveService;
private readonly IFusionCache Cache;
public UploadController(
ILogger<UploadController> logger,
StorageLocationService storageLocationService,
ArchiveService archiveService,
IFusionCache cache) : base(logger)
{
StorageLocationService = storageLocationService;
ArchiveService = archiveService;
Cache = cache;
}
2023-09-10 17:11:06 -05:00
[HttpPost]
2024-11-12 23:22:01 -06:00
public async Task<IActionResult> FileAsync(IFormFile file, string path)
2023-09-10 17:11:06 -05:00
{
try
{
if (!Directory.Exists(path))
return BadRequest("Destination path does not exist.");
path = Path.Combine(path, file.FileName);
using (var fileStream = System.IO.File.OpenWrite(path))
{
await file.CopyToAsync(fileStream);
}
return Ok();
}
catch (Exception ex)
{
Logger?.LogError(ex, "An error occurred while uploading the file");
2023-09-10 17:11:06 -05:00
return BadRequest("An error occurred while uploading the file.");
}
}
}
}