From 26d018d32e75438b764f4460cedb212027cec2d4 Mon Sep 17 00:00:00 2001
From: wzy <wzy19931122ai@163.com>
Date: Thu, 20 Oct 2022 23:20:36 +0800
Subject: [PATCH] fix
---
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CmsTemplateServiceImpl.java | 224 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 219 insertions(+), 5 deletions(-)
diff --git a/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CmsTemplateServiceImpl.java b/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CmsTemplateServiceImpl.java
index f1795c0..df4c3b5 100644
--- a/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CmsTemplateServiceImpl.java
+++ b/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CmsTemplateServiceImpl.java
@@ -1,33 +1,165 @@
package com.xcong.farmer.cms.modules.system.service.Impl;
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.io.file.FileWriter;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.core.util.ZipUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.xcong.farmer.cms.core.template.TemplateConfiguration;
+import com.xcong.farmer.cms.common.exception.GlobalException;
+import com.xcong.farmer.cms.common.response.Result;
+import com.xcong.farmer.cms.common.utils.FileUtils;
+import com.xcong.farmer.cms.configurations.properties.CmsProperties;
+import com.xcong.farmer.cms.modules.system.dto.AdminSaveTemplateInfoDto;
import com.xcong.farmer.cms.modules.system.dto.TemplateListDto;
import com.xcong.farmer.cms.modules.system.entity.CmsTemplateEntity;
+import com.xcong.farmer.cms.modules.system.entity.CompanyEntity;
import com.xcong.farmer.cms.modules.system.mapper.CmsTemplateMapper;
+import com.xcong.farmer.cms.modules.system.mapper.CompanyMapper;
import com.xcong.farmer.cms.modules.system.service.ICmsTemplateService;
import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.List;
/**
* @author wzy
* @date 2022-07-04
**/
+@Slf4j
@Service
public class CmsTemplateServiceImpl extends ServiceImpl<CmsTemplateMapper, CmsTemplateEntity> implements ICmsTemplateService {
@Autowired
- private TemplateConfiguration cfg;
+ private CmsProperties properties;
+
+ @Autowired
+ private CompanyMapper companyMapper;
+
+
+ private List<String> fileSuffix = Arrays.asList(".zip", ".html");
@Override
- public void updateTemplate(MultipartFile file) {
- String templatePath = cfg.templatePath;
- String staticPath = cfg.staticPath;
+ public void updateTemplate(MultipartFile upload) {
+ String templatePath = properties.getTemplatePath();
+ String staticPath = properties.getStaticPath();
+ Long companyId = LoginUserUtil.getCompanyId();
+ CompanyEntity company = companyMapper.selectById(companyId);
+
+ String companyCode = company.getCode();
+ templatePath = FileUtils.path(templatePath, companyCode);
+ staticPath = FileUtils.path(staticPath, companyCode);
+
+ String filename = upload.getOriginalFilename();
+ String suffix = filename.substring(filename.lastIndexOf("."));
+
+ if (!fileSuffix.contains(suffix)) {
+ throw new GlobalException("请上传正确格式文件");
+ }
+
+ try {
+ String path = FileUtils.path(templatePath, filename);
+ File file = new File(path);
+ upload.transferTo(file);
+
+ if (".zip".equals(suffix)) {
+ ZipUtil.unzip(file, new File(templatePath));
+ FileUtil.del(file);
+
+ File template = new File(templatePath);
+ File[] files = template.listFiles();
+ if (files == null) {
+ return;
+ }
+
+ FileUtil.touch(new File(staticPath + "/empty.txt"));
+ for (File templateFile : files) {
+ if (!templateFile.isFile()) {
+ FileUtil.move(templateFile, new File(staticPath), true);
+ continue;
+ }
+
+ if (!"html".equals(FileUtil.extName(templateFile))) {
+ continue;
+ }
+
+ String name = templateFile.getName();
+ if (!name.endsWith(".list.html") && !name.endsWith(".article.html") && !name.endsWith("index.html")) {
+ continue;
+ }
+
+ insertTemplate(templateFile, companyId, companyCode);
+ }
+ }
+
+ if (".html".equals(suffix)) {
+ FileUtil.touch(file);
+
+ insertTemplate(file, companyId, companyCode);
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw new GlobalException("模板上传失败");
+ }
+ }
+
+ private void insertTemplate(File file, Long companyId, String companyCode) throws IOException {
+ Document parse = Jsoup.parse(file, null);
+ String attr = parse.head().attr("name");
+
+ staticPathParser(parse, "img", "src", companyCode);
+ staticPathParser(parse, "link", "href", companyCode);
+ staticPathParser(parse, "script", "src", companyCode);
+
+ FileOutputStream outputStream = new FileOutputStream(file);
+ outputStream.write(parse.html().getBytes());
+ outputStream.close();
+
+ CmsTemplateEntity cmsTemplate = new CmsTemplateEntity();
+ cmsTemplate.setCompanyId(companyId);
+ if (file.getName().endsWith(".list.html")) {
+ cmsTemplate.setType(2);
+ } else if (file.getName().endsWith(".article.html")) {
+ cmsTemplate.setType(3);
+ } else {
+ cmsTemplate.setType(1);
+ }
+ cmsTemplate.setName(StrUtil.isNotBlank(attr) ? attr : file.getName());
+ cmsTemplate.setPath(file.getName());
+
+ this.baseMapper.insert(cmsTemplate);
+ }
+
+ private void staticPathParser(Document document, String tagName, String attrKey, String companyCode) {
+ Elements elements = document.getElementsByTag(tagName);
+ if (elements.isEmpty()) {
+ return;
+ }
+
+ for (Element element : elements) {
+ String attr = element.attr(attrKey);
+ if (StrUtil.isNotBlank(attr) && !attr.contains("http://") && !attr.contains("https://")) {
+ element.attr(attrKey, cmsProperties.getStaticUrl() + companyCode + "/" + attr);
+ }
+ }
}
@Override
@@ -45,4 +177,86 @@
Long companyId = LoginUserUtil.getCompanyId();
this.baseMapper.delete(id, companyId);
}
+
+ @Override
+ public Result dropdownList() {
+ Long companyId = LoginUserUtil.getCompanyId();
+ QueryWrapper<CmsTemplateEntity> objectQueryWrapper = new QueryWrapper<>();
+ objectQueryWrapper.eq("company_id", companyId);
+ List<CmsTemplateEntity> cmsTemplateEntities = this.baseMapper.selectList(objectQueryWrapper);
+ return Result.ok(cmsTemplateEntities);
+ }
+
+ @Autowired
+ private CmsProperties cmsProperties;
+
+ @Override
+ public Result viewTemplateInfo(Long id) {
+ Result result = new Result();
+
+ CompanyEntity companyEntity = companyMapper.selectById(LoginUserUtil.getCompanyId());
+ CmsTemplateEntity cmsTemplateEntity = this.baseMapper.selectById(id);
+ String templatePath = FileUtils.path(cmsProperties.getTemplatePath(), companyEntity.getCode());
+ String pathName = FileUtils.path(templatePath, cmsTemplateEntity.getPath());
+
+ byte[] bytes = new byte[0];
+ try {
+ bytes = Files.readAllBytes(Paths.get(pathName));
+ } catch (IOException e) {
+ e.printStackTrace();
+ return result.fail("未找到模板");
+ }
+
+ String content = new String(bytes, StandardCharsets.UTF_8);
+ result.setData(content);
+ return result;
+ }
+
+ @Override
+ @Transactional
+ public Result saveTemplateInfo(AdminSaveTemplateInfoDto adminSaveTemplateInfoDto) {
+ Long companyId = LoginUserUtil.getCompanyId();
+ Long id = adminSaveTemplateInfoDto.getId();
+
+ CompanyEntity company = this.companyMapper.selectById(companyId);
+ CmsTemplateEntity cmsTemplateEntity = this.baseMapper.selectByIdAndCompanyId(id, companyId);
+ String name = cmsTemplateEntity.getName();
+ Integer type = cmsTemplateEntity.getType();
+ String templatePath = FileUtils.path(cmsProperties.getTemplatePath(), company.getCode());
+ String path = cmsTemplateEntity.getPath();
+
+ this.baseMapper.delete(id, companyId);
+ String pathNew = FileUtils.path(templatePath, path);
+ log.info("模板写入地址:{}", pathNew);
+ File file = new File(pathNew);
+ FileUtil.touch(file);
+
+ //文件写入,直接覆盖
+ FileWriter writer = new FileWriter(file);
+ writer.write(adminSaveTemplateInfoDto.getTemplateInfo(), false);
+
+ CmsTemplateEntity cmsTemplate = new CmsTemplateEntity();
+ cmsTemplate.setCompanyId(companyId);
+ cmsTemplate.setType(type);
+ cmsTemplate.setName(name);
+ cmsTemplate.setPath(path);
+ this.baseMapper.insert(cmsTemplate);
+
+ return Result.ok("保存成功");
+ }
+
+ @Override
+ public Result downloadTemplate() {
+ Long companyId = LoginUserUtil.getCompanyId();
+ CompanyEntity company = this.companyMapper.selectById(companyId);
+
+ String templatePath = FileUtils.path(cmsProperties.getTemplatePath(), company.getCode());
+ String staticPath = FileUtils.path(cmsProperties.getStaticPath(), company.getCode());
+ String downloadPath = FileUtils.path(cmsProperties.getDownloadPath(), company.getCode());
+
+ String fileName = "template.zip";
+ ZipUtil.zip(FileUtil.file(downloadPath + "/template.zip"), true, FileUtil.file(templatePath), FileUtil.file(staticPath));
+ String url = cmsProperties.getStaticUrl() + "download/" + company.getCode();
+ return Result.ok("success", url +"/" + fileName);
+ }
}
--
Gitblit v1.9.1