Skip to content

Commit

Permalink
Store pictures in the database,fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
blackstar-baba committed Oct 11, 2020
1 parent b6f43f9 commit f68be7f
Show file tree
Hide file tree
Showing 26 changed files with 165 additions and 78 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.8</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.ant</groupId>-->
<!-- <artifactId>ant</artifactId>-->
<!-- <version>1.10.8</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Binary file added source.zip
Binary file not shown.
12 changes: 10 additions & 2 deletions src/main/assembly/assembly.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@
</includes>
<fileMode>0644</fileMode>
</fileSet>
<!-- <fileSet>-->
<!-- <directory>src/main/resources/source</directory>-->
<!-- <outputDirectory>source</outputDirectory>-->
<!-- <fileMode>0755</fileMode>-->
<!-- </fileSet>-->
<fileSet>
<directory>src/main/resources/source</directory>
<outputDirectory>source</outputDirectory>
<directory>.</directory>
<outputDirectory>.</outputDirectory>
<includes>
<include>source.zip</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,38 @@ public class SoftwareLabApplication {

private static final String DEFAULT_LOG_PATH = "/logs";

private static final String DATA_PATH = "--data.path";

public static void main(String[] args) {
args = process(args);
SpringApplication.run(SoftwareLabApplication.class, args);
}

private static String[] process(String[] args) {
// process
//log_path set
Optional<String> logArgs = Arrays.stream(args).filter(arg -> arg.startsWith(LOG_PATH)).findFirst();

if (logArgs.isPresent()) {
System.setProperty("log.path", logArgs.get().split("=")[1]);
// scan arg
String logArg = null;
String dataArg = null;
for (String arg : args) {
if (arg.startsWith(LOG_PATH)) {
logArg = arg;
} else if (arg.startsWith(DATA_PATH)) {
dataArg = arg;
}
}
//process log arg
if (logArg != null) {
System.setProperty("log.path", logArg.split("=")[1]);
} else {
System.setProperty("log.path", System.getProperty("user.dir")+DEFAULT_LOG_PATH);
System.setProperty("log.path", System.getProperty("user.dir") + DEFAULT_LOG_PATH);
}
//process data arg
if (dataArg != null) {
System.setProperty("data.path", logArg.split("=")[1]);
} else {
System.setProperty("data.path", System.getProperty("user.dir"));
}


return args;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public class ContainerInfo {
//e.g. user=123456
private List<ContainerEnvSetting> envs;

//e.g. http:https://ip:port/index.html
private String url;
//e.g. http:https://ip:port/index.html
private String home;


public ContainerInfo self() {
Expand Down
34 changes: 23 additions & 11 deletions src/main/java/com/blackstar/softwarelab/config/DataInitConfig.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,61 @@
package com.blackstar.softwarelab.config;

import com.blackstar.softwarelab.service.IAppSourceService;
import com.blackstar.softwarelab.util.ZipUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.init.ScriptUtils;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Configuration
@Slf4j
//todo rewrite: check table app_version
public class DataInitConfig {

private final String INIT_DATA_SCRIPT = "classpath:db/data.sql";
private final String INIT_SOURCE_FILE_NAME = "source.zip";

@Autowired
private DataSource datasource;

@Autowired
private IAppSourceService appSourceService;

@PostConstruct
public void init() throws Exception {
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource(INIT_DATA_SCRIPT);
Connection connection = datasource.getConnection();
if (!dataIsExist(connection)) {
log.info("begin init data from {}", INIT_DATA_SCRIPT);
ScriptUtils.executeSqlScript(connection, resource);
log.info("begin init data from {}", INIT_SOURCE_FILE_NAME);
//todo check package exists?
String dataDir = System.getProperty("data.path");
File file = new File(dataDir + File.separator + INIT_SOURCE_FILE_NAME);
if (file.exists()) {
//unzip to source package
String targetDir = dataDir+File.separator+"source";
ZipUtil.unZip(file.getPath(), targetDir, 1);
//load to db
appSourceService.loadToDb();
log.info("data init success: {}",INIT_SOURCE_FILE_NAME);
}else {
log.warn("can't find source: {}",INIT_SOURCE_FILE_NAME);
}
}

}

private boolean dataIsExist(Connection connection) {

boolean flag = false;
Statement statement = null;
ResultSet resultSet = null;
try {
statement = connection.createStatement();
resultSet = statement.executeQuery("select count(1) from sys_user");
resultSet = statement.executeQuery("select count(1) from app_version");
while (resultSet.next()) {
flag = resultSet.getInt(1) > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class UserController extends BaseController {
@Autowired
private ISysUserService userService;

@Value("softwarelab.user.admin.id")
@Value("${softwarelab.user.admin.id}")
private String adminId;

@RequestMapping(method = RequestMethod.GET, value = "/{id}")
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/blackstar/softwarelab/entity/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ public class App implements Serializable {

private Integer status;

private byte[] logo;

}
5 changes: 5 additions & 0 deletions src/main/java/com/blackstar/softwarelab/entity/AppSource.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.blackstar.softwarelab.entity;

import java.io.Serializable;
import java.time.LocalDateTime;

import com.baomidou.mybatisplus.annotation.TableId;
import lombok.*;
Expand Down Expand Up @@ -30,5 +31,9 @@ public class AppSource implements Serializable {

private String repository;

private LocalDateTime createTime;

private LocalDateTime updateTime;

private Integer status;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface IAppSourceService extends IService<AppSource> {
boolean upgrade();

boolean load(MultipartFile file);

boolean loadToDb();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blackstar.softwarelab.bean.AppSourceInfo;
import com.blackstar.softwarelab.bean.SourceRelease;
import com.blackstar.softwarelab.common.DbConst;
import com.blackstar.softwarelab.entity.App;
import com.blackstar.softwarelab.entity.AppSource;
import com.blackstar.softwarelab.entity.AppVersion;
import com.blackstar.softwarelab.mapper.AppSourceMapper;
import com.blackstar.softwarelab.service.IAppService;
import com.blackstar.softwarelab.service.IAppSourceService;
import com.blackstar.softwarelab.service.IAppVersionService;
import com.blackstar.softwarelab.util.FileUtil;
import com.blackstar.softwarelab.util.ZipUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
Expand All @@ -24,6 +26,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

Expand All @@ -45,6 +48,8 @@ public class AppSourceServiceImpl extends ServiceImpl<AppSourceMapper, AppSource

private String appsDir = targetDir + File.separator + "apps";

private String logosDir = targetDir + File.separator + "logos";

private int ignoreDepth = 1;

@Autowired
Expand All @@ -57,6 +62,9 @@ public class AppSourceServiceImpl extends ServiceImpl<AppSourceMapper, AppSource
@Autowired
private IAppVersionService appVersionService;

@Autowired
private IAppSourceService appSourceService;


@Override
public boolean upgrade() {
Expand Down Expand Up @@ -102,6 +110,7 @@ public boolean load(MultipartFile file) {

@Transactional
public boolean loadToDb() {
LocalDateTime now = LocalDateTime.now();
File appsFile = new File(appsDir);
String[] fileNames = appsFile.list();
for (String fileName : fileNames) {
Expand All @@ -113,18 +122,35 @@ public boolean loadToDb() {
.name(appName)
.type(appSourceInfo.getType())
.description(appSourceInfo.getDescription())
.additionalInfo(appSourceInfo.getAdditionalInfo().asText())
.additionalInfo(appSourceInfo.getAdditionalInfo().toString())
.logo(FileUtil.getContent(System.getProperty("data.path") + File.separator + logosDir + File.separator + appName + ".png"))
.createTime(now)
.updateTime(now)
.status(DbConst.STATUS_NORMAL)
.build());
List<AppSourceInfo.AppSourceVersion> versions = appSourceInfo.getVersions();
if (versions != null && versions.size() > 0) {
for (AppSourceInfo.AppSourceVersion appSourceVersion : versions) {
appVersionService.save(AppVersion.builder()
.appName(appName)
.version(appSourceVersion.getVersion())
.additionalInfo(appSourceVersion.getAdditionalInfo() != null ? appSourceVersion.getAdditionalInfo().asText() : null)
.additionalInfo(appSourceVersion.getAdditionalInfo() != null ? appSourceVersion.getAdditionalInfo().toString(): null)
.createTime(now)
.updateTime(now)
.status(DbConst.STATUS_NORMAL)
.downloadStatus(DbConst.DOWNLOAD_STATUS_INIT)
.build());
}
}
//todo
appSourceService.save(AppSource.builder()
.id("00000000-0000-0000-0000-000000000000")
.version("0.0.0")
.repository(null)
.createTime(now)
.updateTime(now)
.status(DbConst.STATUS_NORMAL)
.build());
} catch (IOException e) {
e.printStackTrace();
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ public void writeAppLogoToResponse(String app, HttpServletResponse response) {
log.error("get logo error",e);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class InstanceServiceImpl extends ServiceImpl<InstanceMapper, Instance> i
@Autowired
private IPortService portService;

@Value("softwarelab.host")
@Value("${softwarelab.host}")
private String host;


Expand Down Expand Up @@ -101,7 +101,7 @@ private void processUrl(ContainerInfo startContainerInfo) {
ports.forEach(containerPortSetting -> {
if(containerPortSetting.getType().equals("http")&& containerPortSetting.isEntrance()){
String url = startContainerInfo.getUrl() != null ? startContainerInfo.getUrl() : "";
startContainerInfo.setUrl("http:https://"+host+":"+containerPortSetting.getTargetPort()+url);
startContainerInfo.setHome("http:https://"+host+":"+containerPortSetting.getTargetPort()+url);
}
});
}
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/com/blackstar/softwarelab/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
import java.util.UUID;

public class FileUtil {
Expand Down Expand Up @@ -41,15 +38,23 @@ public static byte[] getContent(String filePath) {
if (!file.exists()) {
return bytes;
}else {
RandomAccessFile accessFile = null;
try {
FileInputStream fileInputStream = new FileInputStream(file);
bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes);
fileInputStream.close();
accessFile = new RandomAccessFile(file, "r");
bytes = new byte[Math.toIntExact(accessFile.length())];
accessFile.readFully(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (accessFile != null) {
try {
accessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return bytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private void processImageDownload(WebSocketSession session, String content) {
.build());
responseMessage = new WebSocketResponseMessage("success", content + "begin download");
} else {
//todo 如果数据库中下载状态为其他,设置为已下载
responseMessage = new WebSocketResponseMessage("success", content + "is exist");
}
try {
Expand Down
Loading

0 comments on commit f68be7f

Please sign in to comment.