| | |
| | | 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.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.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.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.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.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.FileInputStream; |
| | | import java.io.FileOutputStream; |
| | | 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; |
| | | |
| | |
| | | continue; |
| | | } |
| | | |
| | | Document parse = Jsoup.parse(templateFile, null); |
| | | String attr = parse.head().attr("name"); |
| | | CmsTemplateEntity cmsTemplate = new CmsTemplateEntity(); |
| | | cmsTemplate.setCompanyId(companyId); |
| | | if (name.endsWith(".list.html")) { |
| | | cmsTemplate.setType(2); |
| | | } else if (name.endsWith(".article.html")) { |
| | | cmsTemplate.setType(3); |
| | | } else { |
| | | cmsTemplate.setType(1); |
| | | } |
| | | cmsTemplate.setName(StrUtil.isNotBlank(attr) ? attr : templateFile.getName()); |
| | | cmsTemplate.setPath(templateFile.getName()); |
| | | |
| | | this.baseMapper.insert(cmsTemplate); |
| | | insertTemplate(templateFile, companyId); |
| | | } |
| | | |
| | | } |
| | | |
| | | 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); |
| | | insertTemplate(file, companyId); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | throw new GlobalException("模板上传失败"); |
| | | } |
| | | } |
| | | |
| | | private void insertTemplate(File file, Long companyId) throws IOException { |
| | | Document parse = Jsoup.parse(file, null); |
| | | String attr = parse.head().attr("name"); |
| | | |
| | | staticPathParser(parse, "img", "src"); |
| | | staticPathParser(parse, "link", "href"); |
| | | staticPathParser(parse, "script", "src"); |
| | | |
| | | 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) { |
| | | 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() + attr); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | @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()); |
| | |
| | | bytes = Files.readAllBytes(Paths.get(pathName)); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | return Result.ok("未找到模板"); |
| | | return result.fail("未找到模板"); |
| | | } |
| | | |
| | | String content = new String(bytes, StandardCharsets.UTF_8); |
| | | return Result.ok(content ); |
| | | result.setData(content); |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public Result saveTemplateInfo(AdminSaveTemplateInfoDto adminSaveTemplateInfoDto) { |
| | | Long companyId = LoginUserUtil.getCompanyId(); |
| | | Long id = adminSaveTemplateInfoDto.getId(); |
| | | CmsTemplateEntity cmsTemplateEntity = this.baseMapper.selectByIdAndCompanyId(id,companyId); |
| | | String name = cmsTemplateEntity.getName(); |
| | | Integer type = cmsTemplateEntity.getType(); |
| | | String templatePath = cmsProperties.getTemplatePath(); |
| | | String path = cmsTemplateEntity.getPath(); |
| | | |
| | | this.baseMapper.delete(id,companyId); |
| | | String pathNew = FileUtils.path(templatePath, path); |
| | | 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("保存成功"); |
| | | } |
| | | |
| | | } |