using Microsoft.AspNetCore.Http; using Nop.Core.Domain.Media; using Nop.Data; namespace Nop.Services.Media; /// /// Download service /// public partial class DownloadService : IDownloadService { #region Fields protected readonly IRepository _downloadRepository; #endregion #region Ctor public DownloadService(IRepository downloadRepository) { _downloadRepository = downloadRepository; } #endregion #region Methods /// /// Gets a download /// /// Download identifier /// /// A task that represents the asynchronous operation /// The task result contains the download /// public virtual async Task GetDownloadByIdAsync(int downloadId) { return await _downloadRepository.GetByIdAsync(downloadId); } /// /// Gets a download by GUID /// /// Download GUID /// /// A task that represents the asynchronous operation /// The task result contains the download /// public virtual async Task GetDownloadByGuidAsync(Guid downloadGuid) { if (downloadGuid == Guid.Empty) return null; var query = from o in _downloadRepository.Table where o.DownloadGuid == downloadGuid select o; return await query.FirstOrDefaultAsync(); } /// /// Deletes a download /// /// Download /// A task that represents the asynchronous operation public virtual async Task DeleteDownloadAsync(Download download) { await _downloadRepository.DeleteAsync(download); } /// /// Inserts a download /// /// Download /// A task that represents the asynchronous operation public virtual async Task InsertDownloadAsync(Download download) { await _downloadRepository.InsertAsync(download); } /// /// Gets the download binary array /// /// File /// /// A task that represents the asynchronous operation /// The task result contains the download binary array /// public virtual async Task GetDownloadBitsAsync(IFormFile file) { await using var fileStream = file.OpenReadStream(); await using var ms = new MemoryStream(); await fileStream.CopyToAsync(ms); var fileBytes = ms.ToArray(); return fileBytes; } #endregion }