Export to zip file (wip) (all)
This commit is contained in:
parent
578b88cea2
commit
68de09c109
@ -8,6 +8,7 @@ using Manager.Helpers;
|
||||
using Manager.Interfaces.DTO;
|
||||
using Manager.Interfaces.Models;
|
||||
using Manager.Services;
|
||||
using ManagerService.Helpers;
|
||||
using ManagerService.Service.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@ -281,7 +282,7 @@ namespace ManagerService.Controllers
|
||||
[ProducesResponseType(typeof(string), 404)]
|
||||
[ProducesResponseType(typeof(string), 500)]
|
||||
[HttpGet("{id}/export")]
|
||||
public FileContentResult Export(string id, [FromQuery] string language)
|
||||
public ActionResult Export(string id, [FromQuery] string language)
|
||||
{
|
||||
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";
|
||||
string configFile = Path.Combine(configurationsDirectory, fileName);
|
||||
|
||||
@ -446,10 +470,26 @@ namespace ManagerService.Controllers
|
||||
else
|
||||
{
|
||||
// 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);
|
||||
|
||||
ExportConfigurationDTO exportConfigurationFromFile = JsonConvert.DeserializeObject<ExportConfigurationDTO>(exportInString);
|
||||
|
||||
// Get all ids that are not in the existing file
|
||||
@ -469,7 +509,8 @@ namespace ManagerService.Controllers
|
||||
readText = Encoding.UTF8.GetBytes(jsonString);
|
||||
|
||||
// Check if difference
|
||||
if (exportInString != jsonStringWithAll) {
|
||||
if (exportInString != jsonStringWithAll)
|
||||
{
|
||||
// Delete file
|
||||
System.IO.File.Delete(configFile);
|
||||
|
||||
|
||||
28
ManagerService/Helpers/FileHelper.cs
Normal file
28
ManagerService/Helpers/FileHelper.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user