|
| 1 | +package net.liuxuan.SprKi.controller.videos; |
| 2 | + |
| 3 | +import net.liuxuan.SprKi.entity.CMSVideo; |
| 4 | +import net.liuxuan.SprKi.service.CMSVideoService; |
| 5 | +import net.liuxuan.spring.Helper.SystemHelper; |
| 6 | +import org.dom4j.Document; |
| 7 | +import org.dom4j.DocumentException; |
| 8 | +import org.dom4j.DocumentHelper; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.security.access.prepost.PreAuthorize; |
| 13 | +import org.springframework.stereotype.Controller; |
| 14 | +import org.springframework.util.FileCopyUtils; |
| 15 | +import org.springframework.web.bind.annotation.PathVariable; |
| 16 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 17 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 18 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 19 | + |
| 20 | +import javax.servlet.ServletOutputStream; |
| 21 | +import javax.servlet.http.HttpServletRequest; |
| 22 | +import javax.servlet.http.HttpServletResponse; |
| 23 | +import java.io.*; |
| 24 | +import java.net.URLConnection; |
| 25 | +import java.nio.charset.Charset; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +/** |
| 30 | + * Copyright (c) 2010-2016. by Liuxuan All rights reserved. <br/> |
| 31 | + * *************************************************************************** |
| 32 | + * 源文件名: net.liuxuan.SprKi.controller.ticket.TicketController |
| 33 | + * 功能: |
| 34 | + * 版本: @version 1.0 |
| 35 | + * 编制日期: 2017/9/30 11:27 |
| 36 | + * 修改历史: (主要历史变动原因及说明) |
| 37 | + * YYYY-MM-DD | Author | Change Description |
| 38 | + * 2017/9/30 | Moses | Created |
| 39 | + */ |
| 40 | +@Controller |
| 41 | +@PreAuthorize("hasRole('ROLE_USER')") |
| 42 | +public class VideoController { |
| 43 | + private static Logger log = LoggerFactory.getLogger(VideoController.class); |
| 44 | + |
| 45 | + @Autowired |
| 46 | + CMSVideoService cmsVideoService; |
| 47 | + |
| 48 | + |
| 49 | + @RequestMapping(value = "/video/list", method = RequestMethod.GET) |
| 50 | + public String getVideoList(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) { |
| 51 | + List<CMSVideo> allContents = cmsVideoService.getAllCMSVideo(); |
| 52 | + log.info("video list size is {}", allContents.size()); |
| 53 | + model.put("alllist", allContents); |
| 54 | + return "video/video_list"; |
| 55 | + } |
| 56 | + |
| 57 | + @RequestMapping(value = "/video/{id}", method = RequestMethod.GET) |
| 58 | + public String getVideoById(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) { |
| 59 | + |
| 60 | + CMSVideo content = cmsVideoService.findCMSVideoById(id); |
| 61 | + model.put("content", content); |
| 62 | + return "video/video_show"; |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + @RequestMapping(value = "/video/videoxml/{id}", method = RequestMethod.GET) |
| 67 | + public @ResponseBody |
| 68 | + String getVideoXml(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response) { |
| 69 | + |
| 70 | + String originxml = |
| 71 | + "<vcastr>" + |
| 72 | + //channel |
| 73 | + "<channel> <item>" + |
| 74 | + "<source>http://vcastr.ruochi.com/video/happy_feet.flv</source>" + |
| 75 | + "<duration></duration>" + //对应影片的总时间, 单位是秒,由于有些影片在制作过程中时间信息丢失,可以通过这里来设置 |
| 76 | + "<title></title>" + //对应影片的标题 |
| 77 | + "</item> </channel>" + |
| 78 | + //config |
| 79 | + "<config>" + |
| 80 | + "<bufferTime>4</bufferTime>" + //在缓存区影片的时间,单位是秒 |
| 81 | + "<contralPanelAlpha>0.75</contralPanelAlpha>" + //控制栏的透明度,在 0 — 1 之间 |
| 82 | + "<controlPanelBgColor>0xff6600</controlPanelBgColor>" + //控制栏背景的颜色 |
| 83 | + "<controlPanelBtnColor>0xffffff</controlPanelBtnColor>" + //控制按钮的颜色 |
| 84 | + "<contralPanelBtnGlowColro>0xffff00</contralPanelBtnGlowColro>" + //控制按钮光晕的颜色 |
| 85 | + //控制栏的显示模式, |
| 86 | + // "float": 默认的浮动模式,鼠标移除播放器就会隐藏掉 |
| 87 | + // "normal": 正常模式,任何时候都在影片的下部浮动显示 |
| 88 | + // "bottom": 下部模式,不浮动在影片之上,而是显示在影片下方 |
| 89 | + // "none": 不显示播放器控制栏 |
| 90 | + "<controlPanelMode>float</controlPanelMode>" + |
| 91 | + "<defautVolume>0.8</defautVolume>" +//默认的声音大小,最大为1,最小为0 |
| 92 | + "<isAutoPlay>true</isAutoPlay>" +//是否影片自动开始播放,默认是true |
| 93 | + "<isLoadBegin>true</isLoadBegin>" + //是否一开始就读取影片,默认是true |
| 94 | + "<isRepeat>false</isRepeat>" +//是否循环播放影片,默然是false |
| 95 | + "<isShowAbout>true</isShowAbout>" + // 是否显示关于信息 |
| 96 | + // 影片放缩模式: |
| 97 | + //"showAll": 可以看到全部影片,保持比例,可能上下或者左右 |
| 98 | + //"exactFil": 放缩影片到播放器的尺寸,可能比例失调 |
| 99 | + //"noScale": 影片的原始尺寸,无放缩 |
| 100 | + //"noBorder": 影片充满播放器,保持比例,可能会被裁剪 |
| 101 | + "<scaleMode>showAll</scaleMode>" + |
| 102 | + "</config>" + |
| 103 | + //plugIns |
| 104 | + "<plugIns>" + |
| 105 | + "<logoPlugIn>" + |
| 106 | + "<url>/ui/mosesadd/vcastr/logoPlugIn.swf</url>" + |
| 107 | + "<logoText>www.ruochi.com</logoText>" + |
| 108 | + "<logoTextAlpha>0.75</logoTextAlpha>" + |
| 109 | + "<logoTextFontSize>30</logoTextFontSize>" + |
| 110 | + "<logoTextLink>http://192.168.7.202</logoTextLink>" + |
| 111 | + "<logoTextColor>0xffffff</logoTextColor>" + |
| 112 | + "<textMargin>20 20 auto auto</textMargin>" + |
| 113 | + "</logoPlugIn>" + |
| 114 | + //end of plugIns |
| 115 | + "</plugIns>" + |
| 116 | + "</vcastr>"; |
| 117 | + |
| 118 | + |
| 119 | + try { |
| 120 | + Document document = DocumentHelper.parseText(originxml); |
| 121 | + document.getRootElement().element("channel").element("item").element("source").setText("/video/getVideo/" + id); |
| 122 | + //Not Support Chinese |
| 123 | + String username = SystemHelper.GetCurrentUserName(); |
| 124 | + document.getRootElement().element("plugIns").element("logoPlugIn").element("logoText").setText(username); |
| 125 | + return document.asXML(); |
| 126 | + } catch (DocumentException e) { |
| 127 | + e.printStackTrace(); |
| 128 | + } |
| 129 | + return ""; |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + @RequestMapping(value = "/video/getVideo/{id}", method = RequestMethod.GET) |
| 134 | +// @ResponseBody |
| 135 | + public void getMediaFile(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) throws IOException { |
| 136 | + |
| 137 | + CMSVideo content = cmsVideoService.findCMSVideoById(id); |
| 138 | + if (content == null) { |
| 139 | + //todo do sth |
| 140 | + } |
| 141 | + String path = content.getCmsVideoFilepath(); |
| 142 | +// Path p = Paths.get(path); |
| 143 | + |
| 144 | + File f = new File(path); |
| 145 | + String rtn; |
| 146 | + if (f.exists() && (!f.isDirectory())) { |
| 147 | + rtn = path; |
| 148 | + } else { |
| 149 | + String errorMessage = "Sorry. The file you are looking for does not exist"; |
| 150 | +// System.out.println(errorMessage); |
| 151 | + ServletOutputStream outputStream = response.getOutputStream(); |
| 152 | + outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8"))); |
| 153 | + outputStream.close(); |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + String mimeType = URLConnection.guessContentTypeFromName(f.getName()); |
| 158 | + if (mimeType == null) { |
| 159 | + System.out.println("mimetype is not detectable, will take default"); |
| 160 | + mimeType = "application/octet-stream"; |
| 161 | + } |
| 162 | + |
| 163 | + System.out.println("mimetype : " + mimeType); |
| 164 | +// mimeType = "application/force-download"; |
| 165 | + response.setContentType(mimeType); |
| 166 | + /* "Content-Disposition : inline" will show viewable types [like images/text/pdf/anything viewable by browser] right on browser |
| 167 | + while others(zip e.g) will be directly downloaded [may provide save as popup, based on your browser setting.]*/ |
| 168 | + response.setHeader("Content-Disposition", String.format("inline; filename=\"" + f.getName() + "\"")); |
| 169 | +// response.setHeader("Content-Disposition", String.format("attachment; filename=\"" + f.getName() +"\"")); |
| 170 | + |
| 171 | + |
| 172 | + /* "Content-Disposition : attachment" will be directly download, may provide save as popup, based on your browser setting*/ |
| 173 | + //response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName())); |
| 174 | + |
| 175 | + response.setContentLength((int) f.length()); |
| 176 | + |
| 177 | + InputStream inputStream = new BufferedInputStream(new FileInputStream(f)); |
| 178 | + |
| 179 | + //Copy bytes from source to destination(outputstream in this example), closes both streams. |
| 180 | + FileCopyUtils.copy(inputStream, response.getOutputStream()); |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments