| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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(); |
| | | Long companyId = LoginUserUtil.getCompanyId(); |
| | | |
| | | String filename = upload.getOriginalFilename(); |
| | | String suffix = filename.substring(filename.lastIndexOf(".")); |
| | |
| | | |
| | | for (File templateFile : files) { |
| | | if (!templateFile.isFile()) { |
| | | FileUtil.move(templateFile, new File(staticPath), true); |
| | | continue; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | |
| | | Document parse = Jsoup.parse(templateFile, null); |
| | | String attr = parse.head().attr("name"); |
| | | CmsTemplateEntity cmsTemplate = new CmsTemplateEntity(); |
| | | cmsTemplate.setCompanyId(10L); |
| | | cmsTemplate.setCompanyId(companyId); |
| | | if (name.endsWith(".list.html")) { |
| | | cmsTemplate.setType(2); |
| | | } else if (name.endsWith(".article.html")) { |
| | |
| | | |
| | | if (".html".equals(suffix)) { |
| | | FileUtil.touch(file); |
| | | |
| | | Document parse = Jsoup.parse(file, null); |
| | | String attr = parse.head().attr("name"); |
| | | 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); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | |
| | | 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(); |
| | | CmsTemplateEntity cmsTemplateEntity = this.baseMapper.selectById(id); |
| | | String templatePath = cmsProperties.getTemplatePath(); |
| | | 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; |
| | | } |
| | | |
| | | } |