importPackage(Packages.de.elo.ix.client); importPackage(java.awt); importPackage(java.awt.event); importPackage(java.io); importPackage(java.lang); importPackage(java.util); importPackage(javax.imageio); importPackage(javax.swing); importPackage(javax.swing.filechooser); //@include lib_Class.js //@include lib_sol.common.Cache.js //@include lib_sol.common.Config.js //@include lib_sol.common.IxUtils.js /** * Provides a converter from a Java filechooser dialog selected FILE to a base64 javascript string * * Displays the Java filechooser. If a file is selected, it will be validated against the rules (see eloReceiveBrowserMessage) * If the file is accepted, it will be converter to a base64 String * * @author ESt, ELO Digital Office GmbH * @version 1 * * @requires sol.common.IxUtils */ /** * * @param {java.FILE} file * @param {Object} rules * @param {Number} rules.maxSize maximum filesize in Megabyte (floats possible) * @param {String} rules.accept comma separated image-types to accept ("image/jpeg, image/jpg, image/png") */ function preconditionsFulfilled(file, rules) { var result = {}, is, mimeType; result.size = +(file.length()); result.type = (function (f) { is = new Packages.java.io.BufferedInputStream(new FileInputStream(f)); mimeType = Packages.java.net.URLConnection.guessContentTypeFromStream(is); return String(mimeType); })(file); result.ok = false; if (!rules) { result.ok = true; return result; } if (file.length() <= (rules.maxSize * 1000000)) { if ((rules.accept.split(",").map(function (s) { return String(s.trim()); })).indexOf(String(result.type)) > -1) { result.ok = true; } } return result; } /** * * @param {*} msg * @param {String} msg.name must contain "workspace.showFileChooserDialog_" * @param {Object} msg.rules see preconditionsFulfilled's rules parameter */ function eloReceiveBrowserMessage(msg, compName, browserComponent) { var dialogTitle, files, file, byteArray, re = /.*_(\{.*\})/g, match, result; if (msg.name.indexOf("workspace.showFileChooserDialog_") > -1) { dialogTitle = msg.data.dialogTitle || "Wählen Sie ein Mitarbeiterfoto aus"; files = workspace.showFileChooserDialog(dialogTitle, false, true, ""); if (files && files.length && (files.length > 0)) { file = files[0]; match = re.exec(msg.name); result = preconditionsFulfilled(file, (match && JSON.parse(match[1])) || msg.data.rules); result.base64 = ""; if (result.ok) { byteArray = Packages.org.apache.commons.io.FileUtils.readFileToByteArray(file); result.base64 = String(Packages.org.apache.commons.codec.binary.Base64.encodeBase64String(byteArray)); result.name = Packages.org.apache.commons.io.FilenameUtils.removeExtension(file.name) + ""; result.extension = Packages.org.apache.commons.io.FilenameUtils.getExtension(file.name) + ""; } } browserComponent.sendCustomResponse(msg, { response: result }); } }