|
@@ -8,15 +8,20 @@ import com.caimei.www.service.generate.GenerateHtml;
|
|
|
import com.caimei.www.service.page.BaseService;
|
|
|
import com.caimei.www.utils.RequestUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
+import org.springframework.core.io.support.ResourcePatternResolver;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
|
import org.thymeleaf.TemplateEngine;
|
|
|
import org.thymeleaf.spring5.context.webflux.SpringWebFluxContext;
|
|
|
-import reactor.core.publisher.Mono;
|
|
|
+import org.thymeleaf.util.StringUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.io.PrintWriter;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
@@ -53,7 +58,7 @@ public class GenerateHtmlImpl implements GenerateHtml {
|
|
|
* 生成静态首页
|
|
|
*/
|
|
|
@Override
|
|
|
- public Mono<String> generateStaticHome(ServerWebExchange exchange) {
|
|
|
+ public String generateStaticHome(ServerWebExchange exchange) {
|
|
|
// 上下文
|
|
|
SpringWebFluxContext context = new SpringWebFluxContext(exchange);
|
|
|
// 设置页面数据
|
|
@@ -66,10 +71,10 @@ public class GenerateHtmlImpl implements GenerateHtml {
|
|
|
try (PrintWriter writer = new PrintWriter(dest, "UTF-8")) {
|
|
|
// 生成html
|
|
|
templateEngine.process("index", context, writer);
|
|
|
- return Mono.just("[静态页服务]:生成静态首页成功! ^_^");
|
|
|
+ return "[静态页服务]:生成静态首页成功! ^_^";
|
|
|
} catch (Exception e) {
|
|
|
log.error("[静态页服务]:生成静态首页异常!", e);
|
|
|
- return Mono.just("[静态页服务]:生成静态首页异常!"+e.toString());
|
|
|
+ return "[静态页服务]:生成静态首页异常!"+e.toString();
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -77,16 +82,31 @@ public class GenerateHtmlImpl implements GenerateHtml {
|
|
|
* 删除静态页
|
|
|
*/
|
|
|
@Override
|
|
|
- public Mono<String> deleteStaticHome(String pageName) {
|
|
|
+ public String deleteStaticHome(String pageName) {
|
|
|
// 输出流
|
|
|
File dest = new File(destPath, pageName + ".html");
|
|
|
if (dest.exists()) {
|
|
|
boolean delete = dest.delete();
|
|
|
if (delete) {
|
|
|
- return Mono.just("[静态页服务]:删除静态页成功! ^_^");
|
|
|
+ return "[静态页服务]:删除静态页成功! ^_^";
|
|
|
}
|
|
|
}
|
|
|
- return Mono.just("[静态页服务]:删除静态页失败!");
|
|
|
+ return "[静态页服务]:删除静态页失败!";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拷贝静态资源文件
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String generateStaticFiles() {
|
|
|
+ // 拷贝静态资源文件
|
|
|
+ try {
|
|
|
+ copyResourceToFile();
|
|
|
+ return "[静态页服务]:拷贝静态资源文件成功! ^_^";
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("[静态页服务]:拷贝静态资源文件异常!", e);
|
|
|
+ return "[静态页服务]:拷贝静态资源文件异常!"+e.toString();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -136,5 +156,73 @@ public class GenerateHtmlImpl implements GenerateHtml {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 资源清单获取并拷贝
|
|
|
+ */
|
|
|
+ private void copyResourceToFile() throws IOException {
|
|
|
+ // 资源清单获取
|
|
|
+ ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
|
|
+ org.springframework.core.io.Resource[] resources = resolver.getResources("static/**");
|
|
|
+ for (org.springframework.core.io.Resource resource : resources) {
|
|
|
+ String fileName = resource.getFilename();
|
|
|
+ assert fileName != null;
|
|
|
+ if (fileName.indexOf(".") > 0) {
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ inputStream = resource.getInputStream();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn(String.format("[%s]获取输入流发生异常!", resource.getURL()));
|
|
|
+ throw new RuntimeException(String.format("[%s]获取输入流发生异常!", resource.getURL()));
|
|
|
+ }
|
|
|
+ //分析相对目录
|
|
|
+ String tempPath = "";
|
|
|
+ String pathFlag = "/static/";
|
|
|
+ String sUrl = resource.getURL().toString();
|
|
|
+ if (sUrl.startsWith("file:")) {
|
|
|
+ tempPath = sUrl.substring("file:".length() - 1);
|
|
|
+ }
|
|
|
+ int rootPathIndex = tempPath.indexOf(pathFlag);
|
|
|
+ if (rootPathIndex < 0) {
|
|
|
+ throw new RuntimeException("relativeRootPath有误:无法分析相对目录");
|
|
|
+ }
|
|
|
+ tempPath = tempPath.substring(rootPathIndex + pathFlag.length(), tempPath.length() - fileName.length());
|
|
|
+ if (StringUtils.isEmpty(tempPath)) {
|
|
|
+ tempPath = File.separator;
|
|
|
+ }
|
|
|
+ String filePath = destPath + File.separator + tempPath;
|
|
|
+ if (createDir(filePath)) {
|
|
|
+ String destName = filePath + fileName;
|
|
|
+ // 输出流
|
|
|
+ File dest = new File(destName);
|
|
|
+ if (dest.exists()) {
|
|
|
+ boolean delete = dest.delete();
|
|
|
+ }
|
|
|
+ FileUtils.copyInputStreamToFile(inputStream, dest);
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException(String.format("创建本地目录[%s]失败!", resource.getURL()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建目录
|
|
|
+ */
|
|
|
+ private static boolean createDir(String dirName) {
|
|
|
+ File dir = new File(dirName);
|
|
|
+ if (dir.exists()) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if (!dirName.endsWith(File.separator)) {
|
|
|
+ dirName = dirName + File.separator;
|
|
|
+ }
|
|
|
+ if (dir.mkdirs()) {
|
|
|
+ log.warn("创建目录" + dirName + "成功!");
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ log.warn("创建目录" + dirName + "失败!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|