package lan.dk.podcastserver.service;


import lan.dk.podcastserver.entity.Cover;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * Created by kevin on 28/06/15 for Podcast Server
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ImageService {

    final UrlService urlService;

    public Cover getCoverFromURL(String url) {
        if (StringUtils.isEmpty(url))
            return null;

        try {
            return getCoverFromURL(new URL(url));
        } catch (IOException e) {
            log.error("Error during fetching Cover information for {}", url);
            return null;
        }
    }

    public Cover getCoverFromURL (URL url) throws IOException {
        Cover cover = new Cover(url.toString());

        try (InputStream urlInputStream = urlService.getConnectionWithTimeOut(cover.getUrl(), 5000).getInputStream() ){
            ImageInputStream imageInputStream = ImageIO.createImageInputStream(urlInputStream);
            final BufferedImage image = ImageIO.read(imageInputStream);
            cover.setWidth(image.getWidth());
            cover.setHeight(image.getHeight());
        }

        return cover;
    }
}

@@ -1107,36 +1107,8 @@
 over
- = new Cover(url.toString())
 ;%0A%0A 
@@ -1188,20 +1188,20 @@
 Out(
-cover.getUrl
+url.toString
 (), 
@@ -1415,13 +1415,119 @@
 over
-.setW
+ = Cover%0A                    .builder()%0A                        .url(url.toString())%0A                        .w
 idth
@@ -1544,18 +1544,28 @@
 Width())
-;
 %0A
+           
         
@@ -1572,18 +1572,11 @@
     
-cover.setH
+ .h
 eigh
@@ -1595,16 +1595,45 @@
 eight())
+%0A                    .build()
 ;%0A      

remove usage of wrong constructor