|
| 1 | +package de.telran.processor; |
| 2 | + |
| 3 | +import de.telran.processor.entity.DownloadedImage; |
| 4 | +import de.telran.processor.entity.ImageDescriptor; |
| 5 | +import de.telran.processor.factory.ImageActionFactory; |
| 6 | +import de.telran.processor.service.*; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import java.util.stream.Collectors; |
| 10 | + |
| 11 | +public class ImageProcessor { |
| 12 | + |
| 13 | + private FileService fileService; |
| 14 | + private DownloadService downloadService; |
| 15 | + private ImageService imageService; |
| 16 | + |
| 17 | + public static void main(String[] args) throws Exception { |
| 18 | + |
| 19 | + String csvFile = args[0]; |
| 20 | + |
| 21 | + FileService fs = new FileService(new FileConfigService(), new FileNameGenertorService()); |
| 22 | + DownloadService ds = new DownloadService(new ImageReadingService()); |
| 23 | + ImageService is = new ImageService(new ImageActionFactory(new ActionsConfigService())); |
| 24 | + ImageProcessor processor = new ImageProcessor(fs, ds, is); |
| 25 | + processor.process(csvFile); |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + public ImageProcessor(FileService fileService, |
| 30 | + DownloadService downloadService, |
| 31 | + ImageService imageService) { |
| 32 | + this.fileService = fileService; |
| 33 | + this.downloadService = downloadService; |
| 34 | + this.imageService = imageService; |
| 35 | + } |
| 36 | + |
| 37 | + public void process(String fileName){ |
| 38 | + //main logic is here |
| 39 | + |
| 40 | + //reading CSV file to get image data like urls and actions |
| 41 | + List<ImageDescriptor> imageDescriptors = fileService.readImageDescriptors(fileName); |
| 42 | + |
| 43 | + //download images |
| 44 | + List<DownloadedImage> downloadedImages = downloadService.downloadImages(imageDescriptors); |
| 45 | + |
| 46 | + //filter successfully downloaded images |
| 47 | + List<DownloadedImage> successfullyDownloadedImages = downloadedImages |
| 48 | + .stream() |
| 49 | + .filter(DownloadedImage::isSuccessful) |
| 50 | + .collect(Collectors.toList()); |
| 51 | + |
| 52 | + |
| 53 | + //apply action to every downloaded image |
| 54 | + List<DownloadedImage> processedImages = successfullyDownloadedImages |
| 55 | + .stream() |
| 56 | + .map(di -> imageService.processImage(di)) |
| 57 | + .collect(Collectors.toList()); |
| 58 | + |
| 59 | + |
| 60 | + //save transformed images to disk |
| 61 | + processedImages |
| 62 | + .stream() |
| 63 | + .filter(di -> !di.isSuccessful()) |
| 64 | + .forEach(f -> fileService.saveImageAsFile(f)); |
| 65 | + |
| 66 | + //where do we save an image? - use some config service - FileConfigService |
| 67 | + |
| 68 | + //generate a file name. how? |
| 69 | + //file naming strategy: 1. action_name+random_number.jpg -> which target format to use? |
| 70 | + // 2. use a file name from original URL. |
| 71 | + //lukaroundimg/beelitz2017/1a.jpg -> lukaroundimg_beelitz2017_1a_TRULALA.jpg |
| 72 | + //lukaroundimg/beelitz2017/1a.jpg -> lukaroundimg_beelitz2017_1a_GRAYSCALE.jpg |
| 73 | + //lukaroundimg_beelitz2017_1a.jpg -> lukaroundimg_beelitz2017_1a02.jpg |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments