mirror of
https://gitee.com/EMF/solon-manager.git
synced 2025-12-06 08:48:33 +08:00
153 lines
5.1 KiB
Java
153 lines
5.1 KiB
Java
package vip.fuck.sm.service.impl;
|
||
|
||
import cn.hutool.core.io.FileUtil;
|
||
import cn.hutool.core.util.ObjectUtil;
|
||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||
import vip.fuck.sm.common.exception.BusinessException;
|
||
import vip.fuck.sm.common.utils.DateUtils;
|
||
import vip.fuck.sm.common.utils.FileConfig;
|
||
import vip.fuck.sm.entity.SysFilesEntity;
|
||
import vip.fuck.sm.mapper.SysFilesMapper;
|
||
import vip.fuck.sm.service.SysFilesService;
|
||
import org.noear.solon.annotation.Component;
|
||
import org.noear.solon.annotation.Inject;
|
||
import org.noear.solon.core.handle.UploadedFile;
|
||
import org.smartboot.http.server.HttpRequest;
|
||
|
||
import java.io.File;
|
||
import java.util.Date;
|
||
import java.util.List;
|
||
import java.util.UUID;
|
||
|
||
/**
|
||
* 文件上传 服务类
|
||
*
|
||
* @author xian.zi.ming@qq.com fuck.vip
|
||
* @version V1.0
|
||
* @date 2020年3月18日
|
||
*/
|
||
@Component(value = "share:sysFilesService",typed = true)
|
||
public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEntity> implements SysFilesService {
|
||
|
||
|
||
|
||
|
||
|
||
|
||
@Inject("${file.contextPath}")
|
||
private String fileContextPath;
|
||
|
||
@Inject("${file.schema}")
|
||
private String fileSchema;
|
||
|
||
@Override
|
||
public String saveFile(UploadedFile file, HttpRequest request) {
|
||
//存储文件夹
|
||
String createTime = DateUtils.format(new Date(), DateUtils.DATEPATTERN);
|
||
String filePath = FileConfig.getFilePath();
|
||
String newPath = filePath + File.separator + createTime + File.separator;
|
||
File uploadDirectory = new File(newPath);
|
||
if (uploadDirectory.exists()) {
|
||
if (!uploadDirectory.isDirectory()) {
|
||
uploadDirectory.delete();
|
||
}
|
||
} else {
|
||
uploadDirectory.mkdir();
|
||
}
|
||
try {
|
||
String fileName = file.getName();
|
||
//id与filename保持一直,删除文件
|
||
String fileNameNew = UUID.randomUUID().toString().replace("-", "") + getFileType(fileName);
|
||
String newFilePathName = newPath + fileNameNew;
|
||
String url = ( "/files/" + createTime + "/" + fileNameNew).replaceAll("/+", "/");
|
||
//创建输出文件对象
|
||
File outFile = new File(newFilePathName);
|
||
if(!outFile.exists()){
|
||
FileUtil.mkParentDirs(newFilePathName);
|
||
FileUtil.newFile(newFilePathName);
|
||
}
|
||
//拷贝文件到输出文件对象
|
||
file.transferTo(outFile);
|
||
//保存文件记录
|
||
SysFilesEntity sysFilesEntity = new SysFilesEntity();
|
||
sysFilesEntity.setFileName(fileName);
|
||
sysFilesEntity.setFilePath(newFilePathName);
|
||
sysFilesEntity.setUrl(url);
|
||
this.save(sysFilesEntity);
|
||
return url;
|
||
} catch (Exception e) {
|
||
throw new BusinessException("上传文件失败",e);
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void removeByIdsAndFiles(List<String> ids) {
|
||
List<SysFilesEntity> list = this.listByIds(ids);
|
||
list.forEach(entity -> {
|
||
//如果之前的文件存在,删除
|
||
File file = new File(entity.getFilePath());
|
||
if (file.exists()) {
|
||
file.delete();
|
||
}
|
||
});
|
||
this.removeByIds(ids);
|
||
|
||
}
|
||
|
||
@Override
|
||
public IPage<SysFilesEntity> filesPage(SysFilesEntity sysFiles) {
|
||
IPage<SysFilesEntity> p = this.page(sysFiles.getQueryPage(), Wrappers.<SysFilesEntity>lambdaQuery().orderByDesc(SysFilesEntity::getCreateDate));
|
||
if(ObjectUtil.isNotEmpty(p) && ObjectUtil.isNotEmpty(p.getRecords())){
|
||
for (SysFilesEntity record : p.getRecords()) {
|
||
if(ObjectUtil.isNotEmpty(record.getUrl())){
|
||
record.setUrl(String.format("%s://%s",fileSchema,
|
||
String.format("%s/%s",fileContextPath,record.getUrl()).replaceAll("/+","/") ) );
|
||
}
|
||
}
|
||
}
|
||
return p;
|
||
}
|
||
|
||
/**
|
||
* 获取文件后缀名
|
||
*
|
||
* @param fileName 文件名
|
||
* @return 后缀名
|
||
*/
|
||
private String getFileType(String fileName) {
|
||
if (fileName != null && fileName.contains(".")) {
|
||
return fileName.substring(fileName.lastIndexOf("."));
|
||
}
|
||
return "";
|
||
}
|
||
|
||
/**
|
||
* 获取跟路径
|
||
*
|
||
* @param request
|
||
* @return
|
||
*/
|
||
private String getRootDir(HttpRequest request) {
|
||
// 获取协议 (http 或 https)
|
||
String scheme = request.getScheme();
|
||
|
||
// 获取域名
|
||
String serverName = ""; // = request.getServerName();
|
||
|
||
// 获取端口号
|
||
int serverPort = request.getLocalAddress().getPort();
|
||
|
||
// 构建根路径
|
||
StringBuilder rootURL = new StringBuilder();
|
||
rootURL.append(scheme).append("://").append(serverName);
|
||
|
||
// 仅当端口不是默认端口时,才包括端口号
|
||
if ((scheme.equals("http") && serverPort != 80) || (scheme.equals("https") && serverPort != 443)) {
|
||
rootURL.append(":").append(serverPort);
|
||
}
|
||
|
||
return rootURL.toString();
|
||
}
|
||
} |