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