Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/main/java/de/telran/ImageProcessor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.telran;

import de.telran.entity.DownloadedImage;
import de.telran.entity.ActionableImage;
import de.telran.entity.ImageDescriptor;
import de.telran.factory.ImageActionFactory;
import de.telran.service.DownloadService;
Expand Down Expand Up @@ -32,15 +32,20 @@ public void doProcessing(String fileName) {

List<ImageDescriptor> imageDescriptors = imageDescriptorService.getImageDescriptors(fileName);

List<DownloadedImage> downloadedImages = downloadService.downloadImages(imageDescriptors);
List<ActionableImage> actionableImages = imageDescriptors
.stream()
.map(i -> new ActionableImage(null, false, i.getImageUrlName(), i.getActionName()))
.collect(Collectors.toList());

List<ActionableImage> downloadedImages = downloadService.downloadImages(actionableImages);

List<DownloadedImage> successfullyDownloadedImages = downloadedImages.stream()
.filter(DownloadedImage::isSuccessfull)
List<ActionableImage> successfullyDownloadedImages = downloadedImages.stream()
.filter(ActionableImage::isSuccessfull)
.collect(Collectors.toList());

List<BufferedImage> processedImages = successfullyDownloadedImages
List<ActionableImage> processedImages = successfullyDownloadedImages
.stream()
.map(i -> imageService.processImage(i.getImage(), i.getImageDescriptor().getActionName()))
.map(i -> imageService.processImage(i))
.collect(Collectors.toList());

processedImages.forEach(i -> fileService.saveImageAsFile(i));
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/de/telran/entity/ActionableImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.telran.entity;

import java.awt.image.BufferedImage;

public class ActionableImage {
private BufferedImage image;
private boolean isSuccessfull;
private String sourceUrl;
private String actionName;

public ActionableImage(BufferedImage image, boolean status, String sourceUrl, String actionName) {
this.image = image;
this.isSuccessfull = status;
this.sourceUrl = sourceUrl;
this.actionName = actionName;

}

public BufferedImage getImage() {
return image;
}

public boolean isSuccessfull() {
return isSuccessfull;
}

public String getSourceUrl() {
return sourceUrl;
}

public String getActionName() {
return actionName;
}

public void setImage(BufferedImage image) {
this.image = image;
}

public void setSuccessfull(boolean successfull) {
isSuccessfull = successfull;
}

public void setSourceUrl(String sourceUrl) {
this.sourceUrl = sourceUrl;
}

public void setActionName(String actionName) {
this.actionName = actionName;
}
}
27 changes: 0 additions & 27 deletions src/main/java/de/telran/entity/DownloadedImage.java

This file was deleted.

20 changes: 9 additions & 11 deletions src/main/java/de/telran/service/DownloadService.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package de.telran.service;

import de.telran.entity.DownloadedImage;
import de.telran.entity.ImageDescriptor;
import de.telran.entity.ActionableImage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class DownloadService {
public List<DownloadedImage> downloadImages(List<ImageDescriptor> imageDescriptors) {
List<DownloadedImage> imageList = new ArrayList<>();
for(ImageDescriptor descriptor:imageDescriptors) {
public List<ActionableImage> downloadImages(List<ActionableImage> images) {
List<ActionableImage> imageList = new ArrayList<>(images);
for(ActionableImage actionableImage:images) {
try {
URL url = new URL(descriptor.getImageUrlName());
URL url = new URL(actionableImage.getSourceUrl());
BufferedImage image = ImageIO.read(url);
imageList.add(new DownloadedImage(image, true, descriptor));
actionableImage.setImage(image);
actionableImage.setSuccessfull(true);
} catch (Exception ex) {
System.err.println(descriptor.getImageUrlName());
System.err.println(actionableImage.getSourceUrl());
System.err.println(ex.getMessage());
imageList.add(new DownloadedImage(null, false, descriptor));
actionableImage.setSuccessfull(false);
}
}
return imageList;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/de/telran/service/FileService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.telran.service;

import de.telran.entity.ActionableImage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
Expand All @@ -20,10 +22,10 @@ public List<String> loadStringsFromFile(String fileName) {
}
}

public void saveImageAsFile(BufferedImage image) {
public void saveImageAsFile(ActionableImage image) {

try {
ImageIO.write(image, "jpg",new File("/Users/slukichev/Downloads/images/img_"+image.hashCode()+".jpg"));
ImageIO.write(image.getImage(), "jpg",new File("/Users/slukichev/Downloads/images/img_"+image.hashCode()+".jpg"));
} catch (Exception ex) {
ex.printStackTrace();
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/de/telran/service/ImageService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.telran.service;

import de.telran.action.ImageAction;
import de.telran.entity.ActionableImage;
import de.telran.factory.ImageActionFactory;

import java.awt.image.BufferedImage;
Expand All @@ -13,12 +14,12 @@ public ImageService(ImageActionFactory imageActionFactory) {
this.imageActionFactory = imageActionFactory;
}

public BufferedImage processImage(BufferedImage image, String actionName) {
ImageAction imageAction = imageActionFactory.getImageAction(actionName);
public ActionableImage processImage(ActionableImage image) {
ImageAction imageAction = imageActionFactory.getImageAction(image.getActionName());
try {
return imageAction.doAction(image);
image.setImage(imageAction.doAction(image.getImage()));//better use copying constructor
} catch (Exception ex) {
System.out.println("Could not process image with action "+actionName+": "+ex.getMessage());
System.out.println("Could not process image with action "+image.getActionName()+": "+ex.getMessage());
}
return image;
}
Expand Down
11 changes: 4 additions & 7 deletions src/test/java/ImageProcessorTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import de.telran.ImageProcessor;
import de.telran.entity.DownloadedImage;
import de.telran.entity.ActionableImage;
import de.telran.entity.ImageDescriptor;
import de.telran.service.DownloadService;
import de.telran.service.FileService;
Expand All @@ -8,11 +8,8 @@
import org.junit.Before;
import org.junit.Test;

import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -49,10 +46,10 @@ public void testDoProcessing() {

}

private static List<DownloadedImage> createDownloadedImage() {
private static List<ActionableImage> createDownloadedImage() {
return Arrays.asList(
new DownloadedImage(null, true, new ImageDescriptor("http://server.com/image1.jpg", "PREVIEW")),
new DownloadedImage(null, true, new ImageDescriptor("http://server.com/image2.jpg", "THUMBNAIL"))
new ActionableImage(null, true, new ImageDescriptor("http://server.com/image1.jpg", "PREVIEW")),
new ActionableImage(null, true, new ImageDescriptor("http://server.com/image2.jpg", "THUMBNAIL"))
);
}

Expand Down