# HG changeset patch # User Robert Casties # Date 1504712803 -7200 # Node ID 146c5f619e3ca5bcc1f308c0ba617af3f431b4a9 # Parent 2184c0ef4b303ce33ed458faa327a563043357d2 implementing #7 * sents content-disposition filename for all files sent-as-is * takes filename for generated images from "download-filename" hint (but hint is not yet generated). diff -r 2184c0ef4b30 -r 146c5f619e3c common/src/main/java/digilib/image/DocuImageImpl.java --- a/common/src/main/java/digilib/image/DocuImageImpl.java Wed Sep 06 17:42:20 2017 +0200 +++ b/common/src/main/java/digilib/image/DocuImageImpl.java Wed Sep 06 17:46:43 2017 +0200 @@ -30,6 +30,7 @@ import java.awt.Rectangle; import java.io.IOException; import java.io.OutputStream; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -71,7 +72,7 @@ protected ImageInput input; /** image specific hints */ - protected Map hints; + protected Map hints = new HashMap(); /** * Returns the version. diff -r 2184c0ef4b30 -r 146c5f619e3c servlet/src/main/java/digilib/servlet/ServletOps.java --- a/servlet/src/main/java/digilib/servlet/ServletOps.java Wed Sep 06 17:42:20 2017 +0200 +++ b/servlet/src/main/java/digilib/servlet/ServletOps.java Wed Sep 06 17:46:43 2017 +0200 @@ -267,13 +267,14 @@ public static void sendFile(File f, String mt, String name, HttpServletResponse response, Logger logger) throws ImageOpException, IOException { logger.debug("sendRawFile(" + mt + ", " + f + ")"); - if (response == null) { - logger.error("No response!"); - return; - } - /* - * set content-type - */ + if (response == null) { + logger.error("No response!"); + return; + } + + /* + * set content-type + */ if (mt == null) { // auto-detect mime-type mt = FileOps.mimeForFile(f); @@ -282,34 +283,44 @@ } } response.setContentType(mt); + + /* + * set content-disposition with filename. + * uses image filename. + */ + if (name == null) { + // no download name -- use filename + name = f.getName(); + } if (mt.startsWith("application")) { - if (name == null) { - // no download name -- use filename - name = f.getName(); - } - response.addHeader("Content-Disposition", "attachment; filename=\""+name+"\""); + response.addHeader("Content-Disposition", "attachment; filename=\"" + name + "\""); + } else { + response.addHeader("Content-Disposition", "inline; filename=\"" + name + "\""); } /* - * set CORS header ACAO "*" for image response - */ - if (corsForImageRequests) { - // TODO: would be nice to check request for Origin header - response.setHeader("Access-Control-Allow-Origin", "*"); - } + * set CORS header ACAO "*" for image response + */ + if (corsForImageRequests) { + // TODO: would be nice to check request for Origin header + response.setHeader("Access-Control-Allow-Origin", "*"); + } /* * open file */ - FileInputStream inFile = null; + FileInputStream inFile = null; try { inFile = new FileInputStream(f); OutputStream outStream = response.getOutputStream(); - // TODO: should we set content length? + // TODO: should we set content length? // see http://www.prozesse-und-systeme.de/servletFlush.html - response.setContentLength( (int) f.length()); + response.setContentLength((int) f.length()); byte dataBuffer[] = new byte[4096]; int len; + /* + * write file to stream + */ while ((len = inFile.read(dataBuffer)) != -1) { // copy out file outStream.write(dataBuffer, 0, len); @@ -352,17 +363,16 @@ * @throws ImageOpException * @throws ServletException Exception on sending data. */ - public static void sendImage(DocuImage img, String mimeType, HttpServletResponse response, Logger logger) - throws ImageOpException, ServletException { - if (response == null) { - logger.error("No response!"); - return; - } - logger.debug("sending to response. committed=" + response.isCommitted()); + public static void sendImage(DocuImage img, String mimeType, HttpServletResponse response, Logger logger) + throws ImageOpException, ServletException { + if (response == null) { + logger.error("No response!"); + return; + } try { /* - * determine the content-type: if mime type is set use that - * otherwise if source is JPG then dest will be JPG else it's PNG + * determine the content-type: if mime type is set use that otherwise if source + * is JPG then dest will be JPG else it's PNG */ if (mimeType == null) { mimeType = img.getMimetype(); @@ -372,47 +382,44 @@ mimeType = "image/jpeg"; } } - if ((mimeType.equals("image/jpeg") || mimeType.equals("image/jp2") || - mimeType.equals("image/fpx"))) { + if ((mimeType.equals("image/jpeg") || mimeType.equals("image/jp2") || mimeType.equals("image/fpx"))) { mimeType = "image/jpeg"; } else { mimeType = "image/png"; } // set the content type response.setContentType(mimeType); - // check content type - String respType = response.getContentType(); - if (! mimeType.equals(respType)) { - // this shouldn't happen - logger.error("Crap! ServletResponse lost content type! ["+respType+"] Aborting!"); - // TODO: would this even help? - response.getOutputStream().close(); - return; + + /* + * set content-disposition with filename. + * uses filename provided in DocuImage. + */ + String name = (String) img.getHint("download-filename"); + if (name != null) { + response.addHeader("Content-Disposition", "inline; filename=\"" + name + "\""); } - /* - * set CORS header ACAO "*" for image response - */ - if (corsForImageRequests) { - // TODO: would be nice to check request for Origin header - response.setHeader("Access-Control-Allow-Origin", "*"); - } + * set CORS header ACAO "*" for image response + */ + if (corsForImageRequests) { + // TODO: would be nice to check request for Origin header + response.setHeader("Access-Control-Allow-Origin", "*"); + } /* * write the image */ OutputStream outstream = response.getOutputStream(); img.writeImage(mimeType, outstream); - + } catch (IOException e) { throw new ServletException("Error sending image:", e); - } finally { - img.dispose(); - } + } finally { + img.dispose(); + } } - /** * Returns IIIF compatible image information as application/json response. *