You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
466 lines
21 KiB
Java
466 lines
21 KiB
Java
package cn.jlw.Interceptor;
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.jlw.util.ChunkUploadUtil;
|
|
import cn.jlw.util.ConvertM3U8;
|
|
import cn.jlw.util.SpringContextUtil;
|
|
import cn.jlw.util.ToolUtils;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.ibeetl.admin.core.entity.CoreUser;
|
|
import com.ibeetl.admin.core.service.CorePlatformService;
|
|
import com.ibeetl.jlw.entity.FileEntity;
|
|
import com.ibeetl.jlw.entity.Student;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.core.MethodParameter;
|
|
import org.springframework.web.bind.support.WebDataBinderFactory;
|
|
import org.springframework.web.context.request.NativeWebRequest;
|
|
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
|
import org.springframework.web.method.support.ModelAndViewContainer;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.Part;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.Serializable;
|
|
import java.util.*;
|
|
import java.util.concurrent.Callable;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.Future;
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
import static cn.hutool.core.date.DatePattern.PURE_DATETIME_MS_FORMAT;
|
|
import static cn.jlw.util.CacheUserUtil.checkSession;
|
|
import static com.ibeetl.admin.core.service.CorePlatformService.T_STUDENT;
|
|
import static com.ibeetl.admin.core.util.user.CacheUserUtil.getUserId;
|
|
import static java.io.File.separatorChar;
|
|
|
|
//将从request中获取的file获取出来并存入临时文件夹中
|
|
|
|
@Configuration
|
|
public class GetFile implements HandlerMethodArgumentResolver {
|
|
|
|
private final ExecutorService pool = Executors.newCachedThreadPool();
|
|
|
|
private static final AtomicLong count = new AtomicLong(0);
|
|
|
|
private final Log log = LogFactory.getLog(this.getClass());
|
|
|
|
public static String p = new FileEntity().getParentPath();
|
|
static {
|
|
if(StringUtils.isNotBlank(p)&&p.contains("file")){
|
|
p = p.replace("file:\\","");
|
|
}
|
|
}
|
|
|
|
public GetFile() {
|
|
}
|
|
|
|
@Override
|
|
public boolean supportsParameter(MethodParameter parameter) {
|
|
return parameter.getParameterType().isAssignableFrom(FileEntity.class) && parameter.hasParameterAnnotation(RFile.class);
|
|
}
|
|
|
|
|
|
protected static class Base64Pojo {
|
|
//数据
|
|
private String str;
|
|
|
|
public String getStr() {
|
|
return str;
|
|
}
|
|
|
|
public void setStr(String str) {
|
|
this.str = str;
|
|
}
|
|
|
|
public String getFormatSpot() {
|
|
if(StringUtils.isNotBlank(this.str)){
|
|
return "."+ToolUtils.subString(this.str,"data:image/",";base64,");
|
|
}else {
|
|
return ".png";
|
|
}
|
|
}
|
|
}
|
|
|
|
public class MultipartFileParam implements Serializable {
|
|
|
|
private long chunkNumber;//当前为第几分片
|
|
private long chunkSize;//每个分块的大小
|
|
private long totalChunks;//分片总数
|
|
private String identifier;//文件唯一标识
|
|
private MultipartFile multipartFile;//分块文件传输对象
|
|
|
|
public long getChunkNumber() {
|
|
return chunkNumber;
|
|
}
|
|
public void setChunkNumber(long chunkNumber) {
|
|
this.chunkNumber = chunkNumber;
|
|
}
|
|
public long getChunkSize() {
|
|
return chunkSize;
|
|
}
|
|
public void setChunkSize(long chunkSize) {
|
|
this.chunkSize = chunkSize;
|
|
}
|
|
public long getTotalChunks() {
|
|
return totalChunks;
|
|
}
|
|
public void setTotalChunks(long totalChunks) {
|
|
this.totalChunks = totalChunks;
|
|
}
|
|
public String getIdentifier() {
|
|
return identifier;
|
|
}
|
|
public void setIdentifier(String identifier) {
|
|
this.identifier = identifier;
|
|
}
|
|
public MultipartFile getMultipartFile() {
|
|
return multipartFile;
|
|
}
|
|
public void setMultipartFile(MultipartFile multipartFile) {
|
|
this.multipartFile = multipartFile;
|
|
}
|
|
}
|
|
|
|
public Object resolveArgument2(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
|
|
RFile rFile = parameter.getParameterAnnotation(RFile.class);
|
|
List<FileEntity> fileEntityList = new ArrayList<>();
|
|
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
|
|
checkSession(); final Long userId = getUserId();
|
|
String dateTime = PURE_DATETIME_MS_FORMAT.format(DateUtil.date());
|
|
|
|
String userFilePathPrefix = StrUtil.format("{}{}{}{}", separatorChar, userId, separatorChar, dateTime);
|
|
String generalPathPrefix = StrUtil.format("{}{}", separatorChar, "filesystem");
|
|
|
|
String tempPath = StrUtil.format("{}{}{}{}", generalPathPrefix, separatorChar, "TEMP", userFilePathPrefix);
|
|
// 文件集合
|
|
Collection<Part> requestParts = request.getParts();
|
|
requestParts.forEach(part -> {
|
|
String name = part.getName();
|
|
try {
|
|
part.getInputStream();
|
|
part.write("/" + part.getSubmittedFileName());
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
});
|
|
return fileEntityList;
|
|
}
|
|
|
|
|
|
/**
|
|
* 功能描述: <br>
|
|
* 暂时先注释掉老的上传方法。
|
|
*
|
|
* @param parameter
|
|
* @param mavContainer
|
|
* @param webRequest
|
|
* @param binderFactory
|
|
* @return {@link Object}
|
|
* @Author: 87966
|
|
* @Date: 2022/12/6 17:37
|
|
*/
|
|
@Override
|
|
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
|
|
RFile rFile = parameter.getParameterAnnotation(RFile.class);
|
|
|
|
List<FileEntity> fileEntityList = new ArrayList<>();
|
|
|
|
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
|
|
|
|
//学生
|
|
Student student = null == request.getAttribute(T_STUDENT) ? null : (Student) request.getAttribute(T_STUDENT);
|
|
|
|
//管理员(教师或管理员)
|
|
CoreUser user = null == request.getSession().getAttribute(CorePlatformService.ACCESS_CURRENT_USER)? null : (CoreUser) request.getSession().getAttribute(CorePlatformService.ACCESS_CURRENT_USER);
|
|
|
|
MultipartHttpServletRequest mhsr = null;
|
|
MultipartFileParam multipartFileParam = null;
|
|
try{
|
|
mhsr = (MultipartHttpServletRequest) request;
|
|
}catch (Exception e){}
|
|
try{
|
|
MultipartFileParam m = new MultipartFileParam();
|
|
if(StringUtils.isNotBlank(request.getParameter("chunkNumber"))){
|
|
m.setChunkNumber(Long.parseLong(request.getParameter("chunkNumber")));
|
|
if(StringUtils.isNotBlank(request.getParameter("chunkSize"))){
|
|
m.setChunkSize(Long.parseLong(request.getParameter("chunkSize")));
|
|
if(StringUtils.isNotBlank(request.getParameter("totalChunks"))){
|
|
m.setTotalChunks(Long.parseLong(request.getParameter("totalChunks")));
|
|
if(StringUtils.isNotBlank(request.getParameter("identifier"))){
|
|
m.setIdentifier(request.getParameter("identifier"));
|
|
if(null != mhsr && null != mhsr.getMultiFileMap() && null != mhsr.getMultiFileMap().getFirst("file")){
|
|
m.setMultipartFile(mhsr.getMultiFileMap().getFirst("file"));
|
|
multipartFileParam = m;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}catch (Exception e){}
|
|
|
|
|
|
String base64List = request.getParameter("base64List");
|
|
String fileName = request.getParameter("fileName");
|
|
String url = request.getParameter("url");
|
|
|
|
List<Base64Pojo> strList = new ArrayList<>();
|
|
if(StringUtils.isNotBlank(base64List)) {
|
|
try{
|
|
strList = JSON.parseArray(base64List,Base64Pojo.class);
|
|
}catch (Exception e){
|
|
try {
|
|
strList.add(JSONObject.parseObject(base64List,Base64Pojo.class));
|
|
}catch (Exception e1){
|
|
}
|
|
}
|
|
}
|
|
|
|
if (mhsr == null && strList.size() == 0)
|
|
return null;
|
|
|
|
//存入filesystem下的temp文件夹
|
|
String se = File.separator;
|
|
String path = p + se +"filesystem"+se+"temp";
|
|
path += StringUtils.isNotBlank(url)?(se+url):"";
|
|
|
|
File folders = new File(path);
|
|
if (!folders.exists()) {
|
|
folders.mkdirs();
|
|
}
|
|
long num = 0;
|
|
try {
|
|
// if(null != user || null != student){
|
|
if(mhsr != null){
|
|
if(null != multipartFileParam){
|
|
FileEntity fileEntity = new FileEntity();
|
|
fileEntity.setSort(0);
|
|
String tempName = (System.currentTimeMillis()+num+count.incrementAndGet())+"_"+(null == user?5:user.getId()*5-2)+"_"+(null == student?7:student.getStudentId()*7-1);
|
|
String nameFormat = multipartFileParam.getMultipartFile().getOriginalFilename();
|
|
|
|
if (StringUtils.isBlank(nameFormat)) {
|
|
return null;
|
|
}
|
|
|
|
if(multipartFileParam.getMultipartFile().getSize() == 0){
|
|
return null;
|
|
}
|
|
|
|
String[] nf = nameFormat.split("\\.");
|
|
fileEntity.setTempName(tempName);
|
|
fileEntity.setName(nameFormat.replace("."+nf[nf.length - 1],""));
|
|
fileEntity.setTitle(nameFormat);
|
|
fileEntity.setFormat(nf[nf.length -1]);
|
|
fileEntity.setSlicingList(new Integer[(int)multipartFileParam.getTotalChunks()]);
|
|
|
|
boolean flag = false;
|
|
try {
|
|
ChunkUploadUtil chunkUploadUtil = (ChunkUploadUtil)SpringContextUtil.getBean("chunkUploadUtil");
|
|
flag = chunkUploadUtil.chunkUploadByMappedByteBuffer(multipartFileParam, path, tempName);
|
|
|
|
if(flag){
|
|
fileEntity.setAbsoluteUrl(path+se+tempName+fileEntity.getFormatSpot());
|
|
fileEntity.setUrl(se +"filesystem"+se+"temp"+(StringUtils.isNotBlank(url)?(se+url):"")+se+tempName+fileEntity.getFormatSpot());
|
|
fileEntity.setSrc(se +"filesystem"+se+"temp"+(StringUtils.isNotBlank(url)?(se+url):"")+se+tempName+fileEntity.getFormatSpot());
|
|
fileEntity.setSize(multipartFileParam.getMultipartFile().getSize());
|
|
if( StringUtils.isNotBlank(nameFormat) && StringUtils.isNotBlank(fileName)){
|
|
String[] fName = fileName.split(",");
|
|
for(int i=0;i<fName.length;i++){
|
|
if(nameFormat.equals(fName[i])){
|
|
fileEntity.setSort(i);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
Date date = new Date();
|
|
date.setTime(Long.parseLong(tempName.split("_")[0]));
|
|
fileEntity.setDate(date);
|
|
File file = new File(fileEntity.getAbsoluteUrl());
|
|
if (!file.exists()) {
|
|
return null;
|
|
}else{
|
|
if(ToolUtils.findInSet("asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv",fileEntity.getFormat())){
|
|
List<Future<Boolean>> results = new ArrayList<>();
|
|
results.add(pool.submit(new TaskCallable(path,tempName,fileEntity.getFormatSpot())));
|
|
}
|
|
}
|
|
|
|
File confFile = new File(path, multipartFileParam.getIdentifier() + ".conf");
|
|
if(confFile.exists()) {
|
|
byte[] completeStatusList = FileUtils.readFileToByteArray(confFile);
|
|
Integer[] slicingList = new Integer[completeStatusList.length];
|
|
for(int i=0;i<completeStatusList.length;i++){
|
|
if (completeStatusList[i] != Byte.MAX_VALUE) {
|
|
slicingList[i] = 0;
|
|
}else {
|
|
slicingList[i] = 127;
|
|
}
|
|
}
|
|
fileEntity.setSlicingList(slicingList);
|
|
}
|
|
|
|
return fileEntity;
|
|
}else {
|
|
FileEntity f = new FileEntity();
|
|
File confFile = new File(path, multipartFileParam.getIdentifier() + ".conf");
|
|
if(confFile.exists()) {
|
|
byte[] completeStatusList = FileUtils.readFileToByteArray(confFile);
|
|
Integer[] slicingList = new Integer[completeStatusList.length];
|
|
for(int i=0;i<completeStatusList.length;i++){
|
|
if (completeStatusList[i] != Byte.MAX_VALUE) {
|
|
slicingList[i] = 0;
|
|
}else {
|
|
slicingList[i] = 127;
|
|
}
|
|
}
|
|
f.setSlicingList(slicingList);
|
|
}
|
|
return f;
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}else {
|
|
Map<String, MultipartFile> files = mhsr.getFileMap();
|
|
for (String key : files.keySet()) {
|
|
|
|
FileEntity fileEntity = new FileEntity();
|
|
fileEntity.setSort(0);
|
|
|
|
MultipartFile mf = mhsr.getFile(key);
|
|
String nameFormat = mf.getOriginalFilename();
|
|
|
|
String tempName = (System.currentTimeMillis()+num+count.incrementAndGet())+"_"+(null == user?5:user.getId()*5-2)+"_"+(null == student?7:student.getStudentId()*7-1);
|
|
num++;
|
|
if (StringUtils.isBlank(nameFormat)) {
|
|
continue;
|
|
}
|
|
|
|
if(mf.getSize() == 0){
|
|
continue;
|
|
}
|
|
|
|
if (ToolUtils.isNotSplitBlank(nameFormat,".",1)) {
|
|
String[] nf = nameFormat.split("\\.");
|
|
fileEntity.setTempName(tempName);
|
|
fileEntity.setName(nameFormat.replace("."+nf[nf.length - 1],""));
|
|
fileEntity.setTitle(nameFormat);
|
|
fileEntity.setFormat(nf[nf.length -1]);
|
|
// 这里是指定上传文件的存放位置
|
|
fileEntity.setAbsoluteUrl(path+se+tempName+fileEntity.getFormatSpot());
|
|
fileEntity.setUrl(se +"filesystem"+se+"temp"+(StringUtils.isNotBlank(url)?(se+url):"")+se+tempName+fileEntity.getFormatSpot());
|
|
fileEntity.setSrc(se +"filesystem"+se+"temp"+(StringUtils.isNotBlank(url)?(se+url):"")+se+tempName+fileEntity.getFormatSpot());
|
|
fileEntity.setSize(mf.getSize());
|
|
if( StringUtils.isNotBlank(nameFormat) && StringUtils.isNotBlank(fileName)){
|
|
String[] fName = fileName.split(",");
|
|
for(int i=0;i<fName.length;i++){
|
|
if(nameFormat.equals(fName[i])){
|
|
fileEntity.setSort(i);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
Date date = new Date();
|
|
date.setTime(Long.parseLong(tempName.split("_")[0]));
|
|
fileEntity.setDate(date);
|
|
File file = new File(fileEntity.getAbsoluteUrl());
|
|
if (!file.exists()) {
|
|
file.createNewFile();
|
|
}else{
|
|
if(ToolUtils.findInSet("asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv",fileEntity.getFormat())){
|
|
List<Future<Boolean>> results = new ArrayList<>();
|
|
results.add(pool.submit(new TaskCallable(path,fileName,fileEntity.getFormatSpot())));
|
|
}
|
|
}
|
|
|
|
// 将导入的文件,存放在本地
|
|
mf.transferTo(file);
|
|
fileEntityList.add(fileEntity);
|
|
}
|
|
}
|
|
}
|
|
}else if(strList.size()>0){
|
|
for(int i=0;i<strList.size();i++){
|
|
FileEntity fileEntity = new FileEntity();
|
|
String tempName = (System.currentTimeMillis()+num+count.incrementAndGet())+"_"+(null == user?5:user.getId()*5-2)+"_"+(null == student?7:student.getStudentId()*7-1);
|
|
num++;
|
|
if(null == strList.get(i) || null == strList.get(i).getStr() || strList.get(i).getStr().length() == 0){
|
|
continue;
|
|
}
|
|
String formatSpot = strList.get(i).getFormatSpot();
|
|
|
|
fileEntity.setTempName(tempName);
|
|
fileEntity.setAbsoluteUrl(path+se+tempName+formatSpot);
|
|
fileEntity.setFormat(formatSpot.substring(1));
|
|
fileEntity.setUrl(se +"filesystem"+se+"temp"+(StringUtils.isNotBlank(url)?(se+url):"")+se+tempName+formatSpot);
|
|
fileEntity.setSize((long)strList.get(i).getStr().length());
|
|
Date date = new Date();
|
|
date.setTime(Long.parseLong(tempName.split("_")[0]));
|
|
fileEntity.setDate(date);
|
|
|
|
if(ToolUtils.Base64ToImage(strList.get(i).getStr(),fileEntity.getAbsoluteUrl())){
|
|
}else {
|
|
continue;
|
|
}
|
|
fileEntityList.add(fileEntity);
|
|
|
|
}
|
|
}
|
|
// }else {
|
|
// log.error("有人上传不对劲的玩意!");
|
|
// }
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
if(fileEntityList.size() == 0){
|
|
return null;
|
|
}
|
|
|
|
if(fileEntityList.size() == 1){
|
|
return fileEntityList.get(0);
|
|
}
|
|
return fileEntityList;
|
|
}
|
|
|
|
|
|
private class TaskCallable implements Callable<Boolean> {
|
|
String se = File.separator;
|
|
String path;
|
|
String tempName;
|
|
String formatSpot;
|
|
|
|
public TaskCallable(String path,String tempName,String formatSpot) {
|
|
this.path = path;
|
|
this.tempName = tempName;
|
|
this.formatSpot = formatSpot;
|
|
}
|
|
|
|
@Override
|
|
public Boolean call() {
|
|
log.info("开始转化的啦");
|
|
try {
|
|
ConvertM3U8 convertM3U8 = new ConvertM3U8();
|
|
convertM3U8.toM3U8(path+se,tempName+formatSpot);//开始转换
|
|
log.info("转化完毕的啦");
|
|
return true;
|
|
}catch (Exception e){
|
|
log.info("转化嗝屁的啦");
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
} |