|
@@ -1,12 +1,13 @@
|
|
|
-using System.Text;
|
|
|
-using ImageConvertService.Biz;
|
|
|
+using ImageConvertService.Biz;
|
|
|
+using ImageConvertService.Biz.Models;
|
|
|
+using ImageConvertService.ExceptionHandling;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
namespace ImageConvertService.Controllers;
|
|
|
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
[ApiController]
|
|
|
-public class ConvertToWebpController(ImageConverter converter, ArchiveFileAccessor archiver) : Controller
|
|
|
+public class ConvertToWebpController(ImageConverter converter, ArchiveFileAccessor archiver, PdfImageReader pdf, ArchiveEntrySkipper filter) : Controller
|
|
|
{
|
|
|
[HttpPost]
|
|
|
public IActionResult SingleFile(IFormFile file)
|
|
@@ -19,33 +20,56 @@ public class ConvertToWebpController(ImageConverter converter, ArchiveFileAccess
|
|
|
}
|
|
|
|
|
|
[HttpPost]
|
|
|
- public IActionResult AnyArchiveToFastZip(IFormFile file)
|
|
|
+ public IActionResult AnyArchive(
|
|
|
+ IFormFile file,
|
|
|
+ [FromForm, FromQuery] OutputArchiveFormat format = OutputArchiveFormat.Fast7Z,
|
|
|
+ [FromForm, FromQuery] string? skips = null)
|
|
|
{
|
|
|
var ms = new MemoryStream();
|
|
|
file.CopyTo(ms);
|
|
|
var bytes = ms.ToArray();
|
|
|
- var entries = archiver.ReadArchive(bytes);
|
|
|
|
|
|
- var entriesToCompress = converter.ConvertArchiveEntries(entries,out var withErrors);
|
|
|
+ var (direct, convert) = filter.Filter(archiver.ReadArchive(bytes), skips);
|
|
|
|
|
|
- var outBytes = archiver.MakeArchiveFastZip(entriesToCompress);
|
|
|
+ var noError = converter.ConvertToWebp(convert);
|
|
|
|
|
|
- return File(outBytes, "application/zip", $"{file.FileName}-webp{(withErrors?$"-{nameof(withErrors)}":"")}.zip");
|
|
|
- }
|
|
|
+ direct.AddRange(convert);
|
|
|
|
|
|
+ return OutputAsArchive(file.FileName, format, direct, noError);
|
|
|
+ }
|
|
|
|
|
|
[HttpPost]
|
|
|
- public IActionResult AnyArchiveToFast7Z(IFormFile file)
|
|
|
+ public async Task<IActionResult> AnyArchiveStreaming(
|
|
|
+ [FromQuery] string filename,
|
|
|
+ [FromQuery] OutputArchiveFormat format = OutputArchiveFormat.Fast7Z,
|
|
|
+ [FromQuery] string? skips = null)
|
|
|
{
|
|
|
- var ms = new MemoryStream();
|
|
|
- file.CopyTo(ms);
|
|
|
- var bytes = ms.ToArray();
|
|
|
- var entries = archiver.ReadArchive(bytes);
|
|
|
+ await using var tmpFileStream = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.DeleteOnClose);
|
|
|
+ await Request.Body.CopyToAsync(tmpFileStream);
|
|
|
+ tmpFileStream.Position = 0;
|
|
|
+
|
|
|
+ var (direct, convert) = filter.Filter(archiver.ReadArchive(tmpFileStream), skips);
|
|
|
|
|
|
- var entriesToCompress = converter.ConvertArchiveEntries(entries,out var withErrors);
|
|
|
+ var noError = converter.ConvertToWebp(convert);
|
|
|
|
|
|
- var outBytes = archiver.MakeArchiveFast7Z(entriesToCompress);
|
|
|
+ direct.AddRange(convert);
|
|
|
|
|
|
- return File(outBytes, "application/x-7z-compressed", $"{file.FileName}-webp{(withErrors ? $"-{nameof(withErrors)}" : "")}.7z");
|
|
|
+ return OutputAsArchive(filename, format, direct, noError);
|
|
|
+ }
|
|
|
+
|
|
|
+ private IActionResult OutputAsArchive(string filename, OutputArchiveFormat format, List<ArchiveEntry> direct, bool noError)
|
|
|
+ {
|
|
|
+ return format switch
|
|
|
+ {
|
|
|
+ OutputArchiveFormat.Fast7Z => File(archiver.MakeArchiveFast7Z(direct), "application/x-7z-compressed", $"{filename}-webp{(noError ? "" : "-withErrors")}.7z"),
|
|
|
+ OutputArchiveFormat.FastZip => File(archiver.MakeArchiveFastZip(direct), "application/zip", $"{filename}-webp{(noError ? "" : "-withErrors")}.zip"),
|
|
|
+ _ => throw new UserFriendlyException($"Invalid {nameof(OutputArchiveFormat)}")
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ public enum OutputArchiveFormat
|
|
|
+ {
|
|
|
+ Fast7Z,
|
|
|
+ FastZip
|
|
|
}
|
|
|
}
|