<commit_before>package org.mauritania.photosync.olympus.sync

import java.io.File

import org.mauritania.photosync.olympus.client.CameraClient
import org.slf4j.LoggerFactory

import scala.util.Failure

class FilesManager(
  api: CameraClient,
  outputDir: File
) {

  val logger = LoggerFactory.getLogger(this.getClass)

  private[sync] def isDownloaded(fileId: String, localFiles: Map[String, Long], remoteFiles: Map[String, Long]): Boolean = {
    val localSize = localFiles.get(fileId)
    val remoteSize = remoteFiles.get(fileId)
    localSize == remoteSize
  }

  def listLocalFiles(): Set[FileInfo] = {
    val files = outputDir.listFiles().toSet
    val filesAndSizes = files.map(file => FileInfo(file.getName, file.length()))
    filesAndSizes
  }

  def listRemoteFiles(): Set[FileInfo] = {
    val filesAndSizes = api.listFiles()
    filesAndSizes
  }

  def sync(): Set[File] = {
    def toMap(s: Set[FileInfo]) = s.flatMap(FileInfo.unapply).toMap
    val remoteFiles = listRemoteFiles()
    val localFiles = listLocalFiles()
    val remoteFilesMap = toMap(remoteFiles)
    val localFilesMap = toMap(localFiles)

    outputDir.mkdir() // it may exist already

    remoteFiles.flatMap {
      case fileInfo => handleFile(fileInfo, localFilesMap, remoteFilesMap)
    }
  }

  private def handleFile(
    fileInfo: FileInfo,
    localFilesMap: Map[String, Long],
    remoteFilesMap: Map[String, Long]
  ): Option[File] = {
    isDownloaded(fileInfo.name, localFilesMap, remoteFilesMap) match {
      case false => {
        logger.debug(s"Downloading file ${fileInfo}")
        val downloadedFile = api.downloadFile(fileInfo.name, outputDir)
        downloadedFile.recoverWith {
          case error =>
            logger.error(s"Exception downloading ${fileInfo}", error)
            Failure(error)
        }.toOption
      }
      case true => {
        logger.debug(s"Skipping file ${fileInfo} as it's been already downloaded")
        None
      }
    }
  }
}<commit_msg>Do some cosmetics on FilesManager.syncFile<commit_after>package org.mauritania.photosync.olympus.sync

import java.io.File

import org.mauritania.photosync.olympus.client.CameraClient
import org.slf4j.LoggerFactory

import scala.util.{Success, Failure}

class FilesManager(
  api: CameraClient,
  outputDir: File
) {

  val logger = LoggerFactory.getLogger(this.getClass)

  private[sync] def isDownloaded(fileId: String, localFiles: Map[String, Long], remoteFiles: Map[String, Long]): Boolean = {
    val localSize = localFiles.get(fileId)
    val remoteSize = remoteFiles.get(fileId)
    localSize == remoteSize
  }

  def listLocalFiles(): Set[FileInfo] = {
    val files = outputDir.listFiles().toSet
    val filesAndSizes = files.map(file => FileInfo(file.getName, file.length()))
    filesAndSizes
  }

  def listRemoteFiles(): Set[FileInfo] = {
    val filesAndSizes = api.listFiles()
    filesAndSizes
  }

  def sync(): Set[File] = {
    def toMap(s: Set[FileInfo]) = s.flatMap(FileInfo.unapply).toMap
    val remoteFiles = listRemoteFiles()
    val localFiles = listLocalFiles()
    val remoteFilesMap = toMap(remoteFiles)
    val localFilesMap = toMap(localFiles)

    outputDir.mkdir() // it may exist already

    remoteFiles.flatMap {
      case fileInfo => syncFile(fileInfo, localFilesMap, remoteFilesMap)
    }
  }

  private def syncFile(
    fileInfo: FileInfo,
    localFilesMap: Map[String, Long],
    remoteFilesMap: Map[String, Long]
  ): Option[File] = {
    isDownloaded(fileInfo.name, localFilesMap, remoteFilesMap) match {
      case false => {
        logger.debug(s"Downloading file ${fileInfo}")
        val downloadedFile = api.downloadFile(fileInfo.name, outputDir)
        downloadedFile match {
          case Success(file) =>
            Some(file)
          case Failure(error) =>
            logger.error(s"Exception downloading ${fileInfo}", error)
            None
        }
      }
      case true => {
        logger.debug(s"Skipping file ${fileInfo} as it's been already downloaded")
        None
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.10888393961890622, 'fraction_numerical': 0.0004949269982677555, 'mean_word_length': 3.3, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5833218', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1243, 'n_words': 272}