<commit_before>package lan.dk.podcastserver.business;

import com.mashape.unirest.http.HttpResponse;
import javaslang.collection.List;
import javaslang.control.Try;
import lan.dk.podcastserver.entity.Cover;
import lan.dk.podcastserver.entity.Item;
import lan.dk.podcastserver.entity.Podcast;
import lan.dk.podcastserver.repository.CoverRepository;
import lan.dk.podcastserver.service.UrlService;
import lan.dk.podcastserver.service.properties.PodcastServerParameters;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.UUID;

import static java.util.Objects.isNull;

/**
 * Created by kevin on 08/06/2014 for Podcast-Server
 */
@Slf4j
@Component
@Transactional
@RequiredArgsConstructor
public class CoverBusiness {

    private static final String API_PODCAST_COVER = "/api/podcast/%s/cover.%s";

    final CoverRepository coverRepository;
    final PodcastServerParameters podcastServerParameters;
    final UrlService urlService;

    public Cover findOne(UUID id) {
        return coverRepository.findOne(id);
    }

    public String download(Podcast podcast) {

        if (podcast.getCover() == null || StringUtils.isEmpty(podcast.getCover().getUrl())) {
            return StringUtils.EMPTY;
        }

        if (podcast.getCover().getUrl().startsWith("/"))
            return podcast.getCover().getUrl();

        String coverUrl = podcast.getCover().getUrl();

        String fileName = podcastServerParameters.getCoverDefaultName() + "." + FilenameUtils.getExtension(coverUrl);

        Path fileLocation = podcastServerParameters.getRootfolder().resolve(podcast.getTitle()).resolve(fileName);

        // Download file to correct location :
        try {

            if (!Files.exists(fileLocation.getParent())) {
                Files.createDirectories(fileLocation.getParent());
            }

            HttpResponse<InputStream> request = Try.of(() -> urlService.get(coverUrl).asBinary())
                    .filter(this::isImage)
                    .getOrElseThrow(() -> new IOException("Not an image in content type"));

            Files.copy(
                    request.getBody(),
                    fileLocation,
                    StandardCopyOption.REPLACE_EXISTING
            );
            return String.format(API_PODCAST_COVER, podcast.getId().toString(), FilenameUtils.getExtension(coverUrl));
        } catch (IOException e) {
            log.error("Error during downloading of the cover", e);
            return "";
        }
    }

    private Boolean isImage(HttpResponse<InputStream> request) {
        return List.ofAll(request.getHeaders().get("Content-Type"))
                .find(h -> h.contains("image"))
                .isDefined();
    }

    public Boolean download(Item item) {
        if (isNull(item.getPodcast()) || isNull(item.getId())){
            log.error("Podcast or ID of item should not be null for element with title {}", item.getTitle());
            return false;
        }

        if (isNull(item.getCover()) || Cover.DEFAULT_COVER.equals(item.getCover()) || StringUtils.isEmpty(item.getCover().getUrl())) {
            log.error("Cover null or empty for item of id {}", item.getId());
            return false;
        }

        String coverUrl = item.getCover().getUrl();
        String fileName = item.getId() + "." + FilenameUtils.getExtension(coverUrl);
        Path fileLocation = podcastServerParameters.getRootfolder().resolve(item.getPodcast().getTitle()).resolve(fileName);

        try {
            if (!Files.exists(fileLocation.getParent())) {
                Files.createDirectories(fileLocation.getParent());
            }

            HttpResponse<InputStream> request = Try.of(() -> urlService.get(coverUrl).asBinary())
                    .filter(this::isImage)
                    .getOrElseThrow(() -> new IOException("Not an image in content type"));

            Files.copy(
                    request.getBody(),
                    fileLocation,
                    StandardCopyOption.REPLACE_EXISTING
            );
            return true;
        } catch (IOException e) {
            log.error("Error during downloading of the cover", e);
            return false;
        }

    }

    Boolean hasSameCoverURL(Podcast patchPodcast, Podcast podcastToUpdate) {
        return !isNull(patchPodcast.getCover()) && !isNull(podcastToUpdate.getCover()) &&
                patchPodcast.getCover().equals(podcastToUpdate.getCover());
    }

    Path getCoverPathOf(Podcast podcast) {
        String fileName = podcastServerParameters.getCoverDefaultName() + "." + FilenameUtils.getExtension(podcast.getCover().getUrl());
        return podcastServerParameters.getRootfolder().resolve(podcast.getTitle()).resolve(fileName);
    }

    public Path getCoverPathOf(Item i) {
        String fileName = i.getId() + "." + FilenameUtils.getExtension(i.getCover().getUrl());
        return podcastServerParameters.getRootfolder().resolve(i.getPodcast().getTitle()).resolve(fileName);
    }

    public Cover save(Cover cover) {
        return coverRepository.save(cover);
    }


}<commit_msg>refactor(cover): use Try monad and remove duplication of code<commit_after>package lan.dk.podcastserver.business;

import com.mashape.unirest.http.HttpResponse;
import javaslang.collection.List;
import javaslang.control.Try;
import lan.dk.podcastserver.entity.Cover;
import lan.dk.podcastserver.entity.Item;
import lan.dk.podcastserver.entity.Podcast;
import lan.dk.podcastserver.repository.CoverRepository;
import lan.dk.podcastserver.service.UrlService;
import lan.dk.podcastserver.service.properties.PodcastServerParameters;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.UUID;

import static java.util.Objects.isNull;

/**
 * Created by kevin on 08/06/2014 for Podcast-Server
 */
@Slf4j
@Component
@Transactional
@RequiredArgsConstructor
public class CoverBusiness {

    private static final String API_PODCAST_COVER = "/api/podcast/%s/cover.%s";

    final CoverRepository coverRepository;
    final PodcastServerParameters podcastServerParameters;
    final UrlService urlService;

    public Cover findOne(UUID id) {
        return coverRepository.findOne(id);
    }

    public String download(Podcast podcast) {

        if (podcast.getCover() == null || StringUtils.isEmpty(podcast.getCover().getUrl())) {
            return StringUtils.EMPTY;
        }

        if (podcast.getCover().getUrl().startsWith("/"))
            return podcast.getCover().getUrl();

        String coverUrl = podcast.getCover().getUrl();
        String fileName = podcastServerParameters.getCoverDefaultName() + "." + FilenameUtils.getExtension(coverUrl);
        Path fileLocation = podcastServerParameters.getRootfolder().resolve(podcast.getTitle()).resolve(fileName);

        // Download file to correct location :
        return Try.run(() -> createParentDirectory(fileLocation))
            .mapTry(v -> imageRequest(coverUrl))
            .andThenTry(saveToDisk(fileLocation))
            .map(v -> String.format(API_PODCAST_COVER, podcast.getId().toString(), FilenameUtils.getExtension(coverUrl)))
            .onFailure(e -> log.error("Error during downloading of the cover", e))
            .getOrElse("");
    }

    public Boolean download(Item item) {
        if (isNull(item.getPodcast()) || isNull(item.getId())){
            log.error("Podcast or ID of item should not be null for element with title {}", item.getTitle());
            return false;
        }

        if (isNull(item.getCover()) || Cover.DEFAULT_COVER.equals(item.getCover()) || StringUtils.isEmpty(item.getCover().getUrl())) {
            log.error("Cover null or empty for item of id {}", item.getId());
            return false;
        }

        String coverUrl = item.getCover().getUrl();
        String fileName = item.getId() + "." + FilenameUtils.getExtension(coverUrl);
        Path fileLocation = podcastServerParameters.getRootfolder().resolve(item.getPodcast().getTitle()).resolve(fileName);

        return Try.run(() -> createParentDirectory(fileLocation))
                .mapTry(v -> imageRequest(coverUrl))
                .andThenTry(saveToDisk(fileLocation))
                .map(v -> Boolean.TRUE)
                .onFailure(e -> log.error("Error during downloading of the cover", e))
                .getOrElse(Boolean.FALSE);
    }

    Boolean hasSameCoverURL(Podcast patchPodcast, Podcast podcastToUpdate) {
        return !isNull(patchPodcast.getCover()) && !isNull(podcastToUpdate.getCover()) &&
                patchPodcast.getCover().equals(podcastToUpdate.getCover());
    }

    Path getCoverPathOf(Podcast podcast) {
        String fileName = podcastServerParameters.getCoverDefaultName() + "." + FilenameUtils.getExtension(podcast.getCover().getUrl());
        return podcastServerParameters.getRootfolder().resolve(podcast.getTitle()).resolve(fileName);
    }

    public Path getCoverPathOf(Item i) {
        String fileName = i.getId() + "." + FilenameUtils.getExtension(i.getCover().getUrl());
        return podcastServerParameters.getRootfolder().resolve(i.getPodcast().getTitle()).resolve(fileName);
    }

    public Cover save(Cover cover) {
        return coverRepository.save(cover);
    }

    private Try.CheckedConsumer<HttpResponse<InputStream>> saveToDisk(Path fileLocation) {
        return r -> Files.copy(
                r.getBody(),
                fileLocation,
                StandardCopyOption.REPLACE_EXISTING );
    }

    private HttpResponse<InputStream> imageRequest(String coverUrl) throws IOException {
        return Try.of(() -> urlService.get(coverUrl).asBinary())
                .filter(this::isImage)
                .getOrElseThrow(() -> new IOException("Not an image in content type"));
    }

    private Boolean isImage(HttpResponse<InputStream> request) {
        return List.ofAll(request.getHeaders().get("Content-Type"))
                .find(h -> h.contains("image"))
                .isDefined();
    }

    private void createParentDirectory(Path fileLocation) throws IOException {
        if (!Files.exists(fileLocation.getParent())) {
            Files.createDirectories(fileLocation.getParent());
        }
    }

}
{'fraction_non_alphanumeric': 0.11977359868541172, 'fraction_numerical': 0.0021909804637575314, 'mean_word_length': 3.046915404506834, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1464636', 'n_tokens_mistral': 3028, 'n_tokens_neox': 2939, 'n_words': 558}