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@6
|
20 |
import cz.frantovo.copyImageResizer.SingleImageResizer.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@6
|
25 |
import java.util.logging.Level;
|
franta-hg@6
|
26 |
import java.util.logging.Logger;
|
franta-hg@6
|
27 |
|
franta-hg@5
|
28 |
/**
|
franta-hg@5
|
29 |
*
|
franta-hg@5
|
30 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@5
|
31 |
*/
|
franta-hg@5
|
32 |
public class RecursiveImageResizer {
|
franta-hg@5
|
33 |
|
franta-hg@6
|
34 |
private static final Logger log = Logger.getLogger(RecursiveImageResizer.class.getName());
|
franta-hg@6
|
35 |
|
franta-hg@6
|
36 |
private final SingleImageResizer resizer = new SingleImageResizer();
|
franta-hg@6
|
37 |
|
franta-hg@5
|
38 |
public void resize(RecursiveOptions options) throws RecursiveException, ResizeException {
|
franta-hg@6
|
39 |
resizeDirectory(options.getInput(), options);
|
franta-hg@6
|
40 |
}
|
franta-hg@6
|
41 |
|
franta-hg@6
|
42 |
private void resizeFile(File file, RecursiveOptions options) throws ResizeException {
|
franta-hg@6
|
43 |
log.log(Level.FINE, "Resizing file: {0}", relativize(options.getInput(), file));
|
franta-hg@6
|
44 |
|
franta-hg@6
|
45 |
ImageFormat format = ImageFormat.getMatching(file.getName());
|
franta-hg@6
|
46 |
|
franta-hg@6
|
47 |
if (format == null) {
|
franta-hg@6
|
48 |
log.log(Level.FINE, "Skipping file: {0} (no image format matched this extension)", relativize(options.getInput(), file));
|
franta-hg@6
|
49 |
} else {
|
franta-hg@6
|
50 |
try {
|
franta-hg@6
|
51 |
FileInputStream input = new FileInputStream(file);
|
franta-hg@6
|
52 |
File relative = relativize(options.getInput(), file);
|
franta-hg@6
|
53 |
File outputFile = new File(options.getOutput(), relative.getPath());
|
franta-hg@6
|
54 |
FileOutputStream output = new FileOutputStream(outputFile);
|
franta-hg@6
|
55 |
resizer.resize(input, output, format);
|
franta-hg@6
|
56 |
} catch (FileNotFoundException e) {
|
franta-hg@6
|
57 |
throw new ResizeException("Error while opening stream", e);
|
franta-hg@6
|
58 |
}
|
franta-hg@6
|
59 |
}
|
franta-hg@6
|
60 |
|
franta-hg@6
|
61 |
}
|
franta-hg@6
|
62 |
|
franta-hg@6
|
63 |
private void resizeDirectory(File directory, RecursiveOptions options) throws ResizeException {
|
franta-hg@6
|
64 |
|
franta-hg@6
|
65 |
log.log(Level.FINE, "Resizing directory: {0}", directory);
|
franta-hg@6
|
66 |
|
franta-hg@6
|
67 |
File relative = relativize(options.getInput(), directory);
|
franta-hg@6
|
68 |
File output = new File(options.getOutput(), relative.getPath());
|
franta-hg@6
|
69 |
output.mkdirs();
|
franta-hg@6
|
70 |
|
franta-hg@6
|
71 |
for (File entry : directory.listFiles()) {
|
franta-hg@6
|
72 |
if (entry.isDirectory()) {
|
franta-hg@6
|
73 |
resizeDirectory(entry, options);
|
franta-hg@6
|
74 |
} else {
|
franta-hg@6
|
75 |
resizeFile(entry, options);
|
franta-hg@6
|
76 |
}
|
franta-hg@6
|
77 |
}
|
franta-hg@6
|
78 |
|
franta-hg@6
|
79 |
}
|
franta-hg@6
|
80 |
|
franta-hg@6
|
81 |
private static File relativize(File root, File child) {
|
franta-hg@6
|
82 |
return root.toPath().relativize(child.toPath()).toFile();
|
franta-hg@5
|
83 |
}
|
franta-hg@5
|
84 |
|
franta-hg@5
|
85 |
}
|