franta-hg@5: /** franta-hg@5: * copy-image-resizer franta-hg@5: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@5: * franta-hg@5: * This program is free software: you can redistribute it and/or modify franta-hg@5: * it under the terms of the GNU General Public License as published by franta-hg@5: * the Free Software Foundation, either version 3 of the License, or franta-hg@5: * (at your option) any later version. franta-hg@5: * franta-hg@5: * This program is distributed in the hope that it will be useful, franta-hg@5: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@5: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@5: * GNU General Public License for more details. franta-hg@5: * franta-hg@5: * You should have received a copy of the GNU General Public License franta-hg@5: * along with this program. If not, see . franta-hg@5: */ franta-hg@5: package cz.frantovo.copyImageResizer; franta-hg@5: franta-hg@6: import cz.frantovo.copyImageResizer.SingleImageResizer.ImageFormat; franta-hg@6: import java.io.File; franta-hg@6: import java.io.FileInputStream; franta-hg@6: import java.io.FileNotFoundException; franta-hg@6: import java.io.FileOutputStream; franta-hg@6: import java.util.logging.Level; franta-hg@6: import java.util.logging.Logger; franta-hg@6: franta-hg@5: /** franta-hg@5: * franta-hg@5: * @author Ing. František Kučera (frantovo.cz) franta-hg@5: */ franta-hg@5: public class RecursiveImageResizer { franta-hg@5: franta-hg@6: private static final Logger log = Logger.getLogger(RecursiveImageResizer.class.getName()); franta-hg@6: franta-hg@6: private final SingleImageResizer resizer = new SingleImageResizer(); franta-hg@6: franta-hg@5: public void resize(RecursiveOptions options) throws RecursiveException, ResizeException { franta-hg@6: resizeDirectory(options.getInput(), options); franta-hg@6: } franta-hg@6: franta-hg@6: private void resizeFile(File file, RecursiveOptions options) throws ResizeException { franta-hg@6: log.log(Level.FINE, "Resizing file: {0}", relativize(options.getInput(), file)); franta-hg@6: franta-hg@6: ImageFormat format = ImageFormat.getMatching(file.getName()); franta-hg@6: franta-hg@6: if (format == null) { franta-hg@6: log.log(Level.FINE, "Skipping file: {0} (no image format matched this extension)", relativize(options.getInput(), file)); franta-hg@6: } else { franta-hg@6: try { franta-hg@6: FileInputStream input = new FileInputStream(file); franta-hg@6: File relative = relativize(options.getInput(), file); franta-hg@6: File outputFile = new File(options.getOutput(), relative.getPath()); franta-hg@6: FileOutputStream output = new FileOutputStream(outputFile); franta-hg@6: resizer.resize(input, output, format); franta-hg@6: } catch (FileNotFoundException e) { franta-hg@6: throw new ResizeException("Error while opening stream", e); franta-hg@6: } franta-hg@6: } franta-hg@6: franta-hg@6: } franta-hg@6: franta-hg@6: private void resizeDirectory(File directory, RecursiveOptions options) throws ResizeException { franta-hg@6: franta-hg@6: log.log(Level.FINE, "Resizing directory: {0}", directory); franta-hg@6: franta-hg@6: File relative = relativize(options.getInput(), directory); franta-hg@6: File output = new File(options.getOutput(), relative.getPath()); franta-hg@6: output.mkdirs(); franta-hg@6: franta-hg@6: for (File entry : directory.listFiles()) { franta-hg@6: if (entry.isDirectory()) { franta-hg@6: resizeDirectory(entry, options); franta-hg@6: } else { franta-hg@6: resizeFile(entry, options); franta-hg@6: } franta-hg@6: } franta-hg@6: franta-hg@6: } franta-hg@6: franta-hg@6: private static File relativize(File root, File child) { franta-hg@6: return root.toPath().relativize(child.toPath()).toFile(); franta-hg@5: } franta-hg@5: franta-hg@5: }