2024-10-11 01:09:12 -05:00
|
|
|
|
using HarfBuzzSharp;
|
|
|
|
|
|
using LANCommander.Server.Data.Models;
|
|
|
|
|
|
using LANCommander.Server.Models;
|
2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Services;
|
2023-04-07 13:46:12 -05:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-10-11 01:09:12 -05:00
|
|
|
|
using Octokit.Internal;
|
|
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2023-04-07 13:46:12 -05:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Controllers.Api
|
2023-04-07 13:46:12 -05:00
|
|
|
|
{
|
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
|
[ApiController]
|
2024-08-28 20:33:59 -05:00
|
|
|
|
public class UploadController : BaseApiController
|
2023-04-07 13:46:12 -05:00
|
|
|
|
{
|
2024-10-11 01:09:12 -05:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2024-08-28 20:33:59 -05:00
|
|
|
|
|
2023-04-07 13:46:12 -05:00
|
|
|
|
[HttpPost("Init")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<string> InitAsync(SDK.Models.UploadInitRequest request)
|
2023-04-07 13:46:12 -05:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
var storageLocation = await StorageLocationService.GetAsync(request.StorageLocationId);
|
2023-04-07 13:46:12 -05:00
|
|
|
|
|
2024-10-11 01:09:12 -05:00
|
|
|
|
if (!Directory.Exists(storageLocation.Path))
|
|
|
|
|
|
Directory.CreateDirectory(storageLocation.Path);
|
|
|
|
|
|
|
|
|
|
|
|
var archive = new Archive
|
|
|
|
|
|
{
|
|
|
|
|
|
ObjectKey = Guid.NewGuid().ToString(),
|
2025-01-08 17:10:03 -06:00
|
|
|
|
StorageLocationId = storageLocation.Id,
|
2024-10-11 01:09:12 -05:00
|
|
|
|
Version = ""
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
archive = await ArchiveService.AddAsync(archive);
|
2024-10-11 01:09:12 -05:00
|
|
|
|
|
2025-01-08 17:10:03 -06:00
|
|
|
|
var archivePath = await ArchiveService.GetArchiveFileLocationAsync(archive);
|
2024-10-11 01:09:12 -05:00
|
|
|
|
|
2025-01-21 18:47:06 -06:00
|
|
|
|
await Cache.SetAsync($"ChunkArchivePath/{archive.ObjectKey}", archivePath, TimeSpan.FromHours(6));
|
2023-04-07 13:46:12 -05:00
|
|
|
|
|
2024-10-11 01:09:12 -05:00
|
|
|
|
if (!System.IO.File.Exists(archivePath))
|
|
|
|
|
|
System.IO.File.Create(archivePath).Close();
|
|
|
|
|
|
else
|
|
|
|
|
|
System.IO.File.Delete(archivePath);
|
2023-04-07 13:46:12 -05:00
|
|
|
|
|
2025-01-08 17:10:03 -06:00
|
|
|
|
return archive.ObjectKey;
|
2023-04-07 13:46:12 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("Chunk")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task ChunkAsync([FromForm] ChunkUpload chunk)
|
2023-04-07 13:46:12 -05:00
|
|
|
|
{
|
2025-01-08 17:10:03 -06:00
|
|
|
|
var filePath = await Cache.GetOrDefaultAsync($"ChunkArchivePath/{chunk.Key}", String.Empty);
|
2023-04-07 13:46:12 -05:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-21 18:47:06 -06:00
|
|
|
|
|
|
|
|
|
|
if (chunk.End == chunk.Total)
|
|
|
|
|
|
await Cache.ExpireAsync($"ChunkArchivePath/{chunk.Key}");
|
2023-04-07 13:46:12 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|