Permalink
Browse files

Add -i/--image-size parameter, which lets you only convert files unde…

…r a certain number of bytes.

This tackles the first of two options Nicholas mentioned in Issue #6
  • Loading branch information...
1 parent 34d41a1 commit a4f622dd7ed65a241935601f8fae73ad3f401ee5 @tivac tivac committed Apr 19, 2011
View
19 src/net/nczonline/web/cssembed/CSSEmbed.java
@@ -64,6 +64,7 @@ public static void main(String[] args) {
CmdLineParser.Option mhtmlRootOpt = parser.addStringOption("mhtmlroot");
CmdLineParser.Option skipMissingOpt = parser.addBooleanOption("skip-missing");
CmdLineParser.Option uriLengthOpt = parser.addIntegerOption('u', "maxuri");
+ CmdLineParser.Option imageSizeOpt = parser.addIntegerOption('i', "image-size");
try {
@@ -114,6 +115,8 @@ public static void main(String[] args) {
if (mhtml && mhtmlRoot == null){
throw new Exception("Must use --mhtmlroot when using --mhtml.");
}
+
+ //maximum length allowed for generated dataURI
int maxurilength = 0;
Integer uriOption = (Integer) parser.getOptionValue(uriLengthOpt);
if (uriOption != null){
@@ -123,13 +126,23 @@ public static void main(String[] args) {
}
}
+ //maximum size allowed for image files
+ int maximagesize = 0;
+ Integer imageSizeOption = (Integer) parser.getOptionValue(imageSizeOpt);
+ if (imageSizeOption != null){
+ maximagesize = imageSizeOption.intValue();
+ if (maximagesize < 0){
+ maximagesize = 0;
+ }
+ }
+
//are missing files ok?
boolean skipMissingFiles = parser.getOptionValue(skipMissingOpt) != null;
if(skipMissingFiles) {
options = options | CSSURLEmbedder.SKIP_MISSING_OPTION;
}
- CSSURLEmbedder embedder = new CSSURLEmbedder(in, options, verbose, maxurilength);
+ CSSURLEmbedder embedder = new CSSURLEmbedder(in, options, verbose, maxurilength, maximagesize);
embedder.setMHTMLRoot(mhtmlRoot);
//close in case writing to the same file
@@ -173,8 +186,6 @@ public static void main(String[] args) {
out = new OutputStreamWriter(bytes, charset);
}
-
-
//set verbose option
embedder.embedImages(out, root);
@@ -191,6 +202,7 @@ public static void main(String[] args) {
if (out != null) {
try {
out.close();
+
bytes.writeTo(new FileOutputStream(outputFilename));
} catch (IOException e) {
System.err.println("[ERROR] " + e.getMessage());
@@ -219,6 +231,7 @@ private static void usage() {
+ " --root <root> Prepends <root> to all relative URLs.\n"
+ " --skip-missing Don't throw an error for missing image files.\n"
+ " -u, --maxuri length Maximum length of data URI to use.\n"
+ + " -i, --image-size Maximum image size (in bytes) to convert.\n"
+ " -o <file> Place the output into <file>. Defaults to stdout."
);
}
View
20 src/net/nczonline/web/cssembed/CSSURLEmbedder.java
@@ -63,6 +63,7 @@
private String mhtmlRoot = "";
private String outputFilename = "";
private int maxurilength;
+ private int maximagesize;
//--------------------------------------------------------------------------
// Constructors
@@ -81,14 +82,15 @@ public CSSURLEmbedder(Reader in, boolean verbose) throws IOException {
}
public CSSURLEmbedder(Reader in, int options, boolean verbose) throws IOException {
- this(in,options,verbose,0);
+ this(in,options,verbose,0,0);
}
- public CSSURLEmbedder(Reader in, int options, boolean verbose, int maxurilength) throws IOException {
+ public CSSURLEmbedder(Reader in, int options, boolean verbose, int maxurilength, int maximagesize) throws IOException {
this.code = readCode(in);
this.verbose = verbose;
this.options = options;
this.maxurilength = maxurilength;
+ this.maximagesize = maximagesize;
}
//--------------------------------------------------------------------------
@@ -338,9 +340,19 @@ private String getImageURIString(String url, String originalUrl) throws IOExcept
if (verbose && !file.isFile()){
System.err.println("[INFO] Could not find file '" + file.getCanonicalPath() + "'.");
- }
+ }
- DataURIGenerator.generate(new File(url), writer);
+ //check file size if we've been asked to
+ if(this.maximagesize > 0 && file.length() > this.maximagesize) {
+ if(verbose) {
+ System.err.println("[INFO] File " + originalUrl + " is larger than " + this.maximagesize + " bytes. Skipping.");
+ }
+
+ writer.write(originalUrl);
+
+ } else {
+ DataURIGenerator.generate(new File(url), writer);
+ }
}
if (verbose){
View
34 tests/net/nczonline/web/cssembed/CSSURLEmbedderTest.java
@@ -141,25 +141,51 @@ public void testAbsoluteLocalFileWithMissingFilesEnabled() throws IOException {
}
@Test
- public void testAbsoluteLocalFileUnderMaxSize() throws IOException {
+ public void testAbsoluteLocalFileUnderMaxLength() throws IOException {
String filename = CSSURLEmbedderTest.class.getResource("folder.png").getPath().replace("%20", " ");
String code = "background: url(folder.png);";
StringWriter writer = new StringWriter();
- embedder = new CSSURLEmbedder(new StringReader(code), CSSURLEmbedder.DATAURI_OPTION, true, 1000);
+ embedder = new CSSURLEmbedder(new StringReader(code), CSSURLEmbedder.DATAURI_OPTION, true, 1000, 0);
embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
String result = writer.toString();
assertEquals("background: url(" + folderDataURI + ");", result);
}
@Test
- public void testAbsoluteLocalFileOverMaxSize() throws IOException {
+ public void testAbsoluteLocalFileOverMaxLength() throws IOException {
String filename = CSSURLEmbedderTest.class.getResource("folder.png").getPath().replace("%20", " ");
String code = "background: url(folder.png);";
StringWriter writer = new StringWriter();
- embedder = new CSSURLEmbedder(new StringReader(code), CSSURLEmbedder.DATAURI_OPTION, true, 200);
+ embedder = new CSSURLEmbedder(new StringReader(code), CSSURLEmbedder.DATAURI_OPTION, true, 200, 0);
+ embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
+
+ String result = writer.toString();
+ assertEquals(code, result);
+ }
+
+ @Test
+ public void testAbsoluteLocalFileUnderMaxImageSize() throws IOException {
+ String filename = CSSURLEmbedderTest.class.getResource("folder.png").getPath().replace("%20", " ");
+ String code = "background: url(folder.png);";
+
+ StringWriter writer = new StringWriter();
+ embedder = new CSSURLEmbedder(new StringReader(code), CSSURLEmbedder.DATAURI_OPTION, true, 0, 300);
+ embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
+
+ String result = writer.toString();
+ assertEquals("background: url(" + folderDataURI + ");", result);
+ }
+
+ @Test
+ public void testAbsoluteLocalFileOverMaxImageSize() throws IOException {
+ String filename = CSSURLEmbedderTest.class.getResource("folder.png").getPath().replace("%20", " ");
+ String code = "background: url(folder.png);";
+
+ StringWriter writer = new StringWriter();
+ embedder = new CSSURLEmbedder(new StringReader(code), CSSURLEmbedder.DATAURI_OPTION, true, 0, 200);
embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));
String result = writer.toString();

0 comments on commit a4f622d

Please sign in to comment.