Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

CloudflareThumbService.cs

using Nop.Core.Domain.Media;
using Nop.Core.Infrastructure;
using Nop.Data;
using Nop.Services.Media;

namespace Nop.Plugin.Misc.CloudflareImages.Services;

/// 
/// Picture service for Cloudflare Images
/// 
public partial class CloudflareThumbService : IThumbService
{
    #region Fields

    private readonly CloudflareImagesSettings _cloudflareImagesSettings;
    private readonly CloudflareImagesHttpClient _cloudflareImagesHttpClient;
    private readonly INopFileProvider _fileProvider;
    private readonly IRepository _cloudflareImagesRepository;

    #endregion

    #region Ctor

    public CloudflareThumbService(CloudflareImagesSettings cloudflareImagesSettings,
        CloudflareImagesHttpClient cloudflareImagesHttpClient,
        INopFileProvider fileProvider,
        IRepository cloudflareImagesRepository)
    {
        _cloudflareImagesSettings = cloudflareImagesSettings;
        _cloudflareImagesHttpClient = cloudflareImagesHttpClient;
        _fileProvider = fileProvider;
        _cloudflareImagesRepository = cloudflareImagesRepository;
    }

    #endregion

    #region Methods

    /// 
    /// Get a picture thumb local path
    /// 
    /// Picture URL
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the local picture thumb path
    /// 
    public async Task GetThumbLocalPathAsync(string pictureUrl)
    {
        if (string.IsNullOrEmpty(pictureUrl))
            return string.Empty;

        return await GetThumbLocalPathByFileNameAsync(_fileProvider.GetFileName(pictureUrl));
    }

    /// 
    /// Get a value indicating whether some file (thumb) already exists
    /// 
    /// Thumb file path
    /// Thumb file name
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the check result
    /// 
    public async Task GeneratedThumbExistsAsync(string thumbFilePath, string thumbFileName)
    {
        return await _cloudflareImagesRepository.Table.AnyAsync(i => i.ThumbFileName.Equals(thumbFileName));
    }

    /// 
    /// Save a picture thumb
    /// 
    /// Thumb file path
    /// Thumb file name
    /// MIME type
    /// Picture binary
    /// A task that represents the asynchronous operation
    public async Task SaveThumbAsync(string thumbFilePath, string thumbFileName, string mimeType, byte[] binary)
    {
        var dataContent = new MultipartFormDataContent
        {
            { new StreamContent(new MemoryStream(binary)), "file", thumbFileName }
        };

        var response = await _cloudflareImagesHttpClient.SaveThumbAsync(dataContent);

        if (response is not { Success: true })
            return;

        await _cloudflareImagesRepository.InsertAsync(new Domain.CloudflareImages
        {
            ImageId = response.Result.Id,
            ThumbFileName = thumbFileName
        }, false);
    }

    /// 
    /// Get picture (thumb) local path
    /// 
    /// Filename
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the local picture thumb path
    /// 
    public async Task GetThumbLocalPathByFileNameAsync(string thumbFileName)
    {
        var image = await _cloudflareImagesRepository.Table.FirstOrDefaultAsync(i => i.ThumbFileName.Equals(thumbFileName));

        if (image == null)
            return null;

        return _cloudflareImagesSettings.DeliveryUrl
            .Replace(CloudflareImagesDefaults.ImageIdPattern, image.ImageId)
            .Replace(CloudflareImagesDefaults.VariantNamePattern, "public");
    }

    /// 
    /// Get picture (thumb) URL 
    /// 
    /// Filename
    /// Store location URL; null to use determine the current store location automatically
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the local picture thumb path
    /// 
    public async Task GetThumbUrlAsync(string thumbFileName, string storeLocation = null)
    {
        return await GetThumbLocalPathByFileNameAsync(thumbFileName);
    }

    /// 
    /// Delete picture thumbs
    /// 
    /// Picture
    /// A task that represents the asynchronous operation
    public async Task DeletePictureThumbsAsync(Picture picture)
    {
        var items = await _cloudflareImagesRepository.Table
            .Where(i => i.ThumbFileName.Contains($"_{picture.SeoFilename}.") || i.ThumbFileName.Contains($"_{picture.SeoFilename}_"))
            .ToListAsync();

        foreach (var item in items)
            await _cloudflareImagesHttpClient.DeleteThumbAsync(item.ImageId);

        await _cloudflareImagesRepository.DeleteAsync(items, false);
    }

    #endregion
}