Skip to content

Commit

Permalink
Optimize file download operation handling
Browse files Browse the repository at this point in the history
Improve the file download operation to avoid unnecessary compression when the download involves a single file only. Introduce a check to determine if only a single file is being processed and bypass the compression step accordingly, streamlining the operation and reducing processing time.
  • Loading branch information
pavelbannov committed Nov 28, 2024
1 parent a88a582 commit e80aedd
Showing 1 changed file with 112 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,37 @@ public override async Task RunJob(DistributedTask distributedTask, CancellationT

var thirdPartyOperation = ThirdPartyOperation as FileDownloadOperation<string>;
var daoOperation = DaoOperation as FileDownloadOperation<int>;
await thirdPartyOperation.CompressToZipAsync(stream, scope);
await daoOperation.CompressToZipAsync(stream, scope);

if (stream != null)
{
string archiveExtension;

var thirdPartyFileOnly = thirdPartyOperation.Files.Count == 1 && thirdPartyOperation.Folders.Count == 0;
var daoFileOnly = daoOperation.Files.Count == 1 && daoOperation.Folders.Count == 0;
var compress = !((thirdPartyFileOnly || daoFileOnly) && (thirdPartyFileOnly != daoFileOnly));

string archiveExtension;

if (compress)
{
using (var zip = scope.ServiceProvider.GetService<CompressToArchive>())
{
archiveExtension = await zip.GetArchiveExtension();
}

await thirdPartyOperation.CompressToZipAsync(stream, scope);
await daoOperation.CompressToZipAsync(stream, scope);
}
else
{
if (thirdPartyFileOnly)
{
archiveExtension = await thirdPartyOperation.GetFileAsync(stream, scope);
}
else
{
archiveExtension = await daoOperation.GetFileAsync(stream, scope);
}
}

if (stream != null)
{
stream.Position = 0;
string fileName;

Expand All @@ -109,6 +128,10 @@ public override async Task RunJob(DistributedTask distributedTask, CancellationT
(await daoFactory.GetFolderDao<string>().GetFolderAsync(thirdPartyOperation.Folders[0])).Title :
(await daoFactory.GetFolderDao<int>().GetFolderAsync(daoOperation.Folders[0])).Title)}{archiveExtension}";
}
else if (!compress)
{
fileName = archiveExtension;
}
else
{
fileName = $@"{(await tenantManager.GetCurrentTenantAsync()).Alias.ToLower()}-{FileConstant.DownloadTitle}-{DateTime.UtcNow.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}{archiveExtension}";
Expand Down Expand Up @@ -373,7 +396,7 @@ internal async Task CompressToZipAsync(Stream stream, IServiceScope scope)
var fileConverter = scope.ServiceProvider.GetService<FileConverter>();
var fileDao = scope.ServiceProvider.GetService<IFileDao<T>>();

using var compressTo = scope.ServiceProvider.GetService<CompressToArchive>();
using ICompress compressTo = scope.ServiceProvider.GetService<CompressToArchive>();
await compressTo.SetStream(stream);

foreach (var path in _entriesPathId.AllKeys)
Expand Down Expand Up @@ -478,6 +501,88 @@ await fileConverter.ExecAsync(file, convertToExt) :
await ProgressStep();
}
}

internal async Task<string> GetFileAsync(Stream stream, IServiceScope scope)
{
if (_entriesPathId == null)
{
return null;
}

var fileConverter = scope.ServiceProvider.GetService<FileConverter>();
var fileDao = scope.ServiceProvider.GetService<IFileDao<T>>();

var path = _entriesPathId.AllKeys.FirstOrDefault();
if (string.IsNullOrEmpty(path))
{
await ProgressStep();
return null;
}

var entryId = _entriesPathId[path].FirstOrDefault();
if (CancellationToken.IsCancellationRequested)
{
CancellationToken.ThrowIfCancellationRequested();
}

var newTitle = path;

File<T> file = null;
var convertToExt = string.Empty;

if (!Equals(entryId, default(T)))
{
await fileDao.InvalidateCacheAsync(entryId);
file = await fileDao.GetFileAsync(entryId);

if (file == null)
{
this[Err] = FilesCommonResource.ErrorMessage_FileNotFound;
return null;
}

if (_files.TryGetValue(file.Id, out convertToExt) && !string.IsNullOrEmpty(convertToExt))
{
var sourceFileName = Path.GetFileName(path);
var targetFileName = FileUtility.ReplaceFileExtension(sourceFileName, convertToExt);
newTitle = path.Replace(sourceFileName, targetFileName);
}
}

if (!Equals(entryId, default(T)))
{
try
{
await using var readStream = await fileConverter.EnableConvertAsync(file, convertToExt, true) ?
await fileConverter.ExecAsync(file, convertToExt) :
await fileDao.GetFileStreamAsync(file);

await readStream.CopyToAsync(stream);
}
catch (Exception ex)
{
this[Err] = ex.Message;

Logger.ErrorWithException(ex);
}
}


if (!Equals(entryId, default(T)))
{
ProcessedFile(entryId);
}
else
{
ProcessedFolder(default);
}


await ProgressStep();


return newTitle;
}

private void ReplaceLongPath(ItemNameValueCollection<T> entriesPathId)
{
Expand Down

0 comments on commit e80aedd

Please sign in to comment.