|
@@ -1,16 +1,13 @@
|
|
|
package com.sooka.common.upload;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.sooka.common.constant.CmsConst;
|
|
|
+import com.sooka.common.exception.ApiException;
|
|
|
import com.sooka.common.upload.bean.UploadBean;
|
|
|
-import com.sooka.common.utils.ControllerUtil;
|
|
|
-import com.sooka.common.utils.PathUtil;
|
|
|
-import com.sooka.common.utils.QiniuUtil;
|
|
|
-import com.sooka.common.utils.StrUtil;
|
|
|
+import com.sooka.common.utils.*;
|
|
|
import com.sooka.module.web.system.service.AttachmentService;
|
|
|
import com.sooka.module.web.system.vo.UserVo;
|
|
|
import com.sooka.mybatis.model.TSysAttachment;
|
|
|
-import com.sooka.common.constant.CmsConst;
|
|
|
-import com.sooka.common.exception.ApiException;
|
|
|
import jodd.datetime.JDateTime;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -23,7 +20,15 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.BufferedOutputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
import static com.sun.org.apache.xerces.internal.impl.dv.util.Base64.decode;
|
|
@@ -120,6 +125,188 @@ public class UploadComponent {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public UploadBean ftpuploadFile(MultipartFile multipartFile, HttpServletRequest request){
|
|
|
+ TSysAttachment attachment = new TSysAttachment();
|
|
|
+ /** 获取用户会话 **/
|
|
|
+ UserVo userVo = (UserVo)request.getSession().getAttribute(CmsConst.SITE_USER_SESSION_KEY);
|
|
|
+ if(userVo!=null) {
|
|
|
+ attachment.setUserId(userVo.getUserId().toString());
|
|
|
+ attachment.setUsername(userVo.getUsername());
|
|
|
+ }
|
|
|
+ attachment.setUploadDate(new Date());
|
|
|
+ attachment.setUploadIp(ControllerUtil.getRemoteAddress(request));
|
|
|
+ attachment.setFileSize(Float.valueOf(multipartFile.getSize())/1024);
|
|
|
+ attachment.setFileExtname(multipartFile.getContentType());
|
|
|
+ attachment.setFileKey(UUID.randomUUID().toString().replace("-",""));
|
|
|
+ attachment.setOriginalFilename(multipartFile.getOriginalFilename());
|
|
|
+ /*创建uploadBean*/
|
|
|
+ UploadBean result = new UploadBean();
|
|
|
+ String fileType = this.getFileType(attachment.getOriginalFilename());
|
|
|
+ String fileName = this.getFileName(fileType) ;
|
|
|
+ String newName =this.getNewFileName(fileName);
|
|
|
+ FtpHelper helper=null;
|
|
|
+ if (!multipartFile.isEmpty()) {
|
|
|
+ if (!Boolean.parseBoolean(qiniuUpload)) {
|
|
|
+ File file = new File(this.getUploadPath() + newName);
|
|
|
+ /*如果不存在就创建*/
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ //this.writeFile(multipartFile.getBytes(), file);
|
|
|
+ helper = new FtpHelper() {
|
|
|
+ @Override
|
|
|
+ public void complete(JSONResult jsonResult) {
|
|
|
+ if(jsonResult.isOK()){
|
|
|
+ //success
|
|
|
+ }else{
|
|
|
+ jsonResult.getMsg();
|
|
|
+ jsonResult.getStatus();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ helper.uploadFile(multipartFile.getInputStream(), fileName);
|
|
|
+ attachment.setFilePath(newName);
|
|
|
+ attachment.setFileName(fileName);
|
|
|
+ result.setFileUrl(request.getScheme() + "://" + ControllerUtil.getDomain() + "/res/" + attachment.getFileKey() + "." + fileType);
|
|
|
+ attachmentService.save(attachment);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ApiException(e.getMessage());
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ String qiniuFileResult = QiniuUtil.upload(accessKey, secretKey, bucketname, multipartFile);
|
|
|
+ if (!StrUtil.isBlank(qiniuFileResult)) {
|
|
|
+ String fileKey = JSON.parseObject(qiniuFileResult).getString("key");
|
|
|
+ String fileUrl = domain + "/" + fileKey;
|
|
|
+ if (StrUtil.getExtensionName(fileName).equals("jpg") || StrUtil.getExtensionName(fileName).equals("JPG") || StrUtil.getExtensionName(fileName).equals("png") || StrUtil.getExtensionName(fileName).equals("PNG") || StrUtil.getExtensionName(fileName).equals("jpeg") || StrUtil.getExtensionName(fileName).equals("JPEG")) {
|
|
|
+ fileUrl += "?imageslim";
|
|
|
+ }
|
|
|
+ result.setFileUrl(fileUrl);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }else{
|
|
|
+ throw new ApiException("上传文件不能为空!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public UploadBean qpuploadFile(MultipartFile multipartFile, HttpServletRequest request,Path tempDir,String videoFolder) {
|
|
|
+ TSysAttachment attachment = new TSysAttachment();
|
|
|
+ /** 获取用户会话 **/
|
|
|
+ UserVo userVo = (UserVo)request.getSession().getAttribute(CmsConst.SITE_USER_SESSION_KEY);
|
|
|
+ if(userVo!=null) {
|
|
|
+ attachment.setUserId(userVo.getUserId().toString());
|
|
|
+ attachment.setUsername(userVo.getUsername());
|
|
|
+ }
|
|
|
+ attachment.setUploadDate(new Date());
|
|
|
+ attachment.setUploadIp(ControllerUtil.getRemoteAddress(request));
|
|
|
+ attachment.setFileSize(Float.valueOf(multipartFile.getSize())/1024);
|
|
|
+ attachment.setFileExtname(multipartFile.getContentType());
|
|
|
+ attachment.setFileKey(UUID.randomUUID().toString().replace("-",""));
|
|
|
+ attachment.setOriginalFilename(multipartFile.getOriginalFilename());
|
|
|
+ /*创建uploadBean*/
|
|
|
+ UploadBean result = new UploadBean();
|
|
|
+ String fileType = this.getFileType(attachment.getOriginalFilename());
|
|
|
+ String fileName = this.getFileName(fileType) ;
|
|
|
+ String newName =this.getNewFilenew(fileName);
|
|
|
+ if (!multipartFile.isEmpty()) {
|
|
|
+ if (!Boolean.parseBoolean(qiniuUpload)) {
|
|
|
+ File file = new File(this.getUploadPath() + newName);
|
|
|
+ /*如果不存在就创建*/
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ TranscodeConfig transcodeConfig=new TranscodeConfig();
|
|
|
+ // 原始文件名称,也就是视频的标题
|
|
|
+ String title = multipartFile.getOriginalFilename();
|
|
|
+
|
|
|
+ // io到临时文件
|
|
|
+ Path tempFile = tempDir.resolve(title);
|
|
|
+ try {
|
|
|
+ //this.writeFile(multipartFile.getBytes(), file);
|
|
|
+ /*helper = new FtpHelper() {
|
|
|
+ @Override
|
|
|
+ public void complete(JSONResult jsonResult) {
|
|
|
+ if(jsonResult.isOK()){
|
|
|
+ //success
|
|
|
+ }else{
|
|
|
+ jsonResult.getMsg();
|
|
|
+ jsonResult.getStatus();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ helper.uploadFile(multipartFile.getInputStream(), fileName);*/
|
|
|
+ multipartFile.transferTo(new File(String.valueOf(tempFile)));
|
|
|
+
|
|
|
+ // 删除后缀
|
|
|
+ //title = title.substring(0, title.lastIndexOf(".")) + "-" + UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
+ title = title.substring(0, title.lastIndexOf("."));
|
|
|
+
|
|
|
+ // 按照日期生成子目录
|
|
|
+ String today = this.getNewFilenew()+ attachment.getFileKey();
|
|
|
+ //String today = DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDate.now());
|
|
|
+
|
|
|
+ // 尝试创建视频目录
|
|
|
+ Path targetFolder = Files.createDirectories(Paths.get(videoFolder, today));
|
|
|
+
|
|
|
+ //LOGGER.info("创建文件夹目录:{}", targetFolder);
|
|
|
+ Files.createDirectories(targetFolder);
|
|
|
+
|
|
|
+ // 执行转码操作
|
|
|
+ //LOGGER.info("开始转码");
|
|
|
+ FFmpegUtils.transcodeToM3u8(tempFile.toString(), targetFolder.toString(), transcodeConfig,attachment.getFileKey());
|
|
|
+
|
|
|
+
|
|
|
+ // 封装结果
|
|
|
+ //Map<String, Object> videoInfo = new HashMap<>();
|
|
|
+ //videoInfo.put("title", title);
|
|
|
+ //videoInfo.put("m3u8", String.join("/", "", today, title, attachment.getFileKey() + ".m3u8"));
|
|
|
+ //videoInfo.put("poster", String.join("/", "", today, title, "poster.jpg"));
|
|
|
+
|
|
|
+
|
|
|
+ attachment.setFilePath("\\upload\\100001\\"+attachment.getFileKey()+"\\"+attachment.getFileKey()+".m3u8");
|
|
|
+ attachment.setFileName(fileName);
|
|
|
+ result.setFileUrl(request.getScheme() + "://" + ControllerUtil.getDomain() + "/upload/100001/" +attachment.getFileKey()+"/"+ attachment.getFileKey() + ".m3u8");
|
|
|
+ attachmentService.save(attachment);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ApiException(e.getMessage());
|
|
|
+ }finally {
|
|
|
+ // 始终删除临时文件
|
|
|
+ try{
|
|
|
+ Files.delete(tempFile);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ String qiniuFileResult = QiniuUtil.upload(accessKey, secretKey, bucketname, multipartFile);
|
|
|
+ if (!StrUtil.isBlank(qiniuFileResult)) {
|
|
|
+ String fileKey = JSON.parseObject(qiniuFileResult).getString("key");
|
|
|
+ String fileUrl = domain + "/" + fileKey;
|
|
|
+ if (StrUtil.getExtensionName(fileName).equals("jpg") || StrUtil.getExtensionName(fileName).equals("JPG") || StrUtil.getExtensionName(fileName).equals("png") || StrUtil.getExtensionName(fileName).equals("PNG") || StrUtil.getExtensionName(fileName).equals("jpeg") || StrUtil.getExtensionName(fileName).equals("JPEG")) {
|
|
|
+ fileUrl += "?imageslim";
|
|
|
+ }
|
|
|
+ result.setFileUrl(fileUrl);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }else{
|
|
|
+ throw new ApiException("上传文件不能为空!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public String getUploadPath(){
|
|
|
String os = System.getProperty("os.name");
|
|
|
String uploadPath = enableVirtualPath.equals("true")
|
|
@@ -186,6 +373,17 @@ public class UploadComponent {
|
|
|
+ File.separator + jt.getMonth() + File.separator + jt.getDay() + File.separator + fileName;
|
|
|
}
|
|
|
|
|
|
+ public String getNewFile(){
|
|
|
+ JDateTime jt = new JDateTime();
|
|
|
+ return File.separator + "upload" + File.separator + jt.getYear()
|
|
|
+ + File.separator + jt.getMonth() + File.separator + jt.getDay() + File.separator ;
|
|
|
+ }
|
|
|
+ public String getNewFilenew(){
|
|
|
+ return File.separator + "upload" + File.separator+ "100001" + File.separator;
|
|
|
+ }
|
|
|
+ public String getNewFilenew(String fileName){
|
|
|
+ return File.separator + "res" + File.separator+ fileName;
|
|
|
+ }
|
|
|
public String getFileType(String fileName) {
|
|
|
String type = fileName.substring(fileName.lastIndexOf(".") + 1);
|
|
|
return type;
|