diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/file/FileController.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/file/FileController.java new file mode 100644 index 00000000..0a86bf24 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/file/FileController.java @@ -0,0 +1,206 @@ +package com.ruoyi.platform.controller.file; + +import com.ruoyi.common.core.web.controller.BaseController; +import com.ruoyi.common.core.web.domain.GenericsAjaxResult; +import com.ruoyi.platform.domain.TChunkInfo; +import com.ruoyi.platform.domain.TFileInfo; +import com.ruoyi.platform.domain.UploadResult; +import com.ruoyi.platform.service.ChunkService; +import com.ruoyi.platform.service.FileInfoService; +import com.ruoyi.platform.utils.FileInfoUtils; +import com.ruoyi.platform.utils.ServletUtils; +import com.ruoyi.platform.vo.TFileInfoVO; +import com.ruoyi.system.api.constant.Constant; +import io.swagger.annotations.Api; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +/** + * 上传下载文件 + * + * @author JaredJia + */ +@RestController +@RequestMapping("/uploader") +@Api("文件上传") +public class FileController extends BaseController { + + @Value("${git.localPath}/temp") + String uploadFolder; + + @Resource + private FileInfoService fileInfoService; + + @Resource + private ChunkService chunkService; + + private final Logger logger = LoggerFactory.getLogger(FileController.class); + + /** + * 上传文件块 + * + * @param chunk + * @return + */ + @PostMapping("/chunk") + public GenericsAjaxResult uploadChunk(TChunkInfo chunk) throws IOException { + String apiRlt = "200"; + + MultipartFile file = chunk.getUpfile(); + logger.info("file originName: {}, chunkNumber: {}", file.getOriginalFilename(), chunk.getChunkNumber()); + + try { + byte[] bytes = file.getBytes(); + Path path = Paths.get(FileInfoUtils.generatePath(uploadFolder, chunk)); + //文件写入指定路径 + Files.write(path, bytes); + if (chunkService.saveChunk(chunk) < 0) apiRlt = "415"; + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } + return genericsSuccess(apiRlt); + } + + @GetMapping("/chunk") + public GenericsAjaxResult checkChunk(TChunkInfo chunk, HttpServletResponse response) { + UploadResult ur = new UploadResult(); + + //默认返回其他状态码,前端不进去checkChunkUploadedByResponse函数,正常走标准上传 + response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + + String file = uploadFolder + "/" + chunk.getIdentifier() + "/" + chunk.getFilename(); + + //先判断整个文件是否已经上传过了,如果是,则告诉前端跳过上传,实现秒传 + if (FileInfoUtils.fileExists(file)) { + ur.setSkipUpload(true); + ur.setLocation(file); + response.setStatus(HttpServletResponse.SC_OK); + ur.setMessage("完整文件已存在,直接跳过上传,实现秒传"); + return genericsSuccess(ur); + } + + //如果完整文件不存在,则去数据库判断当前哪些文件块已经上传过了,把结果告诉前端,跳过这些文件块的上传,实现断点续传 + ArrayList list = chunkService.checkChunk(chunk); + if (list != null && list.size() > 0) { + ur.setSkipUpload(false); + ur.setUploadedChunks(list); + response.setStatus(HttpServletResponse.SC_OK); + ur.setMessage("部分文件块已存在,继续上传剩余文件块,实现断点续传"); + return genericsSuccess(ur); + } + return genericsSuccess(ur); + } + + @PostMapping("/mergeFile") + public GenericsAjaxResult mergeFile(@RequestBody TFileInfoVO fileInfoVO) throws Exception { + + String result = Constant.Failed; + + //前端组件参数转换为model对象 + TFileInfo fileInfo = new TFileInfo(); + fileInfo.setFilename(fileInfoVO.getName()); + fileInfo.setIdentifier(fileInfoVO.getUniqueIdentifier()); + fileInfo.setId(fileInfoVO.getId()); + fileInfo.setTotalSize(fileInfoVO.getSize()); + fileInfo.setRefProjectId(fileInfoVO.getRefProjectId()); + + //进行文件的合并操作 + String filename = fileInfo.getFilename(); + String file = uploadFolder + "/" + fileInfo.getIdentifier() + "/" + filename; + String folder = uploadFolder + "/" + fileInfo.getIdentifier(); + String fileSuccess = FileInfoUtils.merge(file, folder, filename); + + fileInfo.setLocation(file); + + //文件合并成功后,保存记录至数据库 + if ("200".equals(fileSuccess)) { + if (fileInfoService.addFileInfo(fileInfo) > 0) result = Constant.Succeeded; + } + + //如果已经存在,则判断是否同一个项目,同一个项目的不用新增记录,否则新增 + if ("300".equals(fileSuccess)) { + List tfList = fileInfoService.selectFileByParams(fileInfo); + if (tfList != null) { + if (tfList.size() == 0 || (tfList.size() > 0 && !fileInfo.getRefProjectId().equals(tfList.get(0).getRefProjectId()))) { + if (fileInfoService.addFileInfo(fileInfo) > 0) result = Constant.Succeeded; + } + } + } + if (Constant.Succeeded.equals(result)) { + return genericsSuccess(fileInfo); + } else { + throw new Exception("文件上传失败"); + } + } + + /** + * 查询列表 + * + * @return ApiResult + */ + @RequestMapping(value = "/selectFileList", method = RequestMethod.GET) + public GenericsAjaxResult> selectFileList(TFileInfo file) { + List list = fileInfoService.selectFileList(file); + return genericsSuccess(list); + } + + /** + * 下载文件 + * + * @param req + * @param resp + */ + @RequestMapping(value = "/download", method = RequestMethod.GET) + public void download(HttpServletRequest req, HttpServletResponse resp) { + String location = req.getParameter("location"); + String fileName = req.getParameter("filename"); + BufferedInputStream bis = null; + BufferedOutputStream bos = null; + OutputStream fos = null; + try { + bis = new BufferedInputStream(new FileInputStream(location)); + fos = resp.getOutputStream(); + bos = new BufferedOutputStream(fos); + ServletUtils.setFileDownloadHeader(req, resp, fileName); + int byteRead = 0; + byte[] buffer = new byte[8192]; + while ((byteRead = bis.read(buffer, 0, 8192)) != -1) { + bos.write(buffer, 0, byteRead); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + bos.flush(); + bis.close(); + fos.close(); + bos.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * 删除 + */ + @RequestMapping(value = "/deleteFile", method = RequestMethod.POST) + public GenericsAjaxResult deleteFile(@RequestBody TFileInfo tFileInfo) { + int result = fileInfoService.deleteFile(tFileInfo); + return genericsSuccess(result); + } +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/TChunkInfo.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/TChunkInfo.java new file mode 100644 index 00000000..900bde90 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/TChunkInfo.java @@ -0,0 +1,142 @@ +package com.ruoyi.platform.domain; + +import org.springframework.web.multipart.MultipartFile; + +import java.io.Serializable; + +public class TChunkInfo implements Serializable { + + private static final long serialVersionUID = 1L; + + private String id; + /** + * 当前文件块,从1开始 + */ + private Integer chunkNumber; + /** + * 每块大小 + */ + private Long chunkSize; + /** + * 当前分块大小 + */ + private Long currentChunkSize; + /** + * 总大小 + */ + private Long totalSize; + /** + * 文件标识 + */ + private String identifier; + /** + * 文件名 + */ + private String filename; + /** + * 相对路径 + */ + private String relativePath; + /** + * 总块数 + */ + private Integer totalChunks; + /** + * 文件类型 + */ + private String type; + + /** + * 块内容 + */ + private transient MultipartFile upfile; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Integer getChunkNumber() { + return chunkNumber; + } + + public void setChunkNumber(Integer chunkNumber) { + this.chunkNumber = chunkNumber; + } + + public Long getChunkSize() { + return chunkSize; + } + + public void setChunkSize(Long chunkSize) { + this.chunkSize = chunkSize; + } + + public Long getCurrentChunkSize() { + return currentChunkSize; + } + + public void setCurrentChunkSize(Long currentChunkSize) { + this.currentChunkSize = currentChunkSize; + } + + public Long getTotalSize() { + return totalSize; + } + + public void setTotalSize(Long totalSize) { + this.totalSize = totalSize; + } + + public String getIdentifier() { + return identifier; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + public String getFilename() { + return filename; + } + + public void setFilename(String filename) { + this.filename = filename; + } + + public String getRelativePath() { + return relativePath; + } + + public void setRelativePath(String relativePath) { + this.relativePath = relativePath; + } + + public Integer getTotalChunks() { + return totalChunks; + } + + public void setTotalChunks(Integer totalChunks) { + this.totalChunks = totalChunks; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public MultipartFile getUpfile() { + return upfile; + } + + public void setUpfile(MultipartFile upfile) { + this.upfile = upfile; + } + +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/TFileInfo.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/TFileInfo.java new file mode 100644 index 00000000..40afc9bd --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/TFileInfo.java @@ -0,0 +1,161 @@ +package com.ruoyi.platform.domain; + +import java.io.Serializable; +import java.util.Date; + +/** + * 文件模型 + * @author JaredJia + * + */ +public class TFileInfo implements Serializable { + + private static final long serialVersionUID = 6969462437970901728L; + + //附件编号 + private String id; + + //附件名称 + private String filename; + + private String nameSearch; + + //附件MD5标识 + private String identifier; + + //附件总大小 + private Long totalSize; + + private String totalSizeName; + + //附件类型 + private String type; + + //附件存储地址 + private String location; + + //删除标志 + private String delFlag; + + //文件所属目标(项目、学生、档案等,预留字段) + private String refProjectId; + + //上传人 + private String uploadBy; + + //上传时间 + private Date uploadTime; + + private String uploadTimeString; + + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getFilename() { + return filename; + } + + public void setFilename(String filename) { + this.filename = filename; + } + + public String getNameSearch() { + return nameSearch; + } + + public void setNameSearch(String nameSearch) { + this.nameSearch = nameSearch; + } + + public String getIdentifier() { + return identifier; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + public Long getTotalSize() { + return totalSize; + } + + public void setTotalSize(Long totalSize) { + this.totalSize = totalSize; + if(1024*1024 > this.totalSize && this.totalSize >= 1024 ) { + this.totalSizeName = String.format("%.2f",this.totalSize.doubleValue()/1024) + "KB"; + }else if(1024*1024*1024 > this.totalSize && this.totalSize >= 1024*1024 ) { + this.totalSizeName = String.format("%.2f",this.totalSize.doubleValue()/(1024*1024)) + "MB"; + }else if(this.totalSize >= 1024*1024*1024 ) { + this.totalSizeName = String.format("%.2f",this.totalSize.doubleValue()/(1024*1024*1024)) + "GB"; + }else { + this.totalSizeName = this.totalSize.toString() + "B"; + } + } + + public String getTotalSizeName() { + return totalSizeName; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + + public String getDelFlag() { + return delFlag; + } + + public void setDelFlag(String delFlag) { + this.delFlag = delFlag; + } + + public String getRefProjectId() { + return refProjectId; + } + + public void setRefProjectId(String refProjectId) { + this.refProjectId = refProjectId; + } + + public String getUploadBy() { + return uploadBy; + } + + public void setUploadBy(String uploadBy) { + this.uploadBy = uploadBy; + } + + public Date getUploadTime() { + return uploadTime; + } + + public void setUploadTime(Date uploadTime) { + this.uploadTime = uploadTime; + } + + public String getUploadTimeString() { + return uploadTimeString; + } + + public void setUploadTimeString(String uploadTimeString) { + this.uploadTimeString = uploadTimeString; + } + +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/UploadResult.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/UploadResult.java new file mode 100644 index 00000000..029ede5a --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/UploadResult.java @@ -0,0 +1,69 @@ +package com.ruoyi.platform.domain; + +import java.io.Serializable; +import java.util.ArrayList; + +/** + * 文件上传结果 + * @author JaredJia + * + */ +public class UploadResult implements Serializable { + + private static final long serialVersionUID = -9000695051292877324L; + + //是否跳过上传(已上传的可以直接跳过,达到秒传的效果) + private boolean skipUpload; + + //已经上传的文件块编号,可以跳过,断点续传 + private ArrayList uploadedChunks; + + //返回结果码 + private String status; + + //返回结果信息 + private String message; + + //已上传完整附件的地址 + private String location; + + public boolean isSkipUpload() { + return skipUpload; + } + + public void setSkipUpload(boolean skipUpload) { + this.skipUpload = skipUpload; + } + + public ArrayList getUploadedChunks() { + return uploadedChunks; + } + + public void setUploadedChunks(ArrayList uploadedChunks) { + this.uploadedChunks = uploadedChunks; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/TChunkInfoDao.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/TChunkInfoDao.java new file mode 100644 index 00000000..fb5bbb7a --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/TChunkInfoDao.java @@ -0,0 +1,23 @@ +package com.ruoyi.platform.mapper; + +import com.ruoyi.platform.domain.TChunkInfo; +import org.apache.ibatis.annotations.Mapper; + +import java.util.ArrayList; + +@Mapper +public interface TChunkInfoDao { + int deleteByPrimaryKey(String id); + + int insert(TChunkInfo record); + + int insertSelective(TChunkInfo record); + + TChunkInfo selectByPrimaryKey(String id); + + int updateByPrimaryKeySelective(TChunkInfo record); + + int updateByPrimaryKey(TChunkInfo record); + + ArrayList selectChunkNumbers(TChunkInfo record); +} \ No newline at end of file diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/TFileInfoDao.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/TFileInfoDao.java new file mode 100644 index 00000000..bf85e32f --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/TFileInfoDao.java @@ -0,0 +1,27 @@ +package com.ruoyi.platform.mapper; + +import com.ruoyi.platform.domain.TFileInfo; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +@Mapper +public interface TFileInfoDao { + + int deleteByPrimaryKey(String id); + + int insert(TFileInfo record); + + int insertSelective(TFileInfo record); + + TFileInfo selectByPrimaryKey(String id); + + int updateByPrimaryKeySelective(TFileInfo record); + + int updateByPrimaryKey(TFileInfo record); + + List selectFileByParams(TFileInfo fileInfo); + + List selectFileList(TFileInfo fileInfo); + +} \ No newline at end of file diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ChunkService.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ChunkService.java new file mode 100644 index 00000000..418fd1e4 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ChunkService.java @@ -0,0 +1,36 @@ +package com.ruoyi.platform.service; + + +import com.ruoyi.platform.domain.TChunkInfo; + +import java.util.ArrayList; + +/** + * 文件块处理 + * @author JaredJia + * + */ +public interface ChunkService { + /** + * 保存文件块 + * + * @param chunk + */ + public int saveChunk(TChunkInfo chunk); + + /** + * 检查文件块是否存在 + * + * @param identifier + * @param chunkNumber + * @return + */ + boolean checkChunk(String identifier, Integer chunkNumber); + + /** + * 查询哪些文件块已经上传 + * @param chunk + * @return + */ + public ArrayList checkChunk(TChunkInfo chunk); +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/FileInfoService.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/FileInfoService.java new file mode 100644 index 00000000..89577772 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/FileInfoService.java @@ -0,0 +1,29 @@ +package com.ruoyi.platform.service; + + +import com.ruoyi.platform.domain.TFileInfo; + +import java.util.List; + +public interface FileInfoService { + + public int addFileInfo(TFileInfo fileInfo); + + public List selectFileByParams(TFileInfo fileInfo); + + /** + * 查询 + * + * @param File 查询条件 + * @return List + */ + List selectFileList(TFileInfo file); + + + /** + * 删除 + * @param TFileInfo + * @return + */ + int deleteFile(TFileInfo tFileInfo); +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ChunkServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ChunkServiceImpl.java new file mode 100644 index 00000000..2974aafc --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ChunkServiceImpl.java @@ -0,0 +1,34 @@ +package com.ruoyi.platform.service.impl; + +import com.ruoyi.platform.domain.TChunkInfo; +import com.ruoyi.platform.mapper.TChunkInfoDao; +import com.ruoyi.platform.service.ChunkService; +import com.ruoyi.platform.utils.SnowflakeIdWorker; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.ArrayList; + +@Service +public class ChunkServiceImpl implements ChunkService { + + @Resource + TChunkInfoDao tChunkInfoMapper; + + @Override + public int saveChunk(TChunkInfo chunk) { + chunk.setId(SnowflakeIdWorker.getUUID()+ SnowflakeIdWorker.getUUID()); + return tChunkInfoMapper.insertSelective(chunk); + } + + @Override + public ArrayList checkChunk(TChunkInfo chunk) { + return tChunkInfoMapper.selectChunkNumbers(chunk); + } + + @Override + public boolean checkChunk(String identifier, Integer chunkNumber) { + return false; + } + +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/FileInfoServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/FileInfoServiceImpl.java new file mode 100644 index 00000000..cbbf5c04 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/FileInfoServiceImpl.java @@ -0,0 +1,46 @@ +package com.ruoyi.platform.service.impl; + +import com.ruoyi.platform.domain.TFileInfo; +import com.ruoyi.platform.mapper.TFileInfoDao; +import com.ruoyi.platform.service.FileInfoService; +import com.ruoyi.platform.utils.SnowflakeIdWorker; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 文件处理类 + * @author JaredJia + * + */ +@Service +public class FileInfoServiceImpl implements FileInfoService { + + @Resource + TFileInfoDao tFileInfoMapper; + + @Override + public int addFileInfo(TFileInfo fileInfo) { + fileInfo.setId(SnowflakeIdWorker.getUUID()+SnowflakeIdWorker.getUUID()); + return tFileInfoMapper.insertSelective(fileInfo); + } + + @Override + public List selectFileByParams(TFileInfo fileInfo) { + return tFileInfoMapper.selectFileByParams(fileInfo); + } + + @Override + public List selectFileList(TFileInfo file) { + return tFileInfoMapper.selectFileList(file); + } + + @Override + public int deleteFile(TFileInfo tFileInfo) { + TFileInfo t = new TFileInfo(); + t.setId(tFileInfo.getId()); + t.setDelFlag("1"); + return tFileInfoMapper.updateByPrimaryKeySelective(t); + } +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java index ad9a1ac7..6df28844 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java @@ -586,7 +586,6 @@ public class ModelsServiceImpl implements ModelsService { } //新建模型 - // 拼接project GitProjectVo gitProjectVo = new GitProjectVo(); gitProjectVo.setRepositoryName(repositoryName); gitProjectVo.setName(Constant.Topic_model + "-" + modelsVo.getName()); @@ -602,18 +601,6 @@ public class ModelsServiceImpl implements ModelsService { throw new RuntimeException("创建模型失败:" + project.get("message")); } - String branchName = modelsVo.getVersion(); - String owner = (String) userInfo.get("login"); - - String projectUrl = gitCloneEndpoint + "/" + owner + "/" + repositoryName + ".git"; - String sourcePath = modelsVo.getModelVersionVos().get(0).getUrl(); - String rootPath = localPath + ci4sUsername + "/model/" + gitlinIid + "/" + repositoryName + "/" + branchName; - String modelPath = rootPath + "/model"; - String metaPath = rootPath + "/metadata"; - String relatePath = ci4sUsername + "/model/" + gitlinIid + "/" + repositoryName + "/" + branchName + "/model"; - - // 创建分支 - gitService.createBranch(token, (String) userInfo.get("login"), repositoryName, branchName, "master"); // 定义标签 标签1:model 标签2:ModelTag 标签3:ModelType gitService.createTopic(token, gitlinIid, Constant.Topic_model); if (StringUtils.isNotEmpty(modelsVo.getModelTag())) { @@ -623,11 +610,17 @@ public class ModelsServiceImpl implements ModelsService { gitService.createTopic(token, gitlinIid, "modeltype-" + modelsVo.getModelType()); } - dvcUtils.gitClone(rootPath, projectUrl, branchName, gitLinkUsername, decryptGitLinkPassword); + String owner = (String) userInfo.get("login"); + String projectUrl = gitCloneEndpoint + "/" + owner + "/" + repositoryName + ".git"; + String relationPath = ci4sUsername + "/model/" + gitlinIid + "/" + repositoryName + "/origin"; + String rootPath = localPath + relationPath; + String modelPath = rootPath + "/model"; + String metaPath = rootPath + "/metadata"; + String relatePath = relationPath + "/model"; - //干掉目标文件夹 - dvcUtils.deleteDirectory(modelPath); - dvcUtils.moveFiles(sourcePath, modelPath); + // 创建origin分支 + gitService.createBranch(token, owner, repositoryName, "origin", "master"); + dvcUtils.gitClone(rootPath, projectUrl, "origin", gitLinkUsername, decryptGitLinkPassword); //拼接生产的元数据后写入yaml文件 ModelMetaVo modelMetaVo = new ModelMetaVo(); @@ -636,19 +629,13 @@ public class ModelsServiceImpl implements ModelsService { modelMetaVo.setCreateBy(String.valueOf(StringUtils.isNotEmpty((String) userInfo.get("nickname")) ? userInfo.get("nickname") : userInfo.get("login"))); modelMetaVo.setCreateTime(DateUtils.getTime()); modelMetaVo.setUpdateTime(DateUtils.getTime()); - String projectUrlIp = gitCloneEndpointIp + "/" + owner + "/" + repositoryName + ".git"; - modelMetaVo.setUsage("
" +
-                    "# 克隆模型配置文件与存储参数到本地\n" +
-                    "git clone -b " + branchName + " " + projectUrlIp + "\n" +
-                    "# 远程拉取配置文件\n" +
-                    "dvc pull\n" +
-                    "
"); modelMetaVo.setIdentifier(repositoryName); modelMetaVo.setOwner(owner); modelMetaVo.setRelativePaths(relatePath); File folder = new File(modelPath); long folderSize = FileUtil.getFolderSize(folder); modelMetaVo.setModelSize(FileUtil.formatFileSize(folderSize)); + BeanUtils.copyProperties(modelMetaVo, modelsVo); Map metaMap = JSON.parseObject(JSON.toJSONString(modelMetaVo), Map.class); YamlUtils.generateYamlFile(metaMap, metaPath, "metadata"); @@ -658,35 +645,39 @@ public class ModelsServiceImpl implements ModelsService { modelDependency.setRepoId(gitlinIid); modelDependency.setIdentifier(repositoryName); modelDependency.setModelName(modelsVo.getName()); - modelDependency.setVersion(modelsVo.getVersion()); + modelDependency.setVersion("origin"); modelDependency.setParentModel(modelsVo.getParentModel()); modelDependency.setOwner(gitLinkUsername); modelDependency.setMeta(meta); modelDependency.setState(Constant.State_valid); modelDependency1Dao.insert(modelDependency); - String s3Path = bucketName + "/mini-model-management-platform-files/" + ci4sUsername + "/model/" + gitlinIid + "/" + repositoryName + "/" + branchName; - CompletableFuture.supplyAsync(() -> { - try { - dvcUtils.dvcInit(rootPath); - // 配置远程S3地址 - dvcUtils.dvcRemoteAdd(rootPath, s3Path); - dvcUtils.dvcConfigS3Credentials(rootPath, endpoint); - dvcUtils.dvcConfigS3Credentials2(rootPath, accessKeyId); - dvcUtils.dvcConfigS3Credentials3(rootPath, secretAccessKey); - - // dvc 跟踪 - dvcUtils.dvcAdd(rootPath, "model"); - // git commit - dvcUtils.gitCommit(rootPath, "commit from ci4s with " + loginUser.getUsername()); - dvcUtils.gitPush(rootPath, gitLinkUsername, decryptGitLinkPassword); - dvcUtils.dvcPush(rootPath); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw new RuntimeException(e.getMessage()); - } - return null; - }); + String s3Path = bucketName + "/mini-model-management-platform-files/" + relationPath; +// CompletableFuture.supplyAsync(() -> { +// try { + dvcUtils.mkdir(modelPath); + dvcUtils.dvcInit(rootPath); + // 配置远程S3地址 + dvcUtils.dvcRemoteAdd(rootPath, s3Path); + dvcUtils.dvcConfigS3Credentials(rootPath, endpoint); + dvcUtils.dvcConfigS3Credentials2(rootPath, accessKeyId); + dvcUtils.dvcConfigS3Credentials3(rootPath, secretAccessKey); + + // dvc 跟踪 + dvcUtils.dvcAdd(rootPath, "model"); + // git commit + dvcUtils.gitCommit(rootPath, "commit from ci4s with " + loginUser.getUsername()); + dvcUtils.gitPush(rootPath, gitLinkUsername, decryptGitLinkPassword); + dvcUtils.dvcPush(rootPath); + + // 创建分支 + newCreateVersion(modelsVo); +// } catch (Exception e) { +// logger.error(e.getMessage(), e); +// throw new RuntimeException(e.getMessage()); +// } +// return null; +// }); return "新增模型成功"; } catch (Exception e) { @@ -727,8 +718,8 @@ public class ModelsServiceImpl implements ModelsService { FileUtil.renameFile(rootPath, rootPath + "_deleted_" + System.currentTimeMillis()); //克隆 - dvcUtils.gitClone(rootPath, projectUrl, oldModelDependencys.get(0).getVersion(), gitLinkUsername, gitLinkPassword); - dvcUtils.refreshRemoteBranches(rootPath, gitLinkUsername, gitLinkPassword, oldModelDependencys.get(0).getVersion()); + dvcUtils.gitClone(rootPath, projectUrl, "origin", gitLinkUsername, gitLinkPassword); + dvcUtils.refreshRemoteBranches(rootPath, gitLinkUsername, gitLinkPassword, "origin"); //查询项目信息 String token = gitService.checkoutToken(); @@ -775,9 +766,10 @@ public class ModelsServiceImpl implements ModelsService { modelMetaVo.setCreateBy(String.valueOf(StringUtils.isNotEmpty((String) userInfo.get("nickname")) ? userInfo.get("nickname") : userInfo.get("login"))); modelMetaVo.setCreateTime(DateUtils.getTime()); modelMetaVo.setUpdateTime(DateUtils.getTime()); + String projectUrlIp = gitCloneEndpointIp + "/" + owner + "/" + repositoryName + ".git"; modelMetaVo.setUsage("
" +
                     "# 克隆模型配置文件与存储参数到本地\n" +
-                    "git clone -b " + branchName + " " + projectUrl + "\n" +
+                    "git clone -b " + branchName + " " + projectUrlIp + "\n" +
                     "# 远程拉取配置文件\n" +
                     "dvc pull\n" +
                     "
"); @@ -984,7 +976,7 @@ public class ModelsServiceImpl implements ModelsService { String token = gitService.checkoutToken(); List> collect = gitService.getBrancheList(token, owner, identifier); List> result = collect.stream() - .filter(branch -> !"master".equals(branch.get("name"))) + .filter(branch -> !"master".equals(branch.get("name")) && !"origin".equals(branch.get("name"))) .skip((pageRequest.getPageNumber()) * pageRequest.getPageSize()).limit(pageRequest.getPageSize()) .collect(Collectors.toList()); @@ -1087,7 +1079,7 @@ public class ModelsServiceImpl implements ModelsService { String token = gitService.checkoutToken(); List> branchList = gitService.getBrancheList(token, owner, identifier); return branchList.stream() - .filter(branch -> !"master".equals(branch.get("name"))) + .filter(branch -> !"master".equals(branch.get("name")) && !"origin".equals(branch.get("name"))) .collect(Collectors.toList()); } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/NewDatasetServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/NewDatasetServiceImpl.java index d238b7b4..24bc4fca 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/NewDatasetServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/NewDatasetServiceImpl.java @@ -104,9 +104,12 @@ public class NewDatasetServiceImpl implements NewDatasetService { String gitLinkUsername = loginUser.getSysUser().getUserName(); String gitLinkPassword = decrypt(loginUser.getSysUser().getOriginPassword()); String userReq = jedis.get(ci4sUsername + "_gitUserInfo"); + // 得到用户操作的路径 Map userInfo = JsonUtils.jsonToMap(userReq); Integer userId = (Integer) userInfo.get("user_id"); + String owner = (String) userInfo.get("login"); + // 拼接project ci4sUsername = Boolean.TRUE.equals(datasetVo.getIsPublic()) ? Constant.Item_Public : loginUser.getUsername(); String repositoryName = ci4sUsername + "_dataset_" + DateUtils.dateTimeNow(); @@ -117,17 +120,13 @@ public class NewDatasetServiceImpl implements NewDatasetService { gitProjectVo.setPrivate(!datasetVo.getIsPublic()); gitProjectVo.setUserId(userId); gitProjectVo.setProjectCategoryId(projectDatasetId); + // 创建项目 Map project = gitService.createProject(token, gitProjectVo); Integer gitlinIid = (Integer) project.get("id"); if (gitlinIid == null) { throw new Exception("创建数据集失败:" + project.get("message")); } - // 创建分支 - String branchName = datasetVo.getVersion(); - String owner = (String) userInfo.get("login"); - - gitService.createBranch(token, (String) userInfo.get("login"), repositoryName, branchName, "master"); // 定义标签 标签1:dataset 标签2:DataTag 标签3:DataType gitService.createTopic(token, gitlinIid, Constant.Topic_Dataset); if (StringUtils.isNotEmpty(datasetVo.getDataTag())) { @@ -137,61 +136,44 @@ public class NewDatasetServiceImpl implements NewDatasetService { gitService.createTopic(token, gitlinIid, "datatype-" + datasetVo.getDataType()); } - // 得到项目地址 - String projectUrl = gitCloneEndpoint + "/" + owner + "/" + repositoryName + ".git"; - - // 得到用户操作的路径 - String sourcePath = datasetVo.getDatasetVersionVos().get(0).getUrl(); - String relatePath = ci4sUsername + "/datasets/" + gitlinIid + "/" + repositoryName + "/" + branchName; + String relatePath = ci4sUsername + "/datasets/" + gitlinIid + "/" + repositoryName + "/origin"; String localPath = localPathlocal + relatePath; String datasetPath = localPath + "/dataset"; + String s3Path = bucketName + "/mini-model-management-platform-files" + "/" + relatePath; - // 命令行操作 git clone 项目地址 - dvcUtils.gitClone(localPath, projectUrl, branchName, gitLinkUsername, gitLinkPassword); - String s3Path = bucketName + "/mini-model-management-platform-files" + "/" + relatePath + "/" + branchName; - //干掉目标文件夹 - dvcUtils.deleteDirectory(datasetPath); - dvcUtils.moveFiles(sourcePath, datasetPath); + // 得到项目地址 + String projectUrl = gitCloneEndpoint + "/" + owner + "/" + repositoryName + ".git"; + + // 创建origin分支 + gitService.createBranch(token, owner, repositoryName, "origin", "master"); + dvcUtils.gitClone(localPath, projectUrl, "origin", gitLinkUsername, gitLinkPassword); // 拼接生产的元数据后写入yaml文件 datasetVo.setCreateBy(String.valueOf(StringUtils.isNotEmpty((String) userInfo.get("nickname")) ? userInfo.get("nickname") : userInfo.get("login"))); datasetVo.setUpdateTime(DateUtils.getTime()); - datasetVo.setUsage("
" +
-                "# 克隆数据集配置文件与存储参数到本地\n" +
-                "git clone -b " + branchName + " " + projectUrl + "\n" +
-                "# 远程拉取配置文件\n" +
-                "dvc pull\n" +
-                "
"); datasetVo.setIdentifier(repositoryName); datasetVo.setId(gitlinIid); datasetVo.setOwner(owner); datasetVo.setRelativePaths(relatePath + "/dataset"); - - addDatasetSourceToDataVo(datasetVo); - YamlUtils.generateYamlFile(JsonUtils.objectToMap(datasetVo), localPath, "dataset"); - CompletableFuture.supplyAsync(() -> { - try { - // dvc init 初始化 - dvcUtils.dvcInit(localPath); - // 配置远程S3地址 - dvcUtils.dvcRemoteAdd(localPath, s3Path); - dvcUtils.dvcConfigS3Credentials(localPath, endpoint); - dvcUtils.dvcConfigS3Credentials2(localPath, accessKeyId); - dvcUtils.dvcConfigS3Credentials3(localPath, secretAccessKey); - // dvc 跟踪 - dvcUtils.dvcAdd(localPath, "dataset"); - // git commit - dvcUtils.gitCommit(localPath, "commit from ci4s with " + loginUser.getUsername()); - dvcUtils.gitPush(localPath, gitLinkUsername, gitLinkPassword); - // dvc push 到远程S3 - dvcUtils.dvcPush(localPath); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw new RuntimeException(e.getMessage()); - } - return null; - }); + // dvc init 初始化 + dvcUtils.mkdir(datasetPath); + dvcUtils.dvcInit(localPath); + // 配置远程S3地址 + dvcUtils.dvcRemoteAdd(localPath, s3Path); + dvcUtils.dvcConfigS3Credentials(localPath, endpoint); + dvcUtils.dvcConfigS3Credentials2(localPath, accessKeyId); + dvcUtils.dvcConfigS3Credentials3(localPath, secretAccessKey); + // dvc 跟踪 + dvcUtils.dvcAdd(localPath, "dataset"); + // git commit + dvcUtils.gitCommit(localPath, "commit from ci4s with " + loginUser.getUsername()); + dvcUtils.gitPush(localPath, gitLinkUsername, gitLinkPassword); + // dvc push 到远程S3 + dvcUtils.dvcPush(localPath); + + // 创建分支 + newCreateVersion(datasetVo); return "新增数据集成功"; } @@ -214,7 +196,7 @@ public class NewDatasetServiceImpl implements NewDatasetService { ci4sUsername = Boolean.TRUE.equals(datasetVo.getIsPublic()) ? Constant.Item_Public : loginUser.getUsername(); Map userInfo = JsonUtils.jsonToMap(userReq); // 创建分支 - String branchName = StringUtils.isEmpty(datasetVo.getVersion()) ? "master" : datasetVo.getVersion(); + String branchName = datasetVo.getVersion(); String owner = (String) userInfo.get("login"); String repositoryName = datasetVo.getIdentifier(); @@ -232,14 +214,10 @@ public class NewDatasetServiceImpl implements NewDatasetService { Map projectDetail = gitService.getProjectDetail(owner, repositoryName, token); //克隆 - List> brancheList = gitService.getBrancheList(token, owner, repositoryName); - String oldBranch = (String) brancheList.stream() - .filter(branch -> !"master".equals(branch.get("name"))).collect(Collectors.toList()).get(0).get("name"); - - dvcUtils.gitClone(localPath, projectUrl, oldBranch, gitLinkUsername, gitLinkPassword); - dvcUtils.refreshRemoteBranches(localPath, gitLinkUsername, gitLinkPassword, oldBranch); + dvcUtils.gitClone(localPath, projectUrl, "origin", gitLinkUsername, gitLinkPassword); + dvcUtils.refreshRemoteBranches(localPath, gitLinkUsername, gitLinkPassword, "origin"); - //检查是否存在本地重名分支,有的话干掉 + //检查是否存在本地重名分支,有的话删掉 dvcUtils.deleteLocalBranch(localPath, branchName); // 创建本地分支 dvcUtils.createLocalBranchBasedOnMaster(localPath, branchName); @@ -249,8 +227,8 @@ public class NewDatasetServiceImpl implements NewDatasetService { dvcUtils.dvcCheckout(localPath); // 准备数据 - String s3Path = bucketName + "/mini-model-management-platform-files" + "/" + relatePath + "/" + branchName; - //干掉目标文件夹 + String s3Path = bucketName + "/mini-model-management-platform-files" + "/" + relatePath; + //删掉已存在文件夹 dvcUtils.deleteDirectory(datasetPath); if (Constant.Source_Hand_Export.equals(datasetVo.getDatasetSource())) { @@ -261,7 +239,6 @@ public class NewDatasetServiceImpl implements NewDatasetService { dvcUtils.moveFiles(sourcePath, datasetPath); } - // 拼接生产的元数据后写入yaml文件 datasetVo.setCreateBy(String.valueOf(StringUtils.isNotEmpty((String) userInfo.get("nickname")) ? userInfo.get("nickname") : userInfo.get("login"))); datasetVo.setUpdateTime(DateUtils.getTime()); @@ -303,7 +280,6 @@ public class NewDatasetServiceImpl implements NewDatasetService { // dvc 跟踪 dvcUtils.dvcAdd(localPath, "dataset"); dvcUtils.pushNewBranchToRemote(localPath, gitLinkUsername, gitLinkPassword, branchName); - dvcUtils.dvcPush(localPath); // CompletableFuture.supplyAsync(() -> { @@ -407,7 +383,7 @@ public class NewDatasetServiceImpl implements NewDatasetService { String token = gitService.checkoutToken(); List> brancheList = gitService.getBrancheList(token, owner, repo); return brancheList.stream() - .filter(branch -> !"master".equals(branch.get("name"))) + .filter(branch -> !"master".equals(branch.get("name")) && !"origin".equals(branch.get("name"))) .collect(Collectors.toList()); } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/FileInfoUtils.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/FileInfoUtils.java new file mode 100644 index 00000000..3cb35965 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/FileInfoUtils.java @@ -0,0 +1,97 @@ +package com.ruoyi.platform.utils; + +import com.ruoyi.platform.domain.TChunkInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.nio.file.*; + +/** + * 文件工具类 + * @author JaredJia + * + */ +public class FileInfoUtils { + + private final static Logger logger = LoggerFactory.getLogger(FileInfoUtils.class); + + public static String generatePath(String uploadFolder, TChunkInfo chunk) { + StringBuilder sb = new StringBuilder(); + sb.append(uploadFolder).append("/").append(chunk.getIdentifier()); + //判断uploadFolder/identifier 路径是否存在,不存在则创建 + if (!Files.isWritable(Paths.get(sb.toString()))) { + logger.info("path not exist,create path: {}", sb.toString()); + try { + Files.createDirectories(Paths.get(sb.toString())); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + } + + return sb.append("/") + .append(chunk.getFilename()) + .append("-") + .append(chunk.getChunkNumber()).toString(); + } + + /** + * 文件合并 + * + * @param targetFile + * @param folder + */ + public static String merge(String file, String folder, String filename){ + //默认合并成功 + String rlt = "200"; + + try { + //先判断文件是否存在 + if(fileExists(file)) { + //文件已存在 + rlt = "300"; + }else { + //不存在的话,进行合并 + Files.createFile(Paths.get(file)); + + Files.list(Paths.get(folder)) + .filter(path -> !path.getFileName().toString().equals(filename)) + .sorted((o1, o2) -> { + String p1 = o1.getFileName().toString(); + String p2 = o2.getFileName().toString(); + int i1 = p1.lastIndexOf("-"); + int i2 = p2.lastIndexOf("-"); + return Integer.valueOf(p2.substring(i2)).compareTo(Integer.valueOf(p1.substring(i1))); + }) + .forEach(path -> { + try { + //以追加的形式写入文件 + Files.write(Paths.get(file), Files.readAllBytes(path), StandardOpenOption.APPEND); + //合并后删除该块 + Files.delete(path); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + }); + } + } catch (IOException e) { + logger.error(e.getMessage(), e); + //合并失败 + rlt = "400"; + } + + return rlt; + } + + /** + * 根据文件的全路径名判断文件是否存在 + * @param file + * @return + */ + public static boolean fileExists(String file) { + boolean fileExists = false; + Path path = Paths.get(file); + fileExists = Files.exists(path,new LinkOption[]{ LinkOption.NOFOLLOW_LINKS}); + return fileExists; + } +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/ServletUtils.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/ServletUtils.java new file mode 100644 index 00000000..5d592122 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/ServletUtils.java @@ -0,0 +1,41 @@ +package com.ruoyi.platform.utils; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.net.URLEncoder; + +/** + * Servlet工具类 + * + * @author JaredJia + */ +public class ServletUtils { + + /** + * 文件下载时用于写文件头部信息 + * @param request http请求 + * @param response http响应 + * @param fileName 文件名 + */ + public static void setFileDownloadHeader(HttpServletRequest request, + HttpServletResponse response, String fileName) { + try { + String encodedFileName = null; + String agent = request.getHeader("USER-AGENT"); + if (null != agent && -1 != agent.indexOf("MSIE")) { + encodedFileName = URLEncoder.encode(fileName, "UTF-8"); + } else if (null != agent && -1 != agent.indexOf("Mozilla")) { + encodedFileName = new String(fileName.getBytes("UTF-8"), + "iso-8859-1"); + } else { + encodedFileName = URLEncoder.encode(fileName, "UTF-8"); + } + + response.setHeader("Content-Disposition", "attachment; filename=\"" + + encodedFileName + "\""); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/SnowflakeIdWorker.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/SnowflakeIdWorker.java new file mode 100644 index 00000000..9c251c39 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/SnowflakeIdWorker.java @@ -0,0 +1,149 @@ +package com.ruoyi.platform.utils; + +/** + * 雪花算法生成uuid + * + * @author JaredJia + */ +public class SnowflakeIdWorker { + + // ==============================Fields=========================================== + /** 开始时间截 (2015-01-01) */ + private final long twepoch = 1420041600000L; + + /** 机器id所占的位数 */ + private final long workerIdBits = 5L; + + /** 数据标识id所占的位数 */ + private final long datacenterIdBits = 5L; + + /** 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ + private final long maxWorkerId = -1L ^ (-1L << workerIdBits); + + /** 支持的最大数据标识id,结果是31 */ + private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); + + /** 序列在id中占的位数 */ + private final long sequenceBits = 12L; + + /** 机器ID向左移12位 */ + private final long workerIdShift = sequenceBits; + + /** 数据标识id向左移17位(12+5) */ + private final long datacenterIdShift = sequenceBits + workerIdBits; + + /** 时间截向左移22位(5+5+12) */ + private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; + + /** 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ + private final long sequenceMask = -1L ^ (-1L << sequenceBits); + + /** 工作机器ID(0~31) */ + private long workerId; + + /** 数据中心ID(0~31) */ + private long datacenterId; + + /** 毫秒内序列(0~4095) */ + private long sequence = 0L; + + /** 上次生成ID的时间截 */ + private long lastTimestamp = -1L; + + // ==============================Constructors===================================== + /** + * 构造函数 + * + * @param workerId + * 工作ID (0~31) + * @param datacenterId + * 数据中心ID (0~31) + */ + public SnowflakeIdWorker(long workerId, long datacenterId) { + if (workerId > maxWorkerId || workerId < 0) { + throw new IllegalArgumentException( + String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); + } + if (datacenterId > maxDatacenterId || datacenterId < 0) { + throw new IllegalArgumentException( + String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); + } + this.workerId = workerId; + this.datacenterId = datacenterId; + } + + // ==============================Methods========================================== + /** + * 获得下一个ID (该方法是线程安全的) + * + * @return SnowflakeId + */ + public synchronized long nextId() { + long timestamp = timeGen(); + + // 如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 + if (timestamp < lastTimestamp) { + throw new RuntimeException(String.format( + "Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); + } + + // 如果是同一时间生成的,则进行毫秒内序列 + if (lastTimestamp == timestamp) { + sequence = (sequence + 1) & sequenceMask; + // 毫秒内序列溢出 + if (sequence == 0) { + // 阻塞到下一个毫秒,获得新的时间戳 + timestamp = tilNextMillis(lastTimestamp); + } + } + // 时间戳改变,毫秒内序列重置 + else { + sequence = 0L; + } + + // 上次生成ID的时间截 + lastTimestamp = timestamp; + + // 移位并通过或运算拼到一起组成64位的ID + return ((timestamp - twepoch) << timestampLeftShift) // + | (datacenterId << datacenterIdShift) // + | (workerId << workerIdShift) // + | sequence; + } + + /** + * 阻塞到下一个毫秒,直到获得新的时间戳 + * + * @param lastTimestamp + * 上次生成ID的时间截 + * @return 当前时间戳 + */ + protected long tilNextMillis(long lastTimestamp) { + long timestamp = timeGen(); + while (timestamp <= lastTimestamp) { + timestamp = timeGen(); + } + return timestamp; + } + + /** + * 返回以毫秒为单位的当前时间 + * + * @return 当前时间(毫秒) + */ + protected long timeGen() { + return System.currentTimeMillis(); + } + + /** + * 获取UUID + * + * @return + */ + public static String getUUID() { + SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0); + long id = idWorker.nextId(); + return String.valueOf(id); + } + +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/TFileInfoVO.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/TFileInfoVO.java new file mode 100644 index 00000000..d0192adf --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/TFileInfoVO.java @@ -0,0 +1,92 @@ +package com.ruoyi.platform.vo; + +import java.io.Serializable; + +/** + * 文件模型参数 + * @author JaredJia + * + */ +public class TFileInfoVO implements Serializable { + + private static final long serialVersionUID = -668666237985927833L; + + //附件编号 + private String id; + + //附件类型 + private String fileType; + + //附件名称 + private String name; + + //附件总大小 + private Long size; + + //附件地址 + private String relativePath; + + //附件MD5标识 + private String uniqueIdentifier; + + //附件所属项目ID + private String refProjectId; + + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getFileType() { + return fileType; + } + + public void setFileType(String fileType) { + this.fileType = fileType; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getSize() { + return size; + } + + public void setSize(Long size) { + this.size = size; + } + + public String getRelativePath() { + return relativePath; + } + + public void setRelativePath(String relativePath) { + this.relativePath = relativePath; + } + + public String getUniqueIdentifier() { + return uniqueIdentifier; + } + + public void setUniqueIdentifier(String uniqueIdentifier) { + this.uniqueIdentifier = uniqueIdentifier; + } + + public String getRefProjectId() { + return refProjectId; + } + + public void setRefProjectId(String refProjectId) { + this.refProjectId = refProjectId; + } + +} diff --git a/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/TChunkInfoMapper.xml b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/TChunkInfoMapper.xml new file mode 100644 index 00000000..6aafb804 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/TChunkInfoMapper.xml @@ -0,0 +1,150 @@ + + + + + + + + + + + + + + + + id, chunk_number, chunk_size, current_chunkSize, identifier, filename, relative_path, + total_chunks, type + + + + delete from t_chunk_info + where id = #{id,jdbcType=VARCHAR} + + + insert into t_chunk_info (id, chunk_number, chunk_size, + current_chunkSize, identifier, filename, + relative_path, total_chunks, type + ) + values (#{id,jdbcType=VARCHAR}, #{chunkNumber,jdbcType=DECIMAL}, #{chunkSize,jdbcType=DECIMAL}, + #{currentChunkSize,jdbcType=DECIMAL}, #{identifier,jdbcType=VARCHAR}, #{filename,jdbcType=VARCHAR}, + #{relativePath,jdbcType=VARCHAR}, #{totalChunks,jdbcType=DECIMAL}, #{type,jdbcType=DECIMAL} + ) + + + insert into t_chunk_info + + + id, + + + chunk_number, + + + chunk_size, + + + current_chunkSize, + + + identifier, + + + filename, + + + relative_path, + + + total_chunks, + + + type, + + + + + #{id,jdbcType=VARCHAR}, + + + #{chunkNumber,jdbcType=DECIMAL}, + + + #{chunkSize,jdbcType=DECIMAL}, + + + #{currentChunkSize,jdbcType=DECIMAL}, + + + #{identifier,jdbcType=VARCHAR}, + + + #{filename,jdbcType=VARCHAR}, + + + #{relativePath,jdbcType=VARCHAR}, + + + #{totalChunks,jdbcType=DECIMAL}, + + + #{type,jdbcType=DECIMAL}, + + + + + update t_chunk_info + + + chunk_number = #{chunkNumber,jdbcType=DECIMAL}, + + + chunk_size = #{chunkSize,jdbcType=DECIMAL}, + + + current_chunkSize = #{currentChunkSize,jdbcType=DECIMAL}, + + + identifier = #{identifier,jdbcType=VARCHAR}, + + + filename = #{filename,jdbcType=VARCHAR}, + + + relative_path = #{relativePath,jdbcType=VARCHAR}, + + + total_chunks = #{totalChunks,jdbcType=DECIMAL}, + + + type = #{type,jdbcType=DECIMAL}, + + + where id = #{id,jdbcType=VARCHAR} + + + update t_chunk_info + set chunk_number = #{chunkNumber,jdbcType=DECIMAL}, + chunk_size = #{chunkSize,jdbcType=DECIMAL}, + current_chunkSize = #{currentChunkSize,jdbcType=DECIMAL}, + identifier = #{identifier,jdbcType=VARCHAR}, + filename = #{filename,jdbcType=VARCHAR}, + relative_path = #{relativePath,jdbcType=VARCHAR}, + total_chunks = #{totalChunks,jdbcType=DECIMAL}, + type = #{type,jdbcType=DECIMAL} + where id = #{id,jdbcType=VARCHAR} + + + + \ No newline at end of file diff --git a/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/TFileInfoMapper.xml b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/TFileInfoMapper.xml new file mode 100644 index 00000000..4caeb021 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/TFileInfoMapper.xml @@ -0,0 +1,178 @@ + + + + + + + + + + + + + + + + + id, filename, identifier, type, total_size, location, del_flag, ref_project_id, upload_by, + upload_time + + + + delete from t_file_info + where id = #{id,jdbcType=VARCHAR} + + + insert into t_file_info (id, filename, identifier, + type, total_size, location, + del_flag, ref_project_id, upload_by, + upload_time) + values (#{id,jdbcType=VARCHAR}, #{filename,jdbcType=VARCHAR}, #{identifier,jdbcType=VARCHAR}, + #{type,jdbcType=VARCHAR}, #{totalSize,jdbcType=DECIMAL}, #{location,jdbcType=VARCHAR}, + #{delFlag,jdbcType=VARCHAR}, #{refProjectId,jdbcType=VARCHAR}, #{uploadBy,jdbcType=VARCHAR}, + #{uploadTime,jdbcType=TIMESTAMP}) + + + insert into t_file_info + + + id, + + + filename, + + + identifier, + + + type, + + + total_size, + + + location, + + + del_flag, + + + ref_project_id, + + + upload_by, + + + upload_time, + + + + + #{id,jdbcType=VARCHAR}, + + + #{filename,jdbcType=VARCHAR}, + + + #{identifier,jdbcType=VARCHAR}, + + + #{type,jdbcType=VARCHAR}, + + + #{totalSize,jdbcType=DECIMAL}, + + + #{location,jdbcType=VARCHAR}, + + + #{delFlag,jdbcType=VARCHAR}, + + + #{refProjectId,jdbcType=VARCHAR}, + + + #{uploadBy,jdbcType=VARCHAR}, + + + #{uploadTime,jdbcType=TIMESTAMP}, + + + + + update t_file_info + + + filename = #{filename,jdbcType=VARCHAR}, + + + identifier = #{identifier,jdbcType=VARCHAR}, + + + type = #{type,jdbcType=VARCHAR}, + + + total_size = #{totalSize,jdbcType=DECIMAL}, + + + location = #{location,jdbcType=VARCHAR}, + + + del_flag = #{delFlag,jdbcType=VARCHAR}, + + + ref_project_id = #{refProjectId,jdbcType=VARCHAR}, + + + upload_by = #{uploadBy,jdbcType=VARCHAR}, + + + upload_time = #{uploadTime,jdbcType=TIMESTAMP}, + + + where id = #{id,jdbcType=VARCHAR} + + + update t_file_info + set filename = #{filename,jdbcType=VARCHAR}, + identifier = #{identifier,jdbcType=VARCHAR}, + type = #{type,jdbcType=VARCHAR}, + total_size = #{totalSize,jdbcType=DECIMAL}, + location = #{location,jdbcType=VARCHAR}, + del_flag = #{delFlag,jdbcType=VARCHAR}, + ref_project_id = #{refProjectId,jdbcType=VARCHAR}, + upload_by = #{uploadBy,jdbcType=VARCHAR}, + upload_time = #{uploadTime,jdbcType=TIMESTAMP} + where id = #{id,jdbcType=VARCHAR} + + + + + + + \ No newline at end of file