| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace Services.EmailRoutingServices; |
| | | 6 | | |
| | | 7 | | public class FileDeduplicationService : IFileDeduplicationService |
| | | 8 | | { |
| | | 9 | | private readonly ApplicationDbContext _context; |
| | | 10 | | |
| | 9 | 11 | | public FileDeduplicationService(ApplicationDbContext context) |
| | | 12 | | { |
| | 9 | 13 | | _context = context; |
| | 9 | 14 | | } |
| | | 15 | | |
| | | 16 | | public string ComputeSha256(byte[] bytes) |
| | | 17 | | { |
| | 8 | 18 | | using var sha = SHA256.Create(); |
| | 8 | 19 | | var hash = sha.ComputeHash(bytes); |
| | 8 | 20 | | return Convert.ToHexString(hash).ToLowerInvariant(); |
| | 8 | 21 | | } |
| | | 22 | | |
| | | 23 | | public Task<bool> IsDuplicateAsync(string messageExternalId, string hash, string fileName, long fileSizeBytes) |
| | | 24 | | { |
| | 5 | 25 | | return _context.EmailAttachmentInboxes |
| | 5 | 26 | | .Include(x => x.EmailMessage) |
| | 5 | 27 | | .AnyAsync(x => |
| | 5 | 28 | | x.EmailMessage != null && |
| | 5 | 29 | | x.EmailMessage.MessageExternalId == messageExternalId && |
| | 5 | 30 | | x.Sha256Hash == hash && |
| | 5 | 31 | | x.FileName == fileName && |
| | 5 | 32 | | x.FileSizeBytes == fileSizeBytes); |
| | | 33 | | } |
| | | 34 | | } |