Export to zip file (wip) (all)

This commit is contained in:
Thomas Fransolet 2023-04-25 17:08:39 +02:00
parent 578b88cea2
commit 68de09c109
2 changed files with 73 additions and 4 deletions

View File

@ -8,6 +8,7 @@ using Manager.Helpers;
using Manager.Interfaces.DTO; using Manager.Interfaces.DTO;
using Manager.Interfaces.Models; using Manager.Interfaces.Models;
using Manager.Services; using Manager.Services;
using ManagerService.Helpers;
using ManagerService.Service.Services; using ManagerService.Service.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -281,7 +282,7 @@ namespace ManagerService.Controllers
[ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)] [ProducesResponseType(typeof(string), 500)]
[HttpGet("{id}/export")] [HttpGet("{id}/export")]
public FileContentResult Export(string id, [FromQuery] string language) public ActionResult Export(string id, [FromQuery] string language)
{ {
try try
{ {
@ -422,6 +423,29 @@ namespace ManagerService.Controllers
} }
} }
if (language == null)
{
// EXPORT IN ZIP
getResourceDTOFromIds(resourcesDirectory, resourceIds);
ExportConfigurationDTO toDownload = configuration.ToExportDTO(sectionDTOs, null); // intentionnaly putting null to get only data
string jsonString = JsonConvert.SerializeObject(toDownload);
var fileBytes = Encoding.UTF8.GetBytes(jsonString);
var configFileMainTitle = $"config-{configuration.Label.Trim().Replace(" ", "_")}.json";
string configFileMainData = Path.Combine(configurationsDirectory, configFileMainTitle);
// Create the file.
createFile(configFileMainData, fileBytes);
byte[] exportFile = FileHelper.CreateZipArchive(currentDirectory);
var fileName0 = $"{configuration.Label.Trim().Replace(" ", "_")}";
return File(exportFile, "application/zip", $"{fileName0}_{DateTime.Now:yyyyMMdd}.zip");
}
var fileName = $"{configuration.Label.Trim().Replace(" ","_")}.json"; var fileName = $"{configuration.Label.Trim().Replace(" ","_")}.json";
string configFile = Path.Combine(configurationsDirectory, fileName); string configFile = Path.Combine(configurationsDirectory, fileName);
@ -446,10 +470,26 @@ namespace ManagerService.Controllers
else else
{ {
// Get file from folder // Get file from folder
byte[] readText = System.IO.File.ReadAllBytes(configFile); //byte[] readText = System.IO.File.ReadAllBytes(configFile);
byte[] readText;
using (var stream = new FileStream(configFile, FileMode.Open, FileAccess.Read))
{
var buffer = new byte[4096];
using (var ms = new MemoryStream())
{
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, bytesRead);
}
readText = ms.ToArray();
}
}
string exportInString = Encoding.UTF8.GetString(readText); string exportInString = Encoding.UTF8.GetString(readText);
//string exportInString = Encoding.UTF8.GetString(readText);
ExportConfigurationDTO exportConfigurationFromFile = JsonConvert.DeserializeObject<ExportConfigurationDTO>(exportInString); ExportConfigurationDTO exportConfigurationFromFile = JsonConvert.DeserializeObject<ExportConfigurationDTO>(exportInString);
// Get all ids that are not in the existing file // Get all ids that are not in the existing file
@ -469,7 +509,8 @@ namespace ManagerService.Controllers
readText = Encoding.UTF8.GetBytes(jsonString); readText = Encoding.UTF8.GetBytes(jsonString);
// Check if difference // Check if difference
if (exportInString != jsonStringWithAll) { if (exportInString != jsonStringWithAll)
{
// Delete file // Delete file
System.IO.File.Delete(configFile); System.IO.File.Delete(configFile);

View File

@ -0,0 +1,28 @@
using System.IO;
using System.IO.Compression;
namespace ManagerService.Helpers
{
public class FileHelper
{
public static byte[] CreateZipArchive(string configurationFolder)
{
byte[] archiveFiles;
using (var archiveStream = new MemoryStream())
{
using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true))
{
var di = new DirectoryInfo(configurationFolder);
foreach (var file in di.EnumerateFiles("*.*", SearchOption.AllDirectories))
{
archive.CreateEntryFromFile(file.FullName, file.Name);
//file.Delete();
}
//di.Delete();
}
archiveFiles = archiveStream.ToArray();
}
return archiveFiles;
}
}
}