From: <fg...@us...> - 2009-04-29 15:17:15
|
Revision: 1168 http://openutils.svn.sourceforge.net/openutils/?rev=1168&view=rev Author: fgiust Date: 2009-04-29 15:17:11 +0000 (Wed, 29 Apr 2009) Log Message: ----------- new MediaLoadUtils Modified Paths: -------------- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/pages/MediaLoadZipFilePage.java trunk/openutils-mgnlmedia/src/site/changes/changes.xml Added Paths: ----------- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/MediaLoadUtils.java Modified: trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/pages/MediaLoadZipFilePage.java =================================================================== --- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/pages/MediaLoadZipFilePage.java 2009-04-29 14:51:15 UTC (rev 1167) +++ trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/pages/MediaLoadZipFilePage.java 2009-04-29 15:17:11 UTC (rev 1168) @@ -18,34 +18,24 @@ package net.sourceforge.openutils.mgnlmedia.media.pages; import info.magnolia.cms.beans.runtime.Document; -import info.magnolia.cms.core.Content; import info.magnolia.cms.core.HierarchyManager; -import info.magnolia.cms.core.ItemType; -import info.magnolia.cms.core.NodeData; -import info.magnolia.cms.core.Path; -import info.magnolia.cms.i18n.Messages; import info.magnolia.cms.security.AccessDeniedException; -import info.magnolia.cms.util.NodeDataUtil; import info.magnolia.context.MgnlContext; -import info.magnolia.module.admininterface.TemplatedMVCHandler; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -import java.util.zip.ZipInputStream; import javax.jcr.RepositoryException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import net.sourceforge.openutils.mgnlmedia.media.configuration.MediaConfigurationManager; -import net.sourceforge.openutils.mgnlmedia.media.configuration.MediaTypeConfiguration; import net.sourceforge.openutils.mgnlmedia.media.lifecycle.MediaModuleLifecycle; -import net.sourceforge.openutils.mgnlmedia.media.types.MediaTypeHandler; +import net.sourceforge.openutils.mgnlmedia.media.utils.MediaLoadUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; @@ -73,13 +63,18 @@ public String loadZip() { + + InputStream zipStream = null; try { HierarchyManager mgr = MgnlContext.getSystemContext().getHierarchyManager(MediaModuleLifecycle.REPO); File temp = File.createTempFile("zipmedia", ".zip"); FileOutputStream fos = new FileOutputStream(temp); - IOUtils.copy(zipFile.getStream(), fos); - fos.close(); + + zipStream = zipFile.getStream(); + IOUtils.copy(zipStream, fos); + + IOUtils.closeQuietly(fos); ZipFile zip = new ZipFile(temp); Enumeration< ? extends ZipEntry> entries = zip.entries(); while (entries.hasMoreElements()) @@ -93,48 +88,16 @@ path = "/" + path; } - if (entry.isDirectory()) + if (!entry.isDirectory()) { - getOrCreateFullPath(mgr, path); - } - else - { + InputStream inputStream = zip.getInputStream(entry); + String parent = StringUtils.substringBeforeLast(path, "/"); String filename = StringUtils.substringAfterLast(path, "/"); - if (StringUtils.isEmpty(parent)) - { - parent = "/"; - } - String extension = StringUtils.substringAfterLast(filename, "."); - String cleanFilename = StringUtils.substringBeforeLast(filename, "."); - MediaTypeConfiguration mtc = MediaConfigurationManager.getMediaHandlerFromExtension(extension); - if (mtc != null) - { - Content parentNode = mgr.getContent(parent); + MediaLoadUtils.loadEntry(inputStream, StringUtils.defaultIfEmpty(parent, "/"), filename, false); - Content media = mgr.createContent( - parent, - Path.getUniqueLabel(parentNode, cleanFilename), - MediaConfigurationManager.MEDIA.getSystemName()); - - NodeData nd = NodeDataUtil.getOrCreate(media, "type"); - nd.setValue(mtc.getName()); - - mgr.save(); - - File f = File.createTempFile("entry", extension); - FileOutputStream fTemp = new FileOutputStream(f); - - IOUtils.copy(zip.getInputStream(entry), fTemp); - - fTemp.close(); - - mtc.getHandler().saveFromZipFile(media, f, cleanFilename, extension); - - mgr.save(); - } } } } @@ -150,6 +113,10 @@ { } + finally + { + IOUtils.closeQuietly(zipStream); + } return this.show(); } @@ -172,27 +139,4 @@ this.zipFile = zipFile; } - private Content getOrCreateFullPath(HierarchyManager mgr, String path) throws RepositoryException - { - try - { - return mgr.getContent(path); - } - catch (RepositoryException ex) - { - String parent = StringUtils.substringBeforeLast(path, "/"); - String label = StringUtils.substringAfterLast(path, "/"); - if (!StringUtils.isEmpty(parent)) - { - getOrCreateFullPath(mgr, parent); - } - else - { - parent = "/"; - } - - return mgr.createContent(parent, label, ItemType.CONTENT.getSystemName()); - } - } - } Added: trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/MediaLoadUtils.java =================================================================== --- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/MediaLoadUtils.java (rev 0) +++ trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/MediaLoadUtils.java 2009-04-29 15:17:11 UTC (rev 1168) @@ -0,0 +1,142 @@ +/** + * Copyright Openmind http://www.openmindonline.it + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package net.sourceforge.openutils.mgnlmedia.media.utils; + +import info.magnolia.cms.core.Content; +import info.magnolia.cms.core.HierarchyManager; +import info.magnolia.cms.core.NodeData; +import info.magnolia.cms.core.Path; +import info.magnolia.cms.util.ContentUtil; +import info.magnolia.cms.util.NodeDataUtil; +import info.magnolia.context.MgnlContext; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Calendar; + +import javax.jcr.RepositoryException; + +import net.sourceforge.openutils.mgnlmedia.media.configuration.MediaConfigurationManager; +import net.sourceforge.openutils.mgnlmedia.media.configuration.MediaTypeConfiguration; +import net.sourceforge.openutils.mgnlmedia.media.lifecycle.MediaModuleLifecycle; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * @author fgiust + * @version $Id$ + */ +public class MediaLoadUtils +{ + + /** + * Logger. + */ + private static Logger log = LoggerFactory.getLogger(MediaLoadUtils.class); + + public static Content loadEntry(InputStream inputStream, String parent, String filename, boolean overwrite) + throws RepositoryException, IOException + { + + log.debug("loading image {} {}", parent, filename); + + HierarchyManager mgr = MgnlContext.getSystemContext().getHierarchyManager(MediaModuleLifecycle.REPO); + + String extension = StringUtils.substringAfterLast(filename, "."); + String cleanFilename = StringUtils.substringBeforeLast(filename, "."); + MediaTypeConfiguration mtc = MediaConfigurationManager.getMediaHandlerFromExtension(extension); + + if (mtc != null) + { + Content parentNode = getOrCreateFullPath(mgr, parent); + + if (overwrite) + { + Content existing = parentNode.getChildByName(cleanFilename); + if (existing != null) + { + existing.delete(); + mgr.save(); + } + } + + Content media = mgr.createContent( + parent, + Path.getUniqueLabel(parentNode, cleanFilename), + MediaConfigurationManager.MEDIA.getSystemName()); + + NodeData nd = NodeDataUtil.getOrCreate(media, "type"); + nd.setValue(mtc.getName()); + + nd = NodeDataUtil.getOrCreate(media, "creator"); + if (StringUtils.isEmpty(nd.getString())) + { + nd.setValue(MgnlContext.getUser().getName()); + } + + nd = NodeDataUtil.getOrCreate(media, "creationDate"); + if (nd.getDate() == null) + { + nd.setValue(Calendar.getInstance()); + } + nd.setValue(MgnlContext.getUser().getName()); + + nd = NodeDataUtil.getOrCreate(media, "modificationDate"); + nd.setValue(Calendar.getInstance()); + + mgr.save(); + + File f = File.createTempFile("entry", extension); + FileOutputStream fTemp = new FileOutputStream(f); + + IOUtils.copy(inputStream, fTemp); + + IOUtils.closeQuietly(fTemp); + + mtc.getHandler().saveFromZipFile(media, f, cleanFilename, extension); + + mgr.save(); + return media; + } + return null; + } + + private static Content getOrCreateFullPath(HierarchyManager mgr, String path) throws RepositoryException + { + String[] contentNodeNames = path.split("/"); + Content currContent = mgr.getRoot(); + for (String contentNodeName : contentNodeNames) + { + if (StringUtils.isNotEmpty(contentNodeName)) + { + currContent = ContentUtil.getOrCreateContent( + currContent, + contentNodeName, + MediaConfigurationManager.FOLDER); + } + } + return currContent; + + } +} Property changes on: trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/MediaLoadUtils.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/openutils-mgnlmedia/src/site/changes/changes.xml =================================================================== --- trunk/openutils-mgnlmedia/src/site/changes/changes.xml 2009-04-29 14:51:15 UTC (rev 1167) +++ trunk/openutils-mgnlmedia/src/site/changes/changes.xml 2009-04-29 15:17:11 UTC (rev 1168) @@ -11,6 +11,8 @@ <release version="4.0-b4" date="in svn" description=""> <action type="update" dev="fgiust">YouTubeVideoTypeHandler modified in order to work with generic external urls (not only youtube)</action> + <action type="update" dev="fgiust">new utility class net.sourceforge.openutils.mgnlmedia.media.utils.MediaLoadUtils + </action> </release> <release version="4.0-b3" date="2009-04-23" description=""> <action type="update" dev="fgiust">Update openutils-mgnltasks dependency to 4.0</action> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |