aboutsummaryrefslogtreecommitdiffstats
path: root/src/Services/AssetsService.cs
blob: bf0029ac734fa4d2dbb24804e17f01f5fcde296c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using IOL.Helpers;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using VSH.Data;
using VSH.Data.Static;

namespace VSH.Services;

public class AssetsService
{
	private readonly MainDbContext _context;
	private readonly ILogger<AssetsService> _logger;


	public AssetsService(MainDbContext context, ILogger<AssetsService> logger) {
		_context = context;
		_logger = logger;
	}

	public Task RemoveUnusedProductImages() {
		try {
			_logger.LogDebug("Starting RemoveUnusedProductImages");
			var inUseFileNames = new List<string>();

			foreach (var productImageList in _context.Products.Select(c => c.Images).AsNoTracking()) {
				var defaultFiles = productImageList.Select(image => image.FileName).ToList();
				var smallFiles = defaultFiles.Select(c => c.ExtractFileName() + "-300" + c.ExtractExtension());
				var miniFiles = defaultFiles.Select(c => c.ExtractFileName() + "-150" + c.ExtractExtension());
				inUseFileNames.AddRange(defaultFiles);
				inUseFileNames.AddRange(miniFiles);
				inUseFileNames.AddRange(smallFiles);
			}

			var removedFileCount = 0;
			if (inUseFileNames.Any()) {
				foreach (var diskFile in Directory.EnumerateFiles(AppPaths.ProductImages.HostPath)) {
					if (inUseFileNames.Any(c => c == diskFile))
						continue;
					if (File.Exists(diskFile))
						File.Delete(diskFile);
					removedFileCount++;
					_logger.LogDebug("Deleted " + diskFile);
				}
			}

			_logger.LogInformation("Removed " + removedFileCount + " unused product images");
			return Task.CompletedTask;
		} catch (Exception ex) {
			_logger.LogError("Exception removing unused assets: {exception}", ex.Message);
			return Task.CompletedTask;
		}
	}
}