Selaa lähdekoodia

视频上传文件路径修改 远程视频删除

limin 7 vuotta sitten
vanhempi
commit
39612d29d4

+ 45 - 8
src/main/java/com/goafanti/admin/controller/AdminVideoApiController.java

@@ -56,9 +56,13 @@ public class AdminVideoApiController extends CertifyApiController {
 	private AdminVideoService adminVideoService;
 
 	@Value(value = "${ffmpeg.path}")
-	public String ffmpegPath = null;
+	private String ffmpegPath = null;
 	@Value(value = "${ffprobe.path}")
-	public String ffprobePath = null;
+	private String ffprobePath = null;
+	@Value(value = "${video.path}")
+	private String videoPath = null;
+	
+	
 
 	// 上传视频只做MP4格式的
 	@RequestMapping(value = "/upload", method = RequestMethod.POST)
@@ -68,7 +72,7 @@ public class AdminVideoApiController extends CertifyApiController {
 		AttachmentType attachmentType = AttachmentType.getField(sign);
 		if (attachmentType == AttachmentType.VIDEO) {
 			// 先将视频存在本地文件中
-			String filename = handleVideoFiles(res, req);
+			String filename = handleVideoFiles(res, req, videoPath);
 			res.setData(filename);
 		} else if (attachmentType == AttachmentType.VIDEO_COVER) {
 			String picturebase = req.getParameter("picturebase");
@@ -86,13 +90,13 @@ public class AdminVideoApiController extends CertifyApiController {
 		return res;
 	}
 
-	private String createFileName() {
+	private String createFileName(String local) {
 		// 年月日
 		Calendar now = Calendar.getInstance();
 		int year = now.get(Calendar.YEAR);
 		int month = now.get(Calendar.MONTH) + 1;
 		int day = now.get(Calendar.DAY_OF_MONTH);
-		String dir = "/" + year + "/" + month + "/" + day;
+		String dir = "/" + local + "/" + year + "/" + month + "/" + day;
 		return dir;
 	}
 
@@ -126,12 +130,23 @@ public class AdminVideoApiController extends CertifyApiController {
 					VideoFields.getFieldDesc(bindingResult.getFieldError().getField())));
 			return result;
 		}
+		if(StringUtils.isBlank(video.getName())){
+			result.getError().add(
+					buildError( "视频名称不能是空", "视频名称不能是空"));
+			return result;
+		}
+		//名称重名限制
+		if(adminVideoService.nameCheck(video.getName().trim()) > 0){
+			result.getError().add(
+					buildError(ErrorConstants.PARAM_ERROR, "", "视频名称重复不能新增"));
+			return result;
+		}
 		
 		String filename = video.getUrl();
 		
 		// 生成保存到数据库的url
 		String output = System.nanoTime() + ".mp4";
-		video.setUrl(createFileName() + "/" + output);
+		video.setUrl(createFileName(videoPath) + "/" + output);
 		
 		video.setTranscoding(1);
 		// 保存到数据库
@@ -164,13 +179,32 @@ public class AdminVideoApiController extends CertifyApiController {
 					buildError(ErrorConstants.PARAM_ERROR, "", "视频id不存在"));
 			return result;
 		}
+		if(null != video.getName() && "".equals(video.getName().trim())){
+			result.getError().add(
+					buildError( "视频名称不能是空", "视频名称不能是空"));
+			return result;
+		}
+		//如果是删除视频
+		if(null != video.getStatus() && video.getStatus() == 3){
+			if(oldVideo.getStatus() == 1){
+				result.getError().add(
+						buildError( "视频已经发布不能删除", "视频已经发布不能删除"));
+				return result;
+			}else{
+				deleteFtpVideo(oldVideo.getUrl());
+			}
+		}
 		if (StringUtils.isNotBlank(video.getUrl()) && !video.getUrl().equals(oldVideo.getUrl())) { // 如果修改了视频
 			video.setTranscoding(1);
 			// 压缩视频并将视频
 			String filename = video.getUrl();
 			String output = System.nanoTime() + ".mp4";
-			video.setUrl(createFileName() + "/" + output);
+			video.setUrl(createFileName(videoPath) + "/" + output);
 			videoChange(filename, output, video.getUrl());
+			
+			//删除原来远程的视频
+			deleteFtpVideo(oldVideo.getUrl());
+			
 		}else{
 			video.setUrl(null);
 		}
@@ -179,7 +213,10 @@ public class AdminVideoApiController extends CertifyApiController {
 		return result;
 	}
 	
-	
+	private Boolean deleteFtpVideo(String url){
+		FtpUtils ftp = new FtpUtils();
+		return ftp.deleteFile("",url);
+	}
 
 	// 将文件转码
 	private String videoChange(String input, String output, String url) throws IOException,

+ 2 - 0
src/main/java/com/goafanti/admin/service/AdminVideoService.java

@@ -17,4 +17,6 @@ public interface AdminVideoService {
 	
 	AdminVideoBo getIndexVideo(String summary);
 	
+	int nameCheck(String name);
+	
 }

+ 5 - 0
src/main/java/com/goafanti/admin/service/impl/AdminVideoServiceImpl.java

@@ -70,6 +70,11 @@ public class AdminVideoServiceImpl extends BaseMybatisDao<JtVideoMapper> impleme
 		return jtVideoMapper.getIndexVideo(summary);
 	}
 
+	@Override
+	public int nameCheck(String name) {
+		return jtVideoMapper.nameCheck(name);
+	}
+
 	
 
 }

+ 6 - 2
src/main/java/com/goafanti/common/controller/BaseApiController.java

@@ -257,10 +257,14 @@ public class BaseApiController extends BaseController {
 		return fileName;
 	}
 	
-	protected String handleVideoFiles(Result res, HttpServletRequest req) {
+	protected String handleVideoFiles(Result res, HttpServletRequest req,String local) {
 		List<MultipartFile> files = getFiles(req);
 		MultipartFile mf = files.get(0);
-		String fileName = mf.getName() + System.nanoTime() + ".mp4" ;
+		File fd = new File(local);
+		if(!fd.exists()){
+			fd.mkdirs();
+		}
+		String fileName = local + "/" + mf.getName() + System.nanoTime() + ".mp4" ;
 		if (!files.isEmpty()) {
 			try {
 				File f = new File(fileName);

+ 2 - 0
src/main/java/com/goafanti/common/dao/JtVideoMapper.java

@@ -83,4 +83,6 @@ public interface JtVideoMapper {
 	int updateByUrl(JtVideo video);
 	
 	AdminVideoBo getIndexVideo(String summary);
+	
+	int nameCheck(String name);
 }

+ 4 - 0
src/main/java/com/goafanti/common/mapper/JtVideoMapper.xml

@@ -519,4 +519,8 @@
     order by release_time desc
     limit 1
   </select>
+  <!-- 检查重复名称 -->
+  <select id="nameCheck" parameterType="java.lang.String" resultType="java.lang.Integer">
+  	select count(*) from jt_video where status &lt; 3 and name = #{_parameter,jdbcType=VARCHAR}
+  </select>
 </mapper>

+ 2 - 0
src/main/resources/props/config_local.properties

@@ -85,4 +85,6 @@ jiguang.pushUrl=https://api.jpush.cn/v3/push
 ffmpeg.path=E\:\\mytools\\ffmpeg-20181028-bdfd2e3-win64-static\\bin\\ffmpeg.exe
 ffprobe.path=E\:\\mytools\\ffmpeg-20181028-bdfd2e3-win64-static\\bin\\ffprobe.exe
 
+video.path=local
+
 collect_flag=true

+ 1 - 1
src/main/resources/props/config_test.properties

@@ -84,6 +84,6 @@ jiguang.pushUrl=https://api.jpush.cn/v3/push
 ffmpeg.path=/usr/bin/ffmpeg
 ffprobe.path=/usr/bin/ffprobe
 
-
+video.path=test
 
 collect_flag=true