From 95ef76d6440a5363c0b7981b1333f82f9345322d Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Thu, 07 Jul 2022 15:15:56 +0800
Subject: [PATCH] fix template upload

---
 src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CmsTemplateServiceImpl.java |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 50 insertions(+), 6 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 807d28e..58c80e2 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,6 +1,7 @@
 package com.xcong.farmer.cms.modules.system.service.Impl;
 
 import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.util.IdUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.core.util.ZipUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -8,11 +9,14 @@
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 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.GlobalExceptionHandler;
+import com.xcong.farmer.cms.configurations.properties.CmsProperties;
 import com.xcong.farmer.cms.core.template.TemplateConfiguration;
 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.UserEntity;
 import com.xcong.farmer.cms.modules.system.mapper.CmsTemplateMapper;
 import com.xcong.farmer.cms.modules.system.service.ICmsTemplateService;
 import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
@@ -23,7 +27,12 @@
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -36,14 +45,14 @@
 public class CmsTemplateServiceImpl extends ServiceImpl<CmsTemplateMapper, CmsTemplateEntity> implements ICmsTemplateService {
 
     @Autowired
-    private TemplateConfiguration cfg;
+    private CmsProperties properties;
 
     private List<String> fileSuffix = Arrays.asList(".zip", ".html");
 
     @Override
     public void updateTemplate(MultipartFile upload) {
-        String templatePath = cfg.templatePath;
-        String staticPath = cfg.staticPath;
+        String templatePath = properties.getTemplatePath();
+        String staticPath = properties.getStaticPath();
 
         String filename = upload.getOriginalFilename();
         String suffix = filename.substring(filename.lastIndexOf("."));
@@ -69,6 +78,7 @@
 
                 for (File templateFile : files) {
                     if (!templateFile.isFile()) {
+                        FileUtil.move(templateFile, new File(FileUtils.path(staticPath, templateFile.getName())), true);
                         continue;
                     }
 
@@ -77,7 +87,7 @@
                     }
 
                     String name = templateFile.getName();
-                    if (!name.endsWith(".list.html") && !name.endsWith(".article.html") && !name.endsWith(".index.html")) {
+                    if (!name.endsWith(".list.html") && !name.endsWith(".article.html") && !name.endsWith("index.html")) {
                         continue;
                     }
 
@@ -93,7 +103,7 @@
                         cmsTemplate.setType(1);
                     }
                     cmsTemplate.setName(StrUtil.isNotBlank(attr) ? attr : templateFile.getName());
-                    cmsTemplate.setPath(templateFile.getName());
+                    cmsTemplate.setPath(IdUtil.simpleUUID()+templateFile.getName());
 
                     this.baseMapper.insert(cmsTemplate);
                 }
@@ -115,7 +125,7 @@
                     cmsTemplate.setType(1);
                 }
                 cmsTemplate.setName(StrUtil.isNotBlank(attr) ? attr : file.getName());
-                cmsTemplate.setPath(file.getName());
+                cmsTemplate.setPath(IdUtil.simpleUUID()+file.getName());
 
                 this.baseMapper.insert(cmsTemplate);
             }
@@ -139,4 +149,38 @@
         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) {
+        Long companyId = LoginUserUtil.getCompanyId();
+        CmsTemplateEntity cmsTemplateEntity = this.baseMapper.selectById(id);
+        String baseUrl = cmsProperties.getBaseUrl();
+        String templatePath = cmsProperties.getTemplatePath();
+        String htmlUrl = baseUrl + templatePath;
+        String pathName = htmlUrl + "\\" + cmsTemplateEntity.getPath();
+
+        byte[] bytes = new byte[0];
+        try {
+            bytes = Files.readAllBytes(Paths.get(pathName));
+        } catch (IOException e) {
+            e.printStackTrace();
+            return Result.ok("未找到模板");
+        }
+
+        String content = new String(bytes, StandardCharsets.UTF_8);
+        return Result.ok(content );
+    }
+
 }

--
Gitblit v1.9.1