java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 20:38:38 +0100
changeset 15 93fa6ce675e5
parent 14 dec0dd934a64
child 17 505031965440
permissions -rw-r--r--
wasResized is decided in SingleImageResizer
     1 /**
     2  * copy-image-resizer
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.copyImageResizer;
    19 
    20 import cz.frantovo.copyImageResizer.SingleImageResizer.ImageFormat;
    21 import java.io.File;
    22 import java.io.FileInputStream;
    23 import java.io.FileNotFoundException;
    24 import java.io.FileOutputStream;
    25 import java.io.IOException;
    26 import java.nio.channels.FileChannel;
    27 import java.util.logging.Level;
    28 import java.util.logging.LogRecord;
    29 import java.util.logging.Logger;
    30 
    31 /**
    32  *
    33  * @author Ing. František Kučera (frantovo.cz)
    34  */
    35 public class RecursiveImageResizer {
    36 
    37 	private static final Logger log = Logger.getLogger(RecursiveImageResizer.class.getName());
    38 
    39 	private final Counters counters = new Counters();
    40 
    41 	private final SingleImageResizer resizer = new SingleImageResizer();
    42 
    43 	private final RecursiveOptions options;
    44 
    45 	public RecursiveImageResizer(RecursiveOptions options) {
    46 		this.options = options;
    47 	}
    48 
    49 	public Counters resize() throws RecursiveException, ResizeException {
    50 		resizeDirectory(options.getInput());
    51 		return counters;
    52 	}
    53 
    54 	private void resizeFile(File inputFile) throws ResizeException {
    55 		File inputFileRelative = relativize(options.getInput(), inputFile);
    56 		log.log(Level.FINER, "Resizing file: {0}", inputFileRelative);
    57 		counters.increment(Counters.COUNTER_TYPE.FILES);
    58 
    59 		ImageFormat format = ImageFormat.getMatching(inputFile.getName());
    60 
    61 		if (format == null) {
    62 			log.log(Level.FINER, "Skipping file: {0} (no image format matched this extension)", inputFileRelative);
    63 			counters.increment(Counters.COUNTER_TYPE.SKIPPED_UNKNOWN_EXTENSION);
    64 		} else {
    65 			try {
    66 				for (SizeSpecification size : options.getSizes()) {
    67 					File sizeRoot = new File(options.getOutput(), size.getDirectory());
    68 					File outputFile = new File(sizeRoot, inputFileRelative.getPath());
    69 					try (FileInputStream input = new FileInputStream(inputFile)) {
    70 
    71 						try (FileOutputStream output = new FileOutputStream(outputFile)) {
    72 							boolean wasResized = resizer.resize(input, output, size, format);
    73 							if (wasResized) {
    74 								counters.increment(Counters.COUNTER_TYPE.RESIZED);
    75 							} else {
    76 								log.log(Level.FINER, "File: {0} has already required (or smaller) size → just copy", inputFileRelative);
    77 								justCopy(inputFile, outputFile);
    78 								counters.increment(Counters.COUNTER_TYPE.JUST_COPIED);
    79 							}
    80 						}
    81 					}
    82 				}
    83 			} catch (FileNotFoundException e) {
    84 				throw new ResizeException("Error while opening stream", e);
    85 			} catch (IOException e) {
    86 				throw new ResizeException("Error while closing stream", e);
    87 			}
    88 		}
    89 	}
    90 
    91 	private static void justCopy(File inputFile, File outputFile) throws ResizeException {
    92 		try {
    93 
    94 			if (!outputFile.exists()) {
    95 				outputFile.createNewFile();
    96 			}
    97 
    98 			try (FileChannel input = new FileInputStream(inputFile).getChannel()) {
    99 				try (FileChannel output = new FileOutputStream(outputFile).getChannel()) {
   100 					output.transferFrom(input, 0, input.size());
   101 				}
   102 			}
   103 
   104 		} catch (IOException e) {
   105 			throw new ResizeException("Unable copy stream/channel", e);
   106 		}
   107 
   108 	}
   109 
   110 	private void resizeDirectory(File directory) throws ResizeException {
   111 
   112 		log.log(Level.FINE, "Resizing directory: {0}", directory);
   113 		counters.increment(Counters.COUNTER_TYPE.DIRECTORIES);
   114 
   115 		for (SizeSpecification size : options.getSizes()) {
   116 			File relative = relativize(options.getInput(), directory);
   117 			File sizeRoot = new File(options.getOutput(), size.getDirectory());
   118 			File dir = new File(sizeRoot, relative.getPath());
   119 			dir.mkdirs();
   120 		}
   121 
   122 		for (File entry : directory.listFiles()) {
   123 			if (entry.isDirectory()) {
   124 				resizeDirectory(entry);
   125 			} else {
   126 				if (options.isSkipErrors()) {
   127 					try {
   128 						resizeFile(entry);
   129 					} catch (Exception e) {
   130 						counters.increment(Counters.COUNTER_TYPE.SKIPPED_ERROR);
   131 						LogRecord record = new LogRecord(Level.WARNING, "Skipping error : {0}");
   132 						record.setParameters(new Object[]{entry});
   133 						record.setThrown(e);
   134 						log.log(record);
   135 					}
   136 				} else {
   137 					resizeFile(entry);
   138 				}
   139 
   140 			}
   141 		}
   142 
   143 	}
   144 
   145 	private static File relativize(File root, File child) {
   146 		return root.toPath().relativize(child.toPath()).toFile();
   147 	}
   148 
   149 }