<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;
using System.Runtime.InteropServices;
using System.Threading;
using log4net;

namespace KindleSender.Service
{
  public class FileSender : IFileSender
  {
    private static readonly ILog Log = LogManager.GetLogger(typeof(FolderWatcher));

    private readonly IConfiguration _configuration;

    private const short FileReadTries = 5;

    public FileSender(IConfiguration configuration)
    {
      if (configuration == null)
      {
        throw new ArgumentNullException("configuration");
      }

      _configuration = configuration;
    }

    public void Send(string filePath)
    {
      var fileName = filePath.Replace(_configuration.FolderPath, string.Empty);

      Log.Info(string.Format("Sending file {0} ... ", fileName));

      var tryOpenFileCounter = 0;

      do
      {
        if (IsFileLocked(filePath))
        {
          Log.Error("File is locked. Try counter: " + tryOpenFileCounter);
          Thread.Sleep(5000);
        }

        tryOpenFileCounter++;
      } while (tryOpenFileCounter > FileReadTries);

      try
      {
        SendEmail(filePath);

        Log.Info(string.Format("Sended file {0} ... ", fileName));
      }
      catch (Exception ex)
      {
        Log.Error(ex.Message);
      }
    }

    private void SendEmail(string filePath)
    {
      var mail = new MailMessage(_configuration.SmtpUserName, _configuration.KindleMail);
      mail.Attachments.Add(GetAtachment(filePath));

      var client = new SmtpClient
      {
        Port = _configuration.SmtpPort,
        UseDefaultCredentials = false,
        Host = _configuration.SmtpServer,
        Credentials = new System.Net.NetworkCredential(_configuration.SmtpUserName, _configuration.SmtpPassword),
        Timeout = 0
      };

      client.Send(mail);
    }

    private static Attachment GetAtachment(string filePath)
    {
      var attachment = new Attachment(filePath, MediaTypeNames.Application.Octet);

      var disposition = attachment.ContentDisposition;
      disposition.CreationDate = File.GetCreationTime(filePath);
      disposition.ModificationDate = File.GetLastWriteTime(filePath);
      disposition.ReadDate = File.GetLastAccessTime(filePath);
      disposition.FileName = Path.GetFileName(filePath);
      disposition.Size = new FileInfo(filePath).Length;
      disposition.DispositionType = DispositionTypeNames.Attachment;

      return attachment;
    }<commit_msg>:tada: Added support multiple devices<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;
using System.Runtime.InteropServices;
using System.Threading;
using log4net;

namespace KindleSender.Service
{
  public class FileSender : IFileSender
  {
    private static readonly ILog Log = LogManager.GetLogger(typeof(FolderWatcher));

    private readonly IConfiguration _configuration;

    private const short FileReadTries = 5;

    private const int SmtpClientTimeout = 0;

    public FileSender(IConfiguration configuration)
    {
      if (configuration == null)
      {
        throw new ArgumentNullException("configuration");
      }

      _configuration = configuration;
    }

    public void Send(string filePath)
    {
      var fileName = filePath.Replace(_configuration.FolderPath, string.Empty);

      Log.Info(string.Format("Sending file {0} ... ", fileName));

      var tryOpenFileCounter = 1;

      do
      {
        if (IsFileLocked(filePath))
        {
          Log.WarnFormat("File is locked. Try counter: {0}/{1}", tryOpenFileCounter, FileReadTries.ToString());
          Thread.Sleep(5000);
        }

        tryOpenFileCounter++;
      } while (tryOpenFileCounter > FileReadTries);

      try
      {
        SendEmail(filePath);

        Log.Info(string.Format("Sended file {0} ... ", fileName));
      }
      catch (Exception ex)
      {
        Log.Error(ex.Message);
      }
    }

    private void SendEmail(string filePath)
    {
      var mail = new MailMessage();
      mail.From = new MailAddress(_configuration.SmtpUserName);
      mail.To.Add(_configuration.KindleMail);
      mail.Attachments.Add(GetAtachment(filePath));

      var client = new SmtpClient
      {
        Port = _configuration.SmtpPort,
        UseDefaultCredentials = false,
        Host = _configuration.SmtpServer,
        Credentials = new System.Net.NetworkCredential(_configuration.SmtpUserName, _configuration.SmtpPassword),
        Timeout = SmtpClientTimeout
      };

      client.Send(mail);
    }

    private static Attachment GetAtachment(string filePath)
    {
      var attachment = new Attachment(filePath, MediaTypeNames.Application.Octet);

      var disposition = attachment.ContentDisposition;
      disposition.CreationDate = File.GetCreationTime(filePath);
      disposition.ModificationDate = File.GetLastWriteTime(filePath);
      disposition.ReadDate = File.GetLastAccessTime(filePath);
      disposition.FileName = Path.GetFileName(filePath);
      disposition.Size = new FileInfo(filePath).Length;
      disposition.DispositionType = DispositionTypeNames.Attachment;

      return attachment;
    }
{'fraction_non_alphanumeric': 0.09273807242766814, 'fraction_numerical': 0.00421536692853037, 'mean_word_length': 2.96958174904943, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5435758', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1422, 'n_words': 298}