List of usage examples for java.net FileNameMap getContentTypeFor
public String getContentTypeFor(String fileName);
From source file:org.etudes.jforum.view.admin.ImportExportAction.java
/** * process attachments/*w w w . ja va2s . c o m*/ * * @param eleRes * @param forum * @param document * @param unZippedDirPath * @param postId * @throws Exception */ /*private void processAttachments(Element eleRes, Forum forum, Document document, String unZippedDirPath, int postId) throws Exception { int total_files = 0; long totalSize = 0; int userId = SessionFacade.getUserSession().getUserId(); AttachmentDAO am = DataAccessDriver.getInstance().newAttachmentDAO(); Map extensions = am.extensionsForSecurity(); // resource elements - <file> List resElements = eleRes.elements(); Attribute resHrefAttr = eleRes.attribute("href"); for (Iterator iter = resElements.iterator(); iter.hasNext();) { Element element = (Element) iter.next(); if (element.getQualifiedName().equalsIgnoreCase("file")) { Attribute hrefAttr = element.attribute("href"); // if (total_files >= SystemGlobals.getIntValue(ConfigKeys.ATTACHMENTS_MAX_POST)) if (total_files >= SakaiSystemGlobals.getIntValue(ConfigKeys.ATTACHMENTS_MAX_POST)) { if (logger.isWarnEnabled()) logger.warn("Total number of files attached exceeding maximum " + "allowed.... for site : " + ToolManager.getCurrentPlacement().getContext() + " - and for forum : " + forum.getName()); return; } String fileext = hrefAttr.getValue().trim().substring(hrefAttr.getValue().trim().lastIndexOf('.') + 1); // if (!fileext.equalsIgnoreCase("html")){ if (!hrefAttr.getValue().trim().equalsIgnoreCase(resHrefAttr.getValue().trim())) { // create as attachment try { // if (logger.isDebugEnabled()) // logger.debug("extensions.entrySet() : "+ // extensions.entrySet()); // Check if the extension is allowed if (extensions.containsKey(fileext)) { if (!((Boolean) extensions.get(fileext)).booleanValue()) { throw new BadExtensionException(I18n.getMessage("Attachments.badExtension", new String[] { fileext })); } } else { if (logger.isWarnEnabled()) logger.warn("site - " + ToolManager.getCurrentPlacement().getContext() + " - Attachment with this extension '" + fileext + "' is not allowed "); continue; } } catch (BadExtensionException e) { if (logger.isWarnEnabled()) logger.warn("site - " + ToolManager.getCurrentPlacement().getContext() + " - " + e.toString()); continue; } Attachment a = new Attachment(); a.setUserId(userId); AttachmentInfo info = new AttachmentInfo(); File attachmentFile = new File(unZippedDirPath + File.separator + hrefAttr.getValue()); if (!attachmentFile.exists()) { if (logger.isErrorEnabled()) logger.error("Error while import for site - " + ToolManager.getCurrentPlacement().getContext() + " - Attachment : " + attachmentFile.getAbsolutePath() + " doesn't exist."); continue; } try { // Check for total attachments size limit // long quotaSize = SystemGlobals.getIntValue(ConfigKeys.ATTACHMENTS_QUOTA_LIMIT) * 1024 * 1024; long quotaSize = SakaiSystemGlobals.getIntValue(ConfigKeys.ATTACHMENTS_QUOTA_LIMIT) * 1024 * 1024; totalSize = totalSize + attachmentFile.length(); if (quotaSize < totalSize) { throw new AttachmentSizeTooBigException(I18n.getMessage("Attachments.tooBig", new Integer[] { new Integer((int) quotaSize / 1024), new Integer((int) totalSize / 1024) })); } } catch (AttachmentSizeTooBigException e) { if (logger.isWarnEnabled()) logger.warn("site - " + ToolManager.getCurrentPlacement().getContext() + " - " + e.toString()); break; } info.setFilesize(attachmentFile.length()); info.setComment(""); info.setMimetype(""); // description List genElements = element.selectNodes("./imsmd:lom/imsmd:general"); if (genElements != null && genElements.size() > 0) { Element generalElement = (Element) genElements.get(0); Element descElement = generalElement.element("description"); String description = descElement.selectSingleNode(".//imsmd:langstring").getText(); if (description != null) info.setComment(description.trim()); } String filename = null; String hrefVal = hrefAttr.getValue().trim(); if (hrefVal.lastIndexOf('/') != -1) filename = hrefVal.substring(hrefVal.lastIndexOf('/') + 1); else if (hrefVal.lastIndexOf('\\') != -1) filename = hrefVal.substring(hrefVal.lastIndexOf('\\') + 1); if (filename.startsWith("post_")) filename = filename.substring(filename.indexOf("_", 5) + 1); info.setRealFilename(filename); info.setUploadTimeInMillis(System.currentTimeMillis()); AttachmentExtension ext = am.selectExtension(fileext); info.setExtension(ext); String savePath = makeStoreFilename(info); info.setPhysicalFilename(savePath); a.setInfo(info); a.setPostId(postId); // String path = SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_STORE_DIR) + "/" + a.getInfo().getPhysicalFilename(); String path = SakaiSystemGlobals.getValue(ConfigKeys.ATTACHMENTS_STORE_DIR) + "/" + a.getInfo().getPhysicalFilename(); am.addAttachment(a); saveAttachmentFile(path, attachmentFile); total_files++; } } } }*/ protected void processAttachments(Element eleRes, Document document, String unZippedDirPath, org.etudes.api.app.jforum.Post post, JForumPostService jforumPostService) throws Exception { // resource elements - <file> List resElements = eleRes.elements(); Attribute resHrefAttr = eleRes.attribute("href"); for (Iterator iter = resElements.iterator(); iter.hasNext();) { Element element = (Element) iter.next(); if (element.getQualifiedName().equalsIgnoreCase("file")) { Attribute hrefAttr = element.attribute("href"); String fileext = hrefAttr.getValue().trim() .substring(hrefAttr.getValue().trim().lastIndexOf('.') + 1); if (!hrefAttr.getValue().trim().equalsIgnoreCase(resHrefAttr.getValue().trim())) { File attachmentFile = new File(unZippedDirPath + File.separator + hrefAttr.getValue()); if (!attachmentFile.exists()) { if (logger.isErrorEnabled()) { logger.error("Error while import for site - " + ToolManager.getCurrentPlacement().getContext() + " - Attachment : " + attachmentFile.getAbsolutePath() + " doesn't exist."); } continue; } // String fileName = null; String contentType = null; String comments = null; byte[] fileContent = null; // description List genElements = element.selectNodes("./imsmd:lom/imsmd:general"); if (genElements != null && genElements.size() > 0) { Element generalElement = (Element) genElements.get(0); Element descElement = generalElement.element("description"); String description = descElement.selectSingleNode(".//imsmd:langstring").getText(); if (description != null) { comments = description.trim(); } } String fileName = null; String hrefVal = hrefAttr.getValue().trim(); if (hrefVal.lastIndexOf('/') != -1) fileName = hrefVal.substring(hrefVal.lastIndexOf('/') + 1); else if (hrefVal.lastIndexOf('\\') != -1) fileName = hrefVal.substring(hrefVal.lastIndexOf('\\') + 1); if (fileName.startsWith("post_")) fileName = fileName.substring(fileName.indexOf("_", 5) + 1); //contentType = new MimetypesFileTypeMap().getContentType(attachmentFile); FileNameMap fileNameMap = URLConnection.getFileNameMap(); contentType = fileNameMap.getContentTypeFor(fileName); fileContent = getBytesFromFile(attachmentFile); org.etudes.api.app.jforum.Attachment attachment = jforumPostService.newAttachment(fileName, contentType, comments, fileContent); if (attachment != null) { post.getAttachments().add(attachment); post.setHasAttachments(true); } } } } }