Skip to content
This repository has been archived by the owner on Nov 16, 2018. It is now read-only.

Commit

Permalink
Merge pull request #27 from tivac/max-image-size.
Browse files Browse the repository at this point in the history
Max image size
  • Loading branch information
Nicholas C. Zakas committed May 1, 2011
2 parents 790b69a + 62288c7 commit ac0d3d1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 20 deletions.
20 changes: 15 additions & 5 deletions src/net/nczonline/web/cssembed/CSSEmbed.java
Expand Up @@ -64,6 +64,7 @@ public static void main(String[] args) {
CmdLineParser.Option mhtmlRootOpt = parser.addStringOption("mhtmlroot"); CmdLineParser.Option mhtmlRootOpt = parser.addStringOption("mhtmlroot");
CmdLineParser.Option skipMissingOpt = parser.addBooleanOption("skip-missing"); CmdLineParser.Option skipMissingOpt = parser.addBooleanOption("skip-missing");
CmdLineParser.Option uriLengthOpt = parser.addIntegerOption("max-uri-length"); CmdLineParser.Option uriLengthOpt = parser.addIntegerOption("max-uri-length");
CmdLineParser.Option imageSizeOpt = parser.addIntegerOption("max-image-size");


try { try {


Expand Down Expand Up @@ -114,6 +115,16 @@ public static void main(String[] args) {
uriLength = 0; 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 //determine if MHTML mode is on
boolean mhtml = parser.getOptionValue(mhtmlOpt) != null; boolean mhtml = parser.getOptionValue(mhtmlOpt) != null;
Expand All @@ -131,7 +142,7 @@ public static void main(String[] args) {
options = options | CSSURLEmbedder.SKIP_MISSING_OPTION; 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); embedder.setMHTMLRoot(mhtmlRoot);


//close in case writing to the same file //close in case writing to the same file
Expand Down Expand Up @@ -175,8 +186,6 @@ public static void main(String[] args) {
out = new OutputStreamWriter(bytes, charset); out = new OutputStreamWriter(bytes, charset);
} }




//set verbose option //set verbose option
embedder.embedImages(out, root); embedder.embedImages(out, root);


Expand Down Expand Up @@ -213,7 +222,7 @@ public static void main(String[] args) {
*/ */
private static void usage() { private static void usage() {
System.out.println( 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" + "Global Options\n"
+ " -h, --help Displays this information.\n" + " -h, --help Displays this information.\n"
Expand All @@ -224,6 +233,7 @@ private static void usage() {
+ " --root <root> Prepends <root> to all relative URLs.\n" + " --root <root> Prepends <root> to all relative URLs.\n"
+ " --skip-missing Don't throw an error for missing image files.\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-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."); + " -o <file> Place the output into <file>. Defaults to stdout.");
} }
} }
34 changes: 26 additions & 8 deletions src/net/nczonline/web/cssembed/CSSURLEmbedder.java
Expand Up @@ -65,6 +65,7 @@ public class CSSURLEmbedder {
private String mhtmlRoot = ""; private String mhtmlRoot = "";
private String outputFilename = ""; private String outputFilename = "";
private int maxUriLength = DEFAULT_MAX_URI_LENGTH; //IE8 only allows dataURIs up to 32KB private int maxUriLength = DEFAULT_MAX_URI_LENGTH; //IE8 only allows dataURIs up to 32KB
private int maxImageSize;


//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// Constructors // Constructors
Expand All @@ -79,20 +80,23 @@ public CSSURLEmbedder(Reader in, int options) throws IOException {
} }


public CSSURLEmbedder(Reader in, boolean verbose) 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 { public CSSURLEmbedder(Reader in, int options, boolean verbose) throws IOException {
this.code = readCode(in); this(in, options, verbose, 0);
this.verbose = verbose;
this.options = options;
} }

public CSSURLEmbedder(Reader in, int options, boolean verbose, int maxUriLength) throws IOException { 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.code = readCode(in);
this.verbose = verbose; this.verbose = verbose;
this.options = options; this.options = options;
this.maxUriLength = maxUriLength; this.maxUriLength = maxUriLength;
this.maxImageSize = maxImageSize;
} }


//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
Expand Down Expand Up @@ -159,7 +163,6 @@ public void embedImages(Writer out, String root) throws IOException {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
StringBuilder mhtmlHeader = new StringBuilder(); StringBuilder mhtmlHeader = new StringBuilder();
HashMap<String,Integer> foundMedia = new HashMap<String,Integer>(); HashMap<String,Integer> foundMedia = new HashMap<String,Integer>();

String line; String line;
int lineNum = 1; int lineNum = 1;
int conversions = 0; int conversions = 0;
Expand Down Expand Up @@ -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."); System.err.println("[WARNING] File " + newUrl + " creates a data URI larger than " + maxUriLength + " bytes. Skipping.");
} }
builder.append(url); 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 { } else {


/* /*
Expand Down Expand Up @@ -342,9 +350,19 @@ private String getImageURIString(String url, String originalUrl) throws IOExcept


if (verbose && !file.isFile()){ if (verbose && !file.isFile()){
System.err.println("[INFO] Could not find file '" + file.getCanonicalPath() + "'."); 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){ if (verbose){
Expand Down
42 changes: 35 additions & 7 deletions tests/net/nczonline/web/cssembed/CSSURLEmbedderTest.java
Expand Up @@ -140,28 +140,56 @@ public void testAbsoluteLocalFileWithMissingFilesEnabled() throws IOException {
assertEquals(code, result); assertEquals(code, result);
} }




@Test @Test
public void testAbsoluteLocalFileUnderMaxSize() throws IOException { public void testAbsoluteLocalFileUnderMaxLength() throws IOException {
String filename = CSSURLEmbedderTest.class.getResource("folder.png").getPath().replace("%20", " "); String filename = CSSURLEmbedderTest.class.getResource("folder.png").getPath().replace("%20", " ");
String code = "background: url(folder.png);"; String code = "background: url(folder.png);";

StringWriter writer = new StringWriter(); 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);
embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1)); embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1));

String result = writer.toString(); String result = writer.toString();
assertEquals("background: url(" + folderDataURI + ");", result); assertEquals("background: url(" + folderDataURI + ");", result);
} }

@Test @Test
public void testAbsoluteLocalFileOverMaxSize() throws IOException { public void testAbsoluteLocalFileOverMaxLength() throws IOException {
String filename = CSSURLEmbedderTest.class.getResource("folder.png").getPath().replace("%20", " "); String filename = CSSURLEmbedderTest.class.getResource("folder.png").getPath().replace("%20", " ");
String code = "background: url(folder.png);"; String code = "background: url(folder.png);";

StringWriter writer = new StringWriter(); 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);
embedder.embedImages(writer, filename.substring(0, filename.lastIndexOf("/")+1)); 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(); String result = writer.toString();
assertEquals(code, result); assertEquals(code, result);
} }
Expand Down

0 comments on commit ac0d3d1

Please sign in to comment.