LANCommander/LANCommander.Server/Controllers/Api/UploadController.cs
Pat Hartl add770b227 Refactor Settings
Settings for the server are now combined with the settings from the SDK. This introduces a large breaking change and a migration should be created. This refactor utilizes .NET Configuration and the Options pattern. This will allow for the overriding of any setting using envionment variables. It also means that Settings.yml can be used to override any .NET configuration. A SettingsProvider implementation was created, and any updating of settings was changed from SettingsService.SaveSettings() to SettingsProvider.Update(s => { ... })
2025-11-16 12:31:25 -06:00

85 lines
2.9 KiB
C#

using LANCommander.Server.Data.Models;
using LANCommander.Server.Models;
using LANCommander.Server.Services;
using Microsoft.AspNetCore.Mvc;
using ZiggyCreatures.Caching.Fusion;
namespace LANCommander.Server.Controllers.Api
{
[Route("api/[controller]")]
[ApiController]
public class UploadController : BaseApiController
{
private readonly StorageLocationService StorageLocationService;
private readonly ArchiveClient _archiveClient;
private readonly IFusionCache Cache;
public UploadController(
ILogger<UploadController> logger,
SettingsProvider<Settings.Settings> settingsProvider,
StorageLocationService storageLocationService,
ArchiveClient archiveClient,
IFusionCache cache) : base(logger, settingsProvider)
{
StorageLocationService = storageLocationService;
_archiveClient = archiveClient;
Cache = cache;
}
[HttpPost("Init")]
public async Task<string> InitAsync(SDK.Models.UploadInitRequest request)
{
var storageLocation = await StorageLocationService.GetAsync(request.StorageLocationId);
if (!Directory.Exists(storageLocation.Path))
Directory.CreateDirectory(storageLocation.Path);
var archive = new Archive
{
ObjectKey = Guid.NewGuid().ToString(),
StorageLocationId = storageLocation.Id,
Version = ""
};
archive = await _archiveClient.AddAsync(archive);
var archivePath = await _archiveClient.GetArchiveFileLocationAsync(archive);
await Cache.SetAsync($"ChunkArchivePath/{archive.ObjectKey}", archivePath, TimeSpan.FromHours(6));
if (!System.IO.File.Exists(archivePath))
System.IO.File.Create(archivePath).Close();
else
System.IO.File.Delete(archivePath);
return archive.ObjectKey;
}
[HttpPost("Chunk")]
public async Task ChunkAsync([FromForm] ChunkUpload chunk)
{
var filePath = await Cache.GetOrDefaultAsync($"ChunkArchivePath/{chunk.Key}", String.Empty);
if (!System.IO.File.Exists(filePath))
throw new Exception("Destination file not initialized.");
Request.EnableBuffering();
using (var ms = new MemoryStream())
{
await chunk.File.CopyToAsync(ms);
var data = ms.ToArray();
using (var fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.None))
{
fs.Position = chunk.Start;
fs.Write(data, 0, data.Length);
}
}
if (chunk.End == chunk.Total)
await Cache.ExpireAsync($"ChunkArchivePath/{chunk.Key}");
}
}
}