Commit 337305b1 by zhongmh

增加文件浏览功能

parent 449da40e
......@@ -4,10 +4,14 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.yunmaozj.maven.plugins</groupId>
<artifactId>site-upload-maven-plugin</artifactId>
<version>1.0.0-SNAPSHOT</version>
<groupId>cn.yunmaozj.tools</groupId>
<artifactId>maven-site-upload-parent</artifactId>
<version>1.0.0</version>
<description>站点上传插件</description>
<modules>
<module>site-upload-maven-plugin</module>
<module>site-server</module>
</modules>
<packaging>pom</packaging>
<url>http://git.cqwzxt.com/opensource/site-upload-maven-plugin</url>
<properties>
......@@ -20,40 +24,16 @@
<artifactId>okhttp</artifactId>
<version>1.3.9-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-artifact-transfer</artifactId>
<version>0.10.0</version>
</dependency>
<!-- Upgrade of transitive dependency. -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<!-- dependencies to annotations -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.76.96</version>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>maven-site-upload-parent</artifactId>
<groupId>cn.yunmaozj.tools</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>cn.yunmaozj.tools</groupId>
<artifactId>site-server</artifactId>
<properties>
<docker.registry>127.0.0.1:5000</docker.registry>
<docker.repository>${project.artifactId}</docker.repository>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.19</version>
</dependency>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>-->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<targetPath>${basedir}/target</targetPath>
<includes>
<include>Dockerfile</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.5</version>
<executions>
<execution>
<goals>
<goal>
revision
</goal>
</goals>
<phase>initialize</phase>
</execution>
</executions>
<configuration>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<injectAllReactorProjects>true</injectAllReactorProjects>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.76.96</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
<configuration>
<dockerDirectory>${basedir}/target</dockerDirectory>
<imageName>
${docker.registry}/${docker.repository}:${maven.build.timestamp}_${git.commit.id.abbrev}
</imageName>
</configuration>
</execution>
<execution>
<id>tag-image</id>
<phase>package</phase>
<goals>
<goal>tag</goal>
</goals>
<configuration>
<image>
${docker.registry}/${docker.repository}:${maven.build.timestamp}_${git.commit.id.abbrev}
</image>
<newName>${docker.registry}/${docker.repository}:latest</newName>
</configuration>
</execution>
<execution>
<id>push-image</id>
<phase>package</phase>
<goals>
<goal>push</goal>
</goals>
<configuration>
<imageName>${docker.registry}/${docker.repository}:latest</imageName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package cn.yunmaozj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* @author zhongminghong
* @email yunmaozj@163.com
* @create 2019-09-10 11:26
*/
@EnableWebMvc
@SpringBootApplication
public class SiteServer {
public static void main(String[] args) {
SpringApplication.run(SiteServer.class, args);
}
}
package cn.yunmaozj.common;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
import org.apache.el.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.*;
import java.util.Enumeration;
import java.util.Stack;
/**
* @author zhongminghong
* @email yunmaozj@163.com
* @create 2019-09-10 16:43
*/
@Component
public class SystemFile {
private final Logger logger = LoggerFactory.getLogger(SystemFile.class);
private String applicationName = "site-server-www";
private final File workDir;
public SystemFile() {
String homeDir = System.getProperty("user.home");
workDir = new File(homeDir, applicationName);
logger.info("服务器工作目录:{}", workDir.getPath());
if (!workDir.exists()) {
workDir.mkdirs();
}
}
public File getWorkDir() {
return workDir;
}
/**
* 加压文件
*
* @param fileName
* @param inputStream
*/
public void unzip(String fileName, InputStream inputStream) throws IOException {
// ArchiveEntry archiveEntry = null;
File unzipFile = new File(workDir, fileName);//临时解压目录
logger.info("解压目录:{}", unzipFile);
if (unzipFile.exists()) {
File currentFile = new File(unzipFile.getPath());
Stack<File> fileStack = new Stack<File>();
fileStack.push(currentFile);
do {
File[] listFiles = currentFile.listFiles();
if (listFiles == null || listFiles.length == 0) {
fileStack.pop().delete();
} else {
currentFile = fileStack.peek();
for (File file : listFiles) {
if (file.isDirectory()) {
fileStack.push(file);
} else {
file.delete();
}
}
}
} while (fileStack.size() > 0);
}
unzipFile.mkdirs();
SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(IOUtils.toByteArray(inputStream));
ZipFile zipFile = new ZipFile(channel);
Enumeration<ZipArchiveEntry> enumeration = zipFile.getEntries();
while (enumeration.hasMoreElements()){
ZipArchiveEntry archiveEntry = enumeration.nextElement();
File file = new File(unzipFile, archiveEntry.getName());
if (!file.getParentFile().exists()) {
//创建父目录
file.getParentFile().mkdirs();
}
OutputStream outputStream = new FileOutputStream(file);
outputStream.write(IOUtils.toByteArray(zipFile.getInputStream(archiveEntry)));
outputStream.flush();
outputStream.close();
}
zipFile.close();
/*ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(inputStream,"utf-8");
while ((archiveEntry = zipArchiveInputStream.getNextEntry()) != null) {
File file = new File(unzipFile, archiveEntry.getName());
if (!file.getParentFile().exists()) {
//创建父目录
file.getParentFile().mkdirs();
}
OutputStream outputStream = new FileOutputStream(file);
long size = archiveEntry.getSize();
byte[] content = new byte[(int) size];
zipArchiveInputStream.read(content);
outputStream.write(content);
outputStream.flush();
outputStream.close();
}*/
}
}
package cn.yunmaozj.configuration;
import cn.yunmaozj.common.SystemFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @author zhongminghong
* @email yunmaozj@163.com
* @create 2019-09-10 17:53
*/
@Configuration
public class SiteServierMvcConfiguration implements WebMvcConfigurer {
@Autowired
private SystemFile systemFile;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println(systemFile.getWorkDir().toURI());
registry.addResourceHandler("/**/**").addResourceLocations(systemFile.getWorkDir().toURI().toString());
}
}
package cn.yunmaozj.controller;
import cn.yunmaozj.common.SystemFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.File;
/**
* @author zhongminghong
* @email yunmaozj@163.com
* @create 2019-09-10 11:28
*/
@Controller
public class IndexController {
@Autowired
private SystemFile systemFile;
@GetMapping({"index.html", "/"})
@ResponseBody
public String index() {
File[] files = systemFile.getWorkDir().listFiles();
StringBuilder sb = new StringBuilder();
sb.append("<ul>");
for (File file : files) {
sb.append("<li>").append("<a ").append("href=\'").append(file.getName())
.append("\\index.html")
.append("\'").append(">").append(file.getName()).append("</a>");
}
sb.append("</ul>");
return sb.toString();
}
}
package cn.yunmaozj.controller;
import cn.yunmaozj.common.SystemFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
import java.io.IOException;
/**
* @author zhongminghong
* @email yunmaozj@163.com
* @create 2019-09-10 16:26
*/
@RestController
public class UploadController {
@Autowired
private SystemFile systemFile;
@PostMapping("/site-upload")
public void siteUpload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
systemFile.unzip(multipartFile.getResource().getFilename(), multipartFile.getInputStream());
}
}
FROM daocloud.io/java
ENV APP_NAME @project.build.finalName@.@project.packaging@
ENV APP_VERSION @project.version@
EXPOSE 51420
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
RUN mkdir /home/logs/
VOLUME ["/home/java/logs"]
ADD $APP_NAME $APP_NAME
CMD java -Xmx320m -Xms320m -jar $APP_NAME
\ No newline at end of file
spring.application.name=site-server
server.port=51420
logging.level.org.springframework=debug
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>maven-site-upload-parent</artifactId>
<groupId>cn.yunmaozj.tools</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>cn.yunmaozj.tools</groupId>
<artifactId>site-upload-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<properties>
<boliyuan.parent.version>1.1.5</boliyuan.parent.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<repositories>
<repository>
<id>yunmaozj</id>
<url>http://nexus.cqwzxt.com/repository/yunmaozj/</url>
</repository>
</repositories>
<distributionManagement>
<snapshotRepository>
<id>snapshots</id>
<url>http://nexus.cqwzxt.com/repository/snapshots/</url>
</snapshotRepository>
<repository>
<id>releases</id>
<url>http://nexus.cqwzxt.com/repository/releases/</url>
</repository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>1.3.9-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-artifact-transfer</artifactId>
<version>0.10.0</version>
</dependency>
<!-- Upgrade of transitive dependency. -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<!-- dependencies to annotations -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.76.96</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package cn.yunmaozj.upload;
import okhttp3.*;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.util.StringUtils;
import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author zhongminghong
* @email yunmaozj@163.com
* @create 2019-09-10 11:30
*/
@Mojo(name = "upload", threadSafe = true)
public class SiteUploadMojo extends AbstractMojo {
@Parameter(property = "inputDirectory", defaultValue = "${project.version}", required = true)
private File inputDirectory;
@Parameter(property = "proxyHost")
private String proxyHost;
@Parameter(property = "proxyPort")
private int proxyPort;
@Parameter(property = "proxyUserName")
private String proxyUserName;
@Parameter(property = "proxyPassword")
private String proxyPassword;
@Parameter(property = "ssl", required = false)
private boolean ssl;
@Parameter(property = "siteServerHost", required = true)
private String siteServerHost;
@Parameter(property = "siteServerPort", required = true)
private int siteServerPort;
@Parameter(property = "siteServerUserName")
private String siteServerUserName;
@Parameter(property = "siteServerPassword")
private String siteServerPassword;
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
@Parameter(defaultValue = "${settings}", readonly = true, required = true)
private Settings settings;
public void execute() throws MojoExecutionException, MojoFailureException {
if (!inputDirectory.exists()) {
getLog().info("资源目录不存在[" + inputDirectory + "]");
return;
}
getLog().info("输入目录:" + inputDirectory);
try {
File outFile = compress();
uploader(outFile);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 上传压缩文件的地方
*/
private void uploader(File outFile) throws MojoExecutionException, IOException {
if (!outFile.exists()) {
throw new MojoExecutionException("上传的文件[" + outFile.getPath() + "]不存在");
}
if (outFile.isDirectory()) {
throw new MojoExecutionException("上传的文件[" + outFile.getPath() + "]的类型是一个目录");
}
OkHttpClient.Builder builder = new OkHttpClient.Builder();
RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), outFile);
MultipartBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", project.getName(), fileBody)
.build();
if (StringUtils.isNotEmpty(siteServerUserName) && StringUtils.isNotEmpty(siteServerPassword)) {
builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
.proxyAuthenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
if (response.request().header("Proxy-Authorization") != null) {
getLog().error("response.request().header(\"Proxy-Authorization\") != null");
return null;
}
String credential = Credentials.basic(proxyUserName, proxyPassword);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
});
}
OkHttpClient client = builder.build();
HttpUrl.Builder httpUrlBuilder = new HttpUrl.Builder();
if (ssl) {
httpUrlBuilder.scheme("https");
} else {
httpUrlBuilder.scheme("http");
}
httpUrlBuilder.host(siteServerHost).port(siteServerPort);
if (StringUtils.isNotEmpty(siteServerUserName) && StringUtils.isNotEmpty(siteServerPassword)) {
httpUrlBuilder.username(siteServerUserName).password(siteServerPassword);
}
httpUrlBuilder.addPathSegment("site-upload");
Request request = new Request.Builder().url(httpUrlBuilder.build())
.post(body).build();
Response response = client.newCall(request).execute();
getLog().info(response.toString());
}
/**
* 压缩文件
*/
private File compress() throws IOException {
Stack<File> directoryStack = new Stack<>();
directoryStack.push(inputDirectory);
File currentDirectoryFile = null;
List<File> fileList = new ArrayList<File>();
do {
currentDirectoryFile = directoryStack.pop();
File[] files = currentDirectoryFile.listFiles();
Arrays.stream(files).forEach(new Consumer<File>() {
@Override
public void accept(File file) {
if (file.isDirectory()) {
directoryStack.push(file);
} else {
fileList.add(file);
}
}
});
} while (directoryStack.size() > 0);
String parentName = inputDirectory.getName();
getLog().info("当前总共需要压缩的文件数量:" + fileList.size());
File outputDirectory = new File(project.getBuild().getDirectory(), "site-upload");
if (!outputDirectory.exists()) {
outputDirectory.mkdirs();
}
File outFile = new File(outputDirectory, project.getName() + ".zip");