<commit_before>package com.github._1element.sc.domain; //NOSONAR

import com.github._1element.sc.events.RemoteCopyEvent;
import com.github._1element.sc.exception.FtpRemoteCopyException;
import com.github._1element.sc.properties.FtpRemoteCopyProperties;
import com.github._1element.sc.service.FileService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Map;
import java.util.TreeMap;

/**
 * Copy surveillance image to ftp remote server (backup).
 */
@ConditionalOnProperty(name="sc.remotecopy.ftp.enabled", havingValue="true")
@Component
@Scope("prototype")
public class FtpRemoteCopy implements RemoteCopy {

  private FileService fileService;

  private FtpRemoteCopyProperties ftpRemoteCopyProperties;

  private FTPClient ftp;

  private long sizeRemoved = 0;

  private long filesRemoved = 0;

  private long totalSize = 0;

  private static final String CRON_EVERY_DAY_AT_5_AM = "0 0 5 * * *";

  private static final Logger LOG = LoggerFactory.getLogger(FtpRemoteCopy.class);

  @Autowired
  public FtpRemoteCopy(FtpRemoteCopyProperties ftpRemoteCopyProperties, FTPClient ftp, FileService fileService) {
    this.ftpRemoteCopyProperties = ftpRemoteCopyProperties;
    this.ftp = ftp;
    this.fileService = fileService;
  }

  @Override
  public void handle(RemoteCopyEvent remoteCopyEvent) {
    LOG.debug("Ftp remote copy handler for '{}' invoked.", remoteCopyEvent.getFileName());

    try {
      connect();
      transferFile(remoteCopyEvent.getFileName());
    } catch (Exception e) {
      LOG.warn("Error during remote ftp copy: {}", e.getMessage());
    } finally {
      disconnect();
    }
  }

  @Override
  @Scheduled(cron=CRON_EVERY_DAY_AT_5_AM)
  public void cleanup() {
    if (!ftpRemoteCopyProperties.isCleanupEnabled()) {
      return;
    }

    try {
      connect();
      removeOldFiles();
    } catch (Exception e) {
      LOG.warn("Error during cleanup remote ftp images: {}", e.getMessage());
    } finally {
      disconnect();
    }
  }

  /**
   * Connect to ftp server.
   *<commit_msg>Log message added if task is disabled in configuration<commit_after>package com.github._1element.sc.domain; //NOSONAR

import com.github._1element.sc.events.RemoteCopyEvent;
import com.github._1element.sc.exception.FtpRemoteCopyException;
import com.github._1element.sc.properties.FtpRemoteCopyProperties;
import com.github._1element.sc.service.FileService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Map;
import java.util.TreeMap;

/**
 * Copy surveillance image to ftp remote server (backup).
 */
@ConditionalOnProperty(name="sc.remotecopy.ftp.enabled", havingValue="true")
@Component
@Scope("prototype")
public class FtpRemoteCopy implements RemoteCopy {

  private FileService fileService;

  private FtpRemoteCopyProperties ftpRemoteCopyProperties;

  private FTPClient ftp;

  private long sizeRemoved = 0;

  private long filesRemoved = 0;

  private long totalSize = 0;

  private static final String CRON_EVERY_DAY_AT_5_AM = "0 0 5 * * *";

  private static final Logger LOG = LoggerFactory.getLogger(FtpRemoteCopy.class);

  @Autowired
  public FtpRemoteCopy(FtpRemoteCopyProperties ftpRemoteCopyProperties, FTPClient ftp, FileService fileService) {
    this.ftpRemoteCopyProperties = ftpRemoteCopyProperties;
    this.ftp = ftp;
    this.fileService = fileService;
  }

  @Override
  public void handle(RemoteCopyEvent remoteCopyEvent) {
    LOG.debug("Ftp remote copy handler for '{}' invoked.", remoteCopyEvent.getFileName());

    try {
      connect();
      transferFile(remoteCopyEvent.getFileName());
    } catch (Exception e) {
      LOG.warn("Error during remote ftp copy: {}", e.getMessage());
    } finally {
      disconnect();
    }
  }

  @Override
  @Scheduled(cron=CRON_EVERY_DAY_AT_5_AM)
  public void cleanup() {
    if (!ftpRemoteCopyProperties.isCleanupEnabled()) {
      LOG.info("Ftp remote copy cleanup task is disabled in configuration.");
      return;
    }

    try {
      connect();
      removeOldFiles();
    } catch (Exception e) {
      LOG.warn("Error during cleanup remote ftp images: {}", e.getMessage());
    } finally {
      disconnect();
    }
  }

  /**
   * Connect to ftp server.
   *
{'fraction_non_alphanumeric': 0.10634557918790649, 'fraction_numerical': 0.005273334505185446, 'mean_word_length': 4.502901353965184, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3139271', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1701, 'n_words': 365}