|
|
@@ -0,0 +1,70 @@
|
|
|
+package com.uas.ops.diff.api.v1;
|
|
|
+
|
|
|
+import com.uas.ops.diff.entity.ProjectDiff;
|
|
|
+import com.uas.ops.diff.entity.ProjectDiffId;
|
|
|
+import com.uas.ops.diff.repository.ProjectDiffRepository;
|
|
|
+import com.uas.ops.diff.service.FileService;
|
|
|
+import com.uas.ops.diff.util.StringUtils;
|
|
|
+import com.uas.ops.diff.util.ZipUtils;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.FileCopyUtils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by Pro1 on 2017/6/13.
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class DiffServiceV1 {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ProjectDiffRepository projectDiffRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FileService fileService;
|
|
|
+
|
|
|
+ private String tmpdir = System.getProperty("java.io.tmpdir");
|
|
|
+
|
|
|
+ public void save(String groupId, String artifactId, String fromVersion, String toVersion, byte[] data) throws IOException {
|
|
|
+ ProjectDiffId projectDiffId = new ProjectDiffId(groupId, artifactId, fromVersion, toVersion);
|
|
|
+ ProjectDiff projectDiff = projectDiffRepository.findOne(projectDiffId);
|
|
|
+ if (null != projectDiff) {
|
|
|
+ fileService.delete(projectDiff.getDiffPath());
|
|
|
+ } else {
|
|
|
+ projectDiff = new ProjectDiff(groupId, artifactId, fromVersion, toVersion);
|
|
|
+ }
|
|
|
+ projectDiff.setCreateTime(new Date());
|
|
|
+ String path = fileService.save(data, ".diff");
|
|
|
+ projectDiff.setDiffPath(path);
|
|
|
+ projectDiffRepository.save(projectDiff);
|
|
|
+ }
|
|
|
+
|
|
|
+ public byte[] getDiffFile(String groupId, String artifactId, String fromVersion, String toVersion) throws IOException{
|
|
|
+ File mergedZipFile = new File(tmpdir, String.format("%s:%s:%s:%s.diff", groupId, artifactId, fromVersion, toVersion));
|
|
|
+ if (!mergedZipFile.exists()) {
|
|
|
+ List<ProjectDiff> diffs = projectDiffRepository.findByFromVersionAndToVersion(groupId, artifactId, fromVersion, toVersion);
|
|
|
+ if (!CollectionUtils.isEmpty(diffs)) {
|
|
|
+ if (diffs.size() == 1) {
|
|
|
+ return fileService.get(diffs.get(0).getDiffPath());
|
|
|
+ }
|
|
|
+ String targetDir = tmpdir + File.separator + StringUtils.uuid() + File.separator;
|
|
|
+ for (ProjectDiff diff:diffs) {
|
|
|
+ byte[] zipData = fileService.get(diff.getDiffPath());
|
|
|
+ ZipUtils.unzip(zipData, targetDir);
|
|
|
+ }
|
|
|
+ ZipUtils.zip(targetDir, mergedZipFile);
|
|
|
+ FileUtils.deleteDirectory(new File(targetDir));
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return FileCopyUtils.copyToByteArray(mergedZipFile);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|