java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java
3 * Copyright © 2014 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package cz.frantovo.copyImageResizer;
20 import cz.frantovo.copyImageResizer.JavaSingleImageResizer.ImageFormat;
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;
33 * @author Ing. František Kučera (frantovo.cz)
35 public class RecursiveImageResizer {
37 private static final Logger log = Logger.getLogger(RecursiveImageResizer.class.getName());
39 private final Counters counters = new Counters();
42 * TODO: support plugins – several implementations of SingleImageResizer
44 private final SingleImageResizer resizer = new JavaSingleImageResizer();
46 private final RecursiveOptions options;
48 public RecursiveImageResizer(RecursiveOptions options) {
49 this.options = options;
52 public Counters resize() throws RecursiveException, ResizeException {
53 resizeDirectory(options.getInput());
57 private void resizeFile(File inputFile) throws ResizeException {
58 File inputFileRelative = relativize(options.getInput(), inputFile);
59 log.log(Level.FINER, "Resizing file: {0}", inputFileRelative);
60 counters.increment(Counters.COUNTER_TYPE.FILES);
62 ImageFormat format = ImageFormat.getMatching(inputFile.getName());
65 log.log(Level.FINER, "Skipping file: {0} (no image format matched this extension)", inputFileRelative);
66 counters.increment(Counters.COUNTER_TYPE.SKIPPED_UNKNOWN_EXTENSION);
69 for (SizeSpecification size : options.getSizes()) {
70 File sizeRoot = new File(options.getOutput(), size.getDirectory());
71 File outputFile = new File(sizeRoot, inputFileRelative.getPath());
72 try (FileInputStream input = new FileInputStream(inputFile)) {
74 try (FileOutputStream output = new FileOutputStream(outputFile)) {
75 boolean wasResized = resizer.resize(input, output, size, format);
77 counters.increment(Counters.COUNTER_TYPE.RESIZED);
79 log.log(Level.FINER, "File: {0} has already required (or smaller) size → just copy", inputFileRelative);
80 justCopy(inputFile, outputFile);
81 counters.increment(Counters.COUNTER_TYPE.JUST_COPIED);
86 } catch (FileNotFoundException e) {
87 throw new ResizeException("Error while opening stream", e);
88 } catch (IOException e) {
89 throw new ResizeException("Error while closing stream", e);
94 private static void justCopy(File inputFile, File outputFile) throws ResizeException {
97 if (!outputFile.exists()) {
98 outputFile.createNewFile();
101 try (FileChannel input = new FileInputStream(inputFile).getChannel()) {
102 try (FileChannel output = new FileOutputStream(outputFile).getChannel()) {
103 output.transferFrom(input, 0, input.size());
107 } catch (IOException e) {
108 throw new ResizeException("Unable copy stream/channel", e);
113 private void resizeDirectory(File directory) throws ResizeException {
115 log.log(Level.FINE, "Resizing directory: {0}", directory);
116 counters.increment(Counters.COUNTER_TYPE.DIRECTORIES);
118 for (SizeSpecification size : options.getSizes()) {
119 File relative = relativize(options.getInput(), directory);
120 File sizeRoot = new File(options.getOutput(), size.getDirectory());
121 File dir = new File(sizeRoot, relative.getPath());
125 for (File entry : directory.listFiles()) {
126 if (entry.isDirectory()) {
127 resizeDirectory(entry);
129 if (options.getErrorMode() == RecursiveOptions.ERROR_MODE.FAIL_EARLY) {
134 } catch (Exception e) {
135 counters.increment(Counters.COUNTER_TYPE.SKIPPED_ERROR);
136 LogRecord record = new LogRecord(Level.WARNING, "Skipping error : {0}");
137 record.setParameters(new Object[]{entry});
148 private static File relativize(File root, File child) {
149 return root.toPath().relativize(child.toPath()).toFile();