file-exposer/ApiControllers/SyncV1Controller.cs
2025-05-10 13:31:06 -07:00

68 lines
2.6 KiB
C#

using System.IO;
using FileExposer.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace FileExposer.ApiControllers;
[ApiController]
[Route("api/[controller]")]
public class SyncV1Controller(ISyncV1Repository SyncRepository) : Controller, ISyncV1Controller<IActionResult, IFormFileCollection>
{
private readonly ISyncV1Repository _SyncRepository = SyncRepository;
[HttpGet]
public IActionResult Get(string path, long? size = null, long? ticks = null)
{
Get result = _SyncRepository.Get(path, size, ticks);
if (result.Bytes is not null)
return File(result.Bytes, "text/plain");
if (!string.IsNullOrEmpty(result.JSON))
return Content(result.JSON, "application/json", System.Text.Encoding.UTF8);
throw new System.NotImplementedException();
}
[HttpPost]
public IActionResult Post()
{
string result = _SyncRepository.Post(Request.Body);
return Content(result, "application/json", System.Text.Encoding.UTF8);
}
[HttpDelete]
public IActionResult Delete(string path, long size, long ticks)
{
_SyncRepository.Delete(path, size, ticks);
return Ok($"{size}-{ticks}");
}
[HttpPut]
[ApiExplorerSettings(IgnoreApi = true)]
public IActionResult Put([FromForm] string path, IFormFileCollection formFiles)
{
foreach (IFormFile formFile in formFiles)
_SyncRepository.Put(path, formFile.FileName, formFile.OpenReadStream());
return Ok();
}
[HttpPatch]
[ApiExplorerSettings(IgnoreApi = true)]
public IActionResult Patch([FromForm] string path, [FromForm] IFormFileCollection formFiles)
{
foreach (IFormFile formFile in formFiles)
_SyncRepository.Patch(path, formFile.FileName, formFile.OpenReadStream());
return Ok();
}
}
// string st = "d:/Tmp/phares/VisualStudioCode/z-include-patterns - Copy.nsv";
// using HttpRequestMessage httpRequestMessage = new(HttpMethod.Patch, page);
// using MultipartFormDataContent multipartFormDataContent = new();
// multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(st)), "formFiles", $"RelativePath:Test.txt|Size:4|Ticks:1143241;");
// multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(st)), "formFiles", "NewTest.txt");
// multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(st)), "formFiles", "NewestTest.txt");
// multipartFormDataContent.Add(new StringContent(path, "path", path);
// httpRequestMessage.Content = multipartFormDataContent;
// Task<HttpResponseMessage> httpResponseMessage = httpClient.SendAsync(httpRequestMessage);
// httpResponseMessage.Wait();