From: <die...@us...> - 2010-02-17 19:56:19
|
Revision: 1972 http://openutils.svn.sourceforge.net/openutils/?rev=1972&view=rev Author: diego_schivo Date: 2010-02-17 19:56:11 +0000 (Wed, 17 Feb 2010) Log Message: ----------- MEDIA-99 Copy the youtube preview image to the repository, when no additional preview image is added Modified Paths: -------------- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/types/impl/ExternalVideoTypeHandler.java Modified: trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/types/impl/ExternalVideoTypeHandler.java =================================================================== --- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/types/impl/ExternalVideoTypeHandler.java 2010-02-17 09:50:56 UTC (rev 1971) +++ trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/types/impl/ExternalVideoTypeHandler.java 2010-02-17 19:56:11 UTC (rev 1972) @@ -19,10 +19,19 @@ package net.sourceforge.openutils.mgnlmedia.media.types.impl; +import info.magnolia.cms.beans.runtime.Document; import info.magnolia.cms.beans.runtime.MultipartForm; import info.magnolia.cms.core.Content; +import info.magnolia.cms.core.Path; +import info.magnolia.module.admininterface.SaveHandlerImpl; +import java.io.BufferedReader; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -33,10 +42,12 @@ import javax.jcr.RepositoryException; import javax.servlet.http.HttpServletRequest; +import net.sourceforge.openutils.mgnlmedia.media.tags.el.MediaEl; import net.sourceforge.openutils.mgnlmedia.media.types.externals.ExternalVideoSupport; import net.sourceforge.openutils.mgnlmedia.media.utils.FLVMedataUtils; import net.sourceforge.openutils.mgnlmedia.media.utils.FLVMedataUtils.FLVMetaData; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.ClassUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; @@ -62,6 +73,21 @@ private List<ExternalVideoSupport> videoSupportHandlers = new ArrayList<ExternalVideoSupport>(); /** + * The directory for temporary storage of files. + */ + private File tempDir; + + /** + * {@inheritDoc} + */ + @Override + public void init(Content typeDefinitionNode) + { + super.init(typeDefinitionNode); + this.tempDir = new File(Path.getTempDirectoryPath()); + } + + /** * Returns the list of configured videoSupportHandlers. * @return the list of configured videoSupportHandlers. */ @@ -200,4 +226,69 @@ } } + /** + * {@inheritDoc} + */ + @Override + public boolean onPostSave(Content media) + { + if (StringUtils.isEmpty(MediaEl.thumbnail(media))) + { + String downloadUrl = getUrl(media); + for (ExternalVideoSupport external : videoSupportHandlers) + { + if (external.isEnabled() && external.canHandle(downloadUrl)) + { + String previewUrl = external.getPreviewUrl(downloadUrl); + if (StringUtils.isNotBlank(previewUrl)) + { + BufferedReader in = null; + try + { + URL url = new URL(previewUrl); + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); + if (httpConn.getResponseCode() == 200) + { + in = new BufferedReader(new InputStreamReader(httpConn.getInputStream())); + File file = File.createTempFile("external-video", "youtube-img", tempDir); + FileOutputStream fout = null; + try + { + fout = new FileOutputStream(file); + IOUtils.copy(in, fout); + } + finally + { + IOUtils.closeQuietly(fout); + } + Document doc = new Document(file, httpConn.getContentType()); + try + { + SaveHandlerImpl.saveDocument(media, doc, PREVIEW_NODEDATA_NAME, "preview", null); + } + catch (RepositoryException e) + { + log.error(e.getMessage(), e); + } + } + } + catch (MalformedURLException e) + { + log.error(e.getMessage(), e); + } + catch (IOException e) + { + log.error(e.getMessage(), e); + } + finally + { + IOUtils.closeQuietly(in); + } + } + break; + } + } + } + return super.onPostSave(media); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |