Permalink
Browse files

Merge pull request #27 from tivac/max-image-size.

Max image size
  • Loading branch information...
2 parents 790b69a + 62288c7 commit ac0d3d17c32ddebb882740ecb11bf5efa25cc318 Nicholas C. Zakas committed May 1, 2011
@@ -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("max-uri-length");
+ CmdLineParser.Option imageSizeOpt = parser.addIntegerOption("max-image-size");
try {
@@ -114,6 +115,16 @@ public static void main(String[] args) {
uriLength = 0;
}
}
+
+ //maximum size allowed for image files
+ int imageSize = 0;
+ Integer imageSizeOption = (Integer) parser.getOptionValue(imageSizeOpt);
+ if (imageSizeOption != null){
+ imageSize = imageSizeOption.intValue();
+ if (imageSize < 0){
+ imageSize = 0;
+ }
+ }
//determine if MHTML mode is on
boolean mhtml = parser.getOptionValue(mhtmlOpt) != null;
@@ -131,7 +142,7 @@ public static void main(String[] args) {
options = options | CSSURLEmbedder.SKIP_MISSING_OPTION;
}
- CSSURLEmbedder embedder = new CSSURLEmbedder(in, options, verbose, uriLength);
+ CSSURLEmbedder embedder = new CSSURLEmbedder(in, options, verbose, uriLength, imageSize);
embedder.setMHTMLRoot(mhtmlRoot);
//close in case writing to the same file
@@ -175,8 +186,6 @@ public static void main(String[] args) {
out = new OutputStreamWriter(bytes, charset);
}
-
-
//set verbose option
embedder.embedImages(out, root);
@@ -213,7 +222,7 @@ public static void main(String[] args) {
*/
private static void usage() {
System.out.println(
- "\nUsage: java -jar cssembed-x.y.z.jar [options] [input file]\n\n"
+ "\nUsage: java -jar cssembed-x.y.z.jar [options] [input file]\n\n"
+ "Global Options\n"
+ " -h, --help Displays this information.\n"
@@ -224,6 +233,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"
+ " --max-uri-length len Maximum length for a data URI. Defaults to 32768.\n"
+ + " --max-image-size size Maximum image size (in bytes) to convert.\n"
+ " -o <file> Place the output into <file>. Defaults to stdout.");
}
-}
+}
@@ -65,6 +65,7 @@
private String mhtmlRoot = "";
private String outputFilename = "";
private int maxUriLength = DEFAULT_MAX_URI_LENGTH; //IE8 only allows dataURIs up to 32KB
+ private int maxImageSize;
//--------------------------------------------------------------------------
// Constructors
@@ -79,20 +80,23 @@ public CSSURLEmbedder(Reader in, int options) throws IOException {
}
public CSSURLEmbedder(Reader in, boolean verbose) throws IOException {
- this(in,1,verbose);
+ this(in, 1, verbose);
}
public CSSURLEmbedder(Reader in, int options, boolean verbose) throws IOException {
- this.code = readCode(in);
- this.verbose = verbose;
- this.options = options;
+ this(in, options, verbose, 0);
}
-
+
public CSSURLEmbedder(Reader in, int options, boolean verbose, int maxUriLength) throws IOException {
+ this(in, options, verbose, maxUriLength, 0);
+ }
+
+ 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;
}
//--------------------------------------------------------------------------
@@ -159,7 +163,6 @@ public void embedImages(Writer out, String root) throws IOException {
StringBuilder builder = new StringBuilder();
StringBuilder mhtmlHeader = new StringBuilder();
HashMap<String,Integer> foundMedia = new HashMap<String,Integer>();
-
String line;
int lineNum = 1;
int conversions = 0;
@@ -237,6 +240,11 @@ public void embedImages(Writer out, String root) throws IOException {
System.err.println("[WARNING] File " + newUrl + " creates a data URI larger than " + maxUriLength + " bytes. Skipping.");
}
builder.append(url);
+ } else if (maxUriLength > 0 && uriString.length() > maxUriLength){
+ if (verbose) {
+ System.err.println("[INFO] File " + newUrl + " creates a data URI longer than " + maxUriLength + " characters. Skipping.");
+ }
+ builder.append(url);
} else {
/*
@@ -342,9 +350,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){
@@ -140,28 +140,56 @@ public void testAbsoluteLocalFileWithMissingFilesEnabled() throws IOException {
assertEquals(code, result);
}
+
+
@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.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.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();
assertEquals(code, result);
}

0 comments on commit ac0d3d1

Please sign in to comment.