Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using JJMasterData.Core.DataManager;
using JJMasterData.Core.DataManager.IO;
using JJMasterData.Core.DataManager.IO.Storage;
using JJMasterData.Core.DataManager.Services;
using JJMasterData.Core.DataManager.Services.Abstractions;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -23,11 +24,11 @@ public static IServiceCollection AddDataManagerServices(this IServiceCollection
services.AddScoped<FieldValuesService>();
services.AddScoped<UploadAreaService>();
services.AddScoped<FormValuesService>();
services.AddScoped<IFileStorage, DiskFileStorage>();
services.AddScoped<ITemporaryUploadStore, TemporaryDiskUploadStore>();
services.AddScoped<FormFileManagerFactory>();
services.AddScoped<FormFileService>();
#if NET
services.AddScoped<ElementFileService>();
#endif
services.AddScoped<ElementMapService>();
services.AddScoped<UrlRedirectService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace JJMasterData.Core.DataDictionary.Services;

public class ElementImportService(IDataDictionaryRepository dataDictionaryRepository)
{
public async Task<bool> Import(MemoryStream file)
public async Task<bool> Import(Stream file)
{
file.Seek(0, SeekOrigin.Begin);

Expand All @@ -20,9 +20,8 @@ public async Task<bool> Import(MemoryStream file)

return true;
}

#if NET
public async Task ImportZipFile(MemoryStream ms)

public async Task ImportZipFile(Stream ms)
{
using var zip = new ZipArchive(ms, ZipArchiveMode.Read, leaveOpen: true);

Expand All @@ -45,5 +44,4 @@ private static IEnumerable<FormElement> GetFormElements(ZipArchive zip)
yield return formElement;
}
}
#endif
}
}
2 changes: 1 addition & 1 deletion src/MasterData.Core/DataManager/DataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,4 @@ public static void RemoveNullValues(Dictionary<string, object?>? values)
values.Remove(key);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
using System.Web;
using JJMasterData.Commons.Data.Entity.Repository;
using JJMasterData.Commons.Exceptions;
using JJMasterData.Commons.Security.Cryptography.Abstractions;
using JJMasterData.Commons.Tasks;
using JJMasterData.Commons.Tasks.Progress;
using JJMasterData.Commons.Util;
using JJMasterData.Core.Configuration.Options;
using JJMasterData.Core.DataDictionary.Models;
using JJMasterData.Core.DataManager.Exportation.Configuration;
using JJMasterData.Core.DataManager.Expressions;
using JJMasterData.Core.DataManager.IO;
using JJMasterData.Core.DataManager.IO.Storage;
using JJMasterData.Core.DataManager.Models;
using JJMasterData.Core.UI.Components;
using Microsoft.Extensions.Localization;
Expand All @@ -25,7 +24,6 @@
namespace JJMasterData.Core.DataManager.Exportation.Abstractions;

public abstract class DataExportationWriterBase(
IEncryptionService encryptionService,
ExpressionsService expressionsService,
IStringLocalizer<MasterDataResources> stringLocalizer,
IOptionsSnapshot<MasterDataCoreOptions> options,
Expand All @@ -35,27 +33,22 @@ public abstract class DataExportationWriterBase(
public event EventHandler<IProgressReporter> OnProgressChanged;

protected const int RecordsPerPage = 100000;


private List<FormElementField> _fields;

#region "Properties"

private DataExportationReporter _processReporter;
private List<FormElementField> _fields;
private FormFilePathBuilder _pathBuilder;

private IEncryptionService EncryptionService { get; } = encryptionService;
private ExpressionsService ExpressionsService { get; } = expressionsService;
protected IStringLocalizer<MasterDataResources> StringLocalizer { get; } = stringLocalizer;
private IOptionsSnapshot<MasterDataCoreOptions> Options { get; } = options;

private ILogger<DataExportationWriterBase> Logger { get; } = logger;
private FormFilePathBuilder PathBuilder => _pathBuilder ??= new FormFilePathBuilder(FormElement);

public string AbsoluteUri { get; internal set; }

private string GetFolderPath(FormElementField field, Dictionary<string, object> values)
{
return PathBuilder.GetFolderPath(field, values);
}
internal FileDownloaderFactory FileDownloaderFactory { get; set; }
internal IFileStorage FileStorage { get; set; }
internal string AbsoluteUri { get; set; }

protected List<FormElementField> VisibleFields
{
Expand Down Expand Up @@ -133,30 +126,12 @@ protected List<FormElementField> VisibleFields
/// <summary>
/// Path where the files are generated.
/// </summary>
public string FolderPath
public string FolderKey
{
get
{
var path = Options.Value.ExportationFolderPath;
string folderPath = DataExportationHelper.GetFolderPath(FormElement, path, UserId);

CreateFolderPathIfNotExits(folderPath);

return folderPath;
}
}

private static void CreateFolderPathIfNotExits(string folderPath)
{
try
{
if (folderPath != null && !Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
}
catch (Exception ex)
{
const string message = "Error on create directory, set a valid ExportationFolderPath on JJMasterData Options.";
throw new JJMasterDataException(message, ex);
return DataExportationHelper.GetFolderPath(FormElement, path, UserId);
}
}

Expand All @@ -178,14 +153,27 @@ public async Task RunWorkerAsync(CancellationToken token)

Reporter(ProcessReporter);

var filePath = Path.Combine(FolderPath, GetFilePath());
var fileName = GetFileName();
var tempFilePath = Path.GetTempFileName();

using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
try
{
await GenerateDocument(fs, token);
await using (var fs = new FileStream(tempFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 81920, true))
{
await GenerateDocument(fs, token);
}

await using var readStream = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 81920, true);
await FileStorage.SaveAsync(FolderKey, fileName, readStream, true, token);
}
finally
{
if (File.Exists(tempFilePath))
File.Delete(tempFilePath);
}

ProcessReporter.FilePath = filePath;
ProcessReporter.FolderKey = FolderKey;
ProcessReporter.FileName = fileName;

ProcessReporter.EndDate = DateTime.Now;
ProcessReporter.HasError = false;
Expand All @@ -201,14 +189,6 @@ public async Task RunWorkerAsync(CancellationToken token)
case ThreadAbortException:
ProcessReporter.Message = StringLocalizer["Process aborted by the user."];
break;
case IOException:
if (FileIO.IsFileLocked(FolderPath))
ProcessReporter.Message =
StringLocalizer[
"File is already being used by another process. Try downloading it from \"Recently generated files\"."];
else
goto default;
break;
case JJMasterDataException:
ProcessReporter.Message = ex.Message;
break;
Expand All @@ -219,8 +199,6 @@ public async Task RunWorkerAsync(CancellationToken token)
break;
}

if (File.Exists(FolderPath) && !FileIO.IsFileLocked(FolderPath))
File.Delete(FolderPath);
}
finally
{
Expand All @@ -236,7 +214,8 @@ protected void Reporter(DataExportationReporter processReporter)

public abstract Task GenerateDocument(Stream ms, CancellationToken token);

protected string GetFileLink(FormElementField field, Dictionary<string, object> row, string value)
protected string GetFileLink(FormElement formElement, FormElementField field, Dictionary<string, object> row,
string value)
{
if (!field.DataFile!.ExportAsLink)
return null;
Expand All @@ -248,11 +227,16 @@ protected string GetFileLink(FormElementField field, Dictionary<string, object>
if (files.Length != 1)
return null;

var filePath = GetFolderPath(field, row) + value;
return JJFileDownloader.GetExternalDownloadLink(EncryptionService, AbsoluteUri, filePath);
var fileName = Path.GetFileName(files[0]);
if (string.IsNullOrEmpty(fileName))
return null;

var downloader = FileDownloaderFactory.Create(formElement, field, row, fileName);

return new Uri(new Uri(AbsoluteUri), downloader.GetDownloadUrl(AbsoluteUri)).AbsoluteUri;
}

private string GetFilePath()
private string GetFileName()
{
string fileName;
var exportActionFileName = FormElement.Options.GridToolbarActions.ExportAction.FileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace JJMasterData.Core.DataManager.Exportation;

internal static class DataExportationHelper
public static class DataExportationHelper
{
/// <summary>
/// Path where the files are generated.
Expand All @@ -28,4 +28,4 @@ public static string GetFolderPath(JJDataExportation dataExportation)
var path = dataExportation.MasterDataOptions.ExportationFolderPath;
return GetFolderPath(dataExportation.FormElement , path, dataExportation.UserId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@ namespace JJMasterData.Core.DataManager.Exportation;

public class DataExportationReporter : ProgressReporter
{
private int _totalProcessed;
public int TotalProcessed
{
get => _totalProcessed;
get;
set
{
_totalProcessed = value;
field = value;
UpdatePercentage();
}
}

private int _totalOfRecords;
public int TotalOfRecords
{
get => _totalOfRecords;
get;
set
{
_totalOfRecords = value;
field = value;
UpdatePercentage();
}
}

public string FilePath { get; set; }
public string FolderKey { get; set; }
public string FileName { get; set; }

private void UpdatePercentage()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using JJMasterData.Commons.Tasks;
using JJMasterData.Core.DataManager.Exportation.Abstractions;
using JJMasterData.Core.DataManager.Exportation.Configuration;
using JJMasterData.Core.DataManager.IO.Storage;
using JJMasterData.Core.UI.Components;
using JJMasterData.Core.UI.Events.Args;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -81,12 +82,14 @@ public DataExportationWriterBase GetInstance(JJDataExportation dataExportation)
return writer;
}

private static void ConfigureWriter(JJDataExportation dataExportation, DataExportationWriterBase writer)
private void ConfigureWriter(JJDataExportation dataExportation, DataExportationWriterBase writer)
{
writer.FormElement = dataExportation.FormElement;
writer.Configuration = dataExportation.ExportOptions;
writer.UserId = dataExportation.UserId;
writer.ProcessOptions = dataExportation.ProcessOptions;
writer.FileDownloaderFactory = serviceProvider.GetRequiredService<FileDownloaderFactory>();
writer.FileStorage = serviceProvider.GetRequiredService<IFileStorage>();
writer.AbsoluteUri = dataExportation.CurrentContext.HttpContext!.Request.GetAbsoluteUri();
}

Expand Down
10 changes: 3 additions & 7 deletions src/MasterData.Core/DataManager/Exportation/ExcelWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using JJMasterData.Commons.Data.Entity.Models;
using JJMasterData.Commons.Data.Entity.Repository;
using JJMasterData.Commons.Data.Entity.Repository.Abstractions;
using JJMasterData.Commons.Security.Cryptography.Abstractions;
using JJMasterData.Commons.Tasks;
using JJMasterData.Core.Configuration.Options;
using JJMasterData.Core.DataDictionary.Models;
Expand All @@ -27,14 +26,11 @@ namespace JJMasterData.Core.DataManager.Exportation;
public class ExcelWriter(
ExpressionsService expressionsService,
DataItemService dataItemService,
IEncryptionService encryptionService,
IStringLocalizer<MasterDataResources> stringLocalizer,
IOptionsSnapshot<MasterDataCoreOptions> options,
ILoggerFactory loggerFactory,
IEntityRepository entityRepository)
: DataExportationWriterBase(
encryptionService,
expressionsService,
: DataExportationWriterBase(expressionsService,
stringLocalizer,
options,
loggerFactory.CreateLogger<DataExportationWriterBase>()), IExcelWriter
Expand Down Expand Up @@ -175,7 +171,7 @@ private async ValueTask<string> CreateCell(FormElementField field, Dictionary<st

if (field.Component == FormComponent.File)
{
string link = GetFileLink(field, row, value);
string link = GetFileLink(FormElement, field, row, value);
if (link != null)
value = $"<a href=\"{link}\">{value}</a>";
else
Expand Down Expand Up @@ -226,4 +222,4 @@ private async Task GenerateHeader(StreamWriter sw)

await sw.WriteLineAsync("\t\t\t</tr>");
}
}
}
8 changes: 2 additions & 6 deletions src/MasterData.Core/DataManager/Exportation/TextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using JJMasterData.Commons.Data.Entity.Models;
using JJMasterData.Commons.Data.Entity.Repository;
using JJMasterData.Commons.Data.Entity.Repository.Abstractions;
using JJMasterData.Commons.Security.Cryptography.Abstractions;
using JJMasterData.Commons.Tasks;
using JJMasterData.Core.Configuration.Options;
using JJMasterData.Core.DataManager.Exportation.Abstractions;
Expand All @@ -21,15 +20,12 @@
namespace JJMasterData.Core.DataManager.Exportation;

public class TextWriter(
IEncryptionService encryptionService,
ExpressionsService expressionsService,
ExpressionsService expressionsService,
IStringLocalizer<MasterDataResources> stringLocalizer,
IOptionsSnapshot<MasterDataCoreOptions> options,
ILoggerFactory logger,
IEntityRepository entityRepository)
: DataExportationWriterBase(
encryptionService,
expressionsService,
: DataExportationWriterBase(expressionsService,
stringLocalizer,
options,
logger.CreateLogger<DataExportationWriterBase>()), ITextWriter
Expand Down
5 changes: 3 additions & 2 deletions src/MasterData.Core/DataManager/IO/FormFileContent.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.IO;

namespace JJMasterData.Core.DataManager.IO;

public class FormFileContent
{
public string FileName { get; set; }
public byte[] Bytes { get; init; }
public Stream Stream { get; set; }
public long Length { get; set;}
public DateTime LastWriteTime { get; set; }
}
}
Loading
Loading