missing files
This commit is contained in:
parent
94e0c1e647
commit
92d7efd34d
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -293,17 +294,35 @@ namespace ManagerService.Controllers
|
|||||||
throw new KeyNotFoundException("Configuration does not exist");
|
throw new KeyNotFoundException("Configuration does not exist");
|
||||||
|
|
||||||
List<SectionDTO> sectionDTOs = _sectionService.GetAllFromConfiguration(configuration.Id).Select(s => s.ToDTO()).ToList();
|
List<SectionDTO> sectionDTOs = _sectionService.GetAllFromConfiguration(configuration.Id).Select(s => s.ToDTO()).ToList();
|
||||||
List<ResourceDTO> resourceDTOs = new List<ResourceDTO>();
|
List<string> resourceIds = new List<string>();
|
||||||
|
|
||||||
|
var mimeType = "application/json";
|
||||||
|
|
||||||
|
string currentDirectory = System.IO.Directory.GetCurrentDirectory();
|
||||||
|
Console.WriteLine($"currentDirectory: {currentDirectory}");
|
||||||
|
#if RELEASE
|
||||||
|
Console.WriteLine($"currentDirectory: {Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}");
|
||||||
|
currentDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "service-data");
|
||||||
|
System.IO.Directory.CreateDirectory(currentDirectory.ToString());
|
||||||
|
#endif
|
||||||
|
|
||||||
|
var resourcesDirectory = Path.Combine(currentDirectory, "resources");
|
||||||
|
System.IO.Directory.CreateDirectory(resourcesDirectory.ToString());
|
||||||
|
Console.WriteLine($"resourcesDirectory: {resourcesDirectory}");
|
||||||
|
|
||||||
|
var configurationsDirectory = Path.Combine(currentDirectory, "configurations");
|
||||||
|
System.IO.Directory.CreateDirectory(configurationsDirectory.ToString());
|
||||||
|
Console.WriteLine($"configurationsDirectory: {configurationsDirectory}");
|
||||||
|
|
||||||
if (configuration.ImageId != null)
|
if (configuration.ImageId != null)
|
||||||
{
|
{
|
||||||
addResourceToList(resourceDTOs, configuration.ImageId);
|
addResourceIdToList(resourceIds, configuration.ImageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var section in sectionDTOs)
|
foreach (var section in sectionDTOs)
|
||||||
{
|
{
|
||||||
if (section.imageId != null) {
|
if (section.imageId != null) {
|
||||||
addResourceToList(resourceDTOs, section.imageId);
|
addResourceIdToList(resourceIds, section.imageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (section.type) {
|
switch (section.type) {
|
||||||
@ -311,14 +330,14 @@ namespace ManagerService.Controllers
|
|||||||
MapDTO mapDTO = JsonConvert.DeserializeObject<MapDTO>(section.data);
|
MapDTO mapDTO = JsonConvert.DeserializeObject<MapDTO>(section.data);
|
||||||
if (mapDTO.iconResourceId != null)
|
if (mapDTO.iconResourceId != null)
|
||||||
{
|
{
|
||||||
addResourceToList(resourceDTOs, mapDTO.iconResourceId);
|
addResourceIdToList(resourceIds, mapDTO.iconResourceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var point in mapDTO.points) {
|
foreach (var point in mapDTO.points) {
|
||||||
foreach (var image in point.images) {
|
foreach (var image in point.images) {
|
||||||
if (image.imageResourceId != null)
|
if (image.imageResourceId != null)
|
||||||
{
|
{
|
||||||
addResourceToList(resourceDTOs, image.imageResourceId);
|
addResourceIdToList(resourceIds, image.imageResourceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -330,7 +349,7 @@ namespace ManagerService.Controllers
|
|||||||
{
|
{
|
||||||
if (image.resourceId != null)
|
if (image.resourceId != null)
|
||||||
{
|
{
|
||||||
addResourceToList(resourceDTOs, image.resourceId);
|
addResourceIdToList(resourceIds, image.resourceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -340,35 +359,35 @@ namespace ManagerService.Controllers
|
|||||||
{
|
{
|
||||||
if (question.resourceId != null)
|
if (question.resourceId != null)
|
||||||
{
|
{
|
||||||
addResourceToList(resourceDTOs, question.resourceId);
|
addResourceIdToList(resourceIds, question.resourceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (quizzDTO.bad_level != null)
|
if (quizzDTO.bad_level != null)
|
||||||
{
|
{
|
||||||
if (quizzDTO.bad_level.resourceId != null)
|
if (quizzDTO.bad_level.resourceId != null)
|
||||||
{
|
{
|
||||||
addResourceToList(resourceDTOs, quizzDTO.bad_level.resourceId);
|
addResourceIdToList(resourceIds, quizzDTO.bad_level.resourceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (quizzDTO.medium_level != null)
|
if (quizzDTO.medium_level != null)
|
||||||
{
|
{
|
||||||
if (quizzDTO.medium_level.resourceId != null)
|
if (quizzDTO.medium_level.resourceId != null)
|
||||||
{
|
{
|
||||||
addResourceToList(resourceDTOs, quizzDTO.medium_level.resourceId);
|
addResourceIdToList(resourceIds, quizzDTO.medium_level.resourceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (quizzDTO.good_level != null)
|
if (quizzDTO.good_level != null)
|
||||||
{
|
{
|
||||||
if (quizzDTO.good_level.resourceId != null)
|
if (quizzDTO.good_level.resourceId != null)
|
||||||
{
|
{
|
||||||
addResourceToList(resourceDTOs, quizzDTO.good_level.resourceId);
|
addResourceIdToList(resourceIds, quizzDTO.good_level.resourceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (quizzDTO.great_level != null)
|
if (quizzDTO.great_level != null)
|
||||||
{
|
{
|
||||||
if (quizzDTO.great_level.resourceId != null)
|
if (quizzDTO.great_level.resourceId != null)
|
||||||
{
|
{
|
||||||
addResourceToList(resourceDTOs, quizzDTO.great_level.resourceId);
|
addResourceIdToList(resourceIds, quizzDTO.great_level.resourceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -378,7 +397,7 @@ namespace ManagerService.Controllers
|
|||||||
{
|
{
|
||||||
if (image.resourceId != null)
|
if (image.resourceId != null)
|
||||||
{
|
{
|
||||||
addResourceToList(resourceDTOs, image.resourceId);
|
addResourceIdToList(resourceIds, image.resourceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -388,7 +407,7 @@ namespace ManagerService.Controllers
|
|||||||
{
|
{
|
||||||
if (audio.value != null)
|
if (audio.value != null)
|
||||||
{
|
{
|
||||||
addResourceToList(resourceDTOs, audio.value);
|
addResourceIdToList(resourceIds, audio.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -399,15 +418,69 @@ namespace ManagerService.Controllers
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ExportConfigurationDTO toDownload = configuration.ToExportDTO(sectionDTOs, resourceDTOs);
|
|
||||||
string jsonString = JsonConvert.SerializeObject(toDownload);
|
var fileName = $"{configuration.Label.Trim().Replace(" ","_")}.json";
|
||||||
var fileName = $"{configuration.Label}.json";
|
string configFile = Path.Combine(configurationsDirectory, fileName);
|
||||||
var mimeType = "application/json";
|
|
||||||
var fileBytes = Encoding.UTF8.GetBytes(jsonString);
|
if (!System.IO.File.Exists(configFile))
|
||||||
return new FileContentResult(fileBytes, mimeType)
|
|
||||||
{
|
{
|
||||||
FileDownloadName = fileName
|
|
||||||
};
|
List<ResourceDTO> resourceDTOs = getResourceDTOFromIds(resourcesDirectory, resourceIds);
|
||||||
|
|
||||||
|
ExportConfigurationDTO toDownload = configuration.ToExportDTO(sectionDTOs, resourceDTOs);
|
||||||
|
string jsonString = JsonConvert.SerializeObject(toDownload);
|
||||||
|
|
||||||
|
var fileBytes = Encoding.UTF8.GetBytes(jsonString);
|
||||||
|
|
||||||
|
// Create the file.
|
||||||
|
createFile(configFile, fileBytes);
|
||||||
|
|
||||||
|
return new FileContentResult(fileBytes, mimeType)
|
||||||
|
{
|
||||||
|
FileDownloadName = fileName
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Get file from folder
|
||||||
|
byte[] readText = System.IO.File.ReadAllBytes(configFile);
|
||||||
|
|
||||||
|
string exportInString = Encoding.UTF8.GetString(readText);
|
||||||
|
|
||||||
|
ExportConfigurationDTO exportConfigurationFromFile = JsonConvert.DeserializeObject<ExportConfigurationDTO>(exportInString);
|
||||||
|
|
||||||
|
// Get all ids that are not in the existing file
|
||||||
|
List<string> resourceIdsToDownload = resourceIds.Where(r => !exportConfigurationFromFile.resources.Select(r => r.id).Distinct().ToList().Contains(r)).ToList();
|
||||||
|
|
||||||
|
List<ResourceDTO> resourceDTOs = getResourceDTOFromIds(resourcesDirectory, resourceIdsToDownload);
|
||||||
|
|
||||||
|
exportConfigurationFromFile.resources.AddRange(resourceDTOs);
|
||||||
|
|
||||||
|
//exportConfigurationFromFile.resources.AddRange(exportConfigurationFromFile.resources.Distinct());
|
||||||
|
ExportConfigurationDTO exportWithAll = configuration.ToExportDTO(sectionDTOs, exportConfigurationFromFile.resources);
|
||||||
|
string jsonStringWithAll = JsonConvert.SerializeObject(exportWithAll);
|
||||||
|
|
||||||
|
ExportConfigurationDTO toDownload = configuration.ToExportDTO(sectionDTOs, exportConfigurationFromFile.resources.Where(r => resourceIds.Contains(r.id)).ToList()); //Only download from resourceIds (only language ask (or not)
|
||||||
|
|
||||||
|
string jsonString = JsonConvert.SerializeObject(toDownload);
|
||||||
|
readText = Encoding.UTF8.GetBytes(jsonString);
|
||||||
|
|
||||||
|
// Check if difference
|
||||||
|
if (exportInString != jsonStringWithAll) {
|
||||||
|
// Delete file
|
||||||
|
System.IO.File.Delete(configFile);
|
||||||
|
|
||||||
|
var fileBytes = Encoding.UTF8.GetBytes(jsonStringWithAll);
|
||||||
|
|
||||||
|
// Recreate file with new content
|
||||||
|
createFile(configFile, fileBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new FileContentResult(readText, mimeType)
|
||||||
|
{
|
||||||
|
FileDownloadName = fileName
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (ArgumentNullException ex)
|
catch (ArgumentNullException ex)
|
||||||
{
|
{
|
||||||
@ -421,11 +494,62 @@ namespace ManagerService.Controllers
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine($"Error: {ex.Message}");
|
||||||
return null;
|
return null;
|
||||||
//return new ObjectResult(ex.Message) { StatusCode = 500 };
|
//return new ObjectResult(ex.Message) { StatusCode = 500 };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<ResourceDTO> getResourceDTOFromIds(string resourcesDirectory, List<string> resourceIds)
|
||||||
|
{
|
||||||
|
List<ResourceDTO> resourceDTOs = new List<ResourceDTO>();
|
||||||
|
|
||||||
|
foreach (var resourceId in resourceIds)
|
||||||
|
{
|
||||||
|
string resourceFile = Path.Combine(resourcesDirectory, resourceId)+".json";
|
||||||
|
if (System.IO.File.Exists(resourceFile))
|
||||||
|
{
|
||||||
|
// FILE EXIST, JUST GET IT FROM FILE
|
||||||
|
byte[] readText = System.IO.File.ReadAllBytes(resourceFile);
|
||||||
|
string resourceInString = Encoding.UTF8.GetString(readText);
|
||||||
|
ResourceDTO resourceDTO = JsonConvert.DeserializeObject<ResourceDTO>(resourceInString);
|
||||||
|
resourceDTOs.Add(resourceDTO);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// FILE DO NOT EXIST
|
||||||
|
Resource resource = _resourceService.GetById(resourceId);
|
||||||
|
ResourceData resourceData = _resourceDataService.GetByResourceId(resourceId);
|
||||||
|
|
||||||
|
if (resource != null && resourceData != null && !resourceDTOs.Any(r => r.id == resource.Id)) // Check if file already exist
|
||||||
|
{
|
||||||
|
resourceDTOs.Add(resource.ToDTO(resourceData.Data));
|
||||||
|
|
||||||
|
// Put resource in resources folder
|
||||||
|
string resourceJsonString = JsonConvert.SerializeObject(resource.ToDTO(resourceData.Data));
|
||||||
|
byte[] resourceBytes = Encoding.UTF8.GetBytes(resourceJsonString);
|
||||||
|
|
||||||
|
createFile(resourceFile, resourceBytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return resourceDTOs;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createFile(String fileName, byte[] fileBytes)
|
||||||
|
{
|
||||||
|
using (FileStream fs = System.IO.File.Create(fileName))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Try to create file at : {fileName}");
|
||||||
|
|
||||||
|
// Add some information to the file.
|
||||||
|
fs.Write(fileBytes, 0, fileBytes.Length);
|
||||||
|
|
||||||
|
Console.WriteLine($"Created file: {fileName}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Import a configuration
|
/// Import a configuration
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -633,16 +757,13 @@ namespace ManagerService.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<ResourceDTO> addResourceToList(List<ResourceDTO> resourceDTOs, string resourceId) {
|
private List<string> addResourceIdToList(List<string> resourceIds, string resourceId) {
|
||||||
if (!resourceDTOs.Select(r => r.id).Contains(resourceId)) {
|
if (!resourceIds.Contains(resourceId))
|
||||||
Resource resource = _resourceService.GetById(resourceId);
|
{
|
||||||
ResourceData resourceData = _resourceDataService.GetByResourceId(resourceId);
|
resourceIds.Add(resourceId);
|
||||||
|
|
||||||
if (resource != null && resourceData != null && !resourceDTOs.Any(r => r.id == resource.Id)) {
|
|
||||||
resourceDTOs.Add(resource.ToDTO(resourceData.Data));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return resourceDTOs;
|
|
||||||
|
return resourceIds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user