Helius
2022-07-11 a508775d5c69e61e605c8f00fc18e70279444869
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CmsTemplateServiceImpl.java
@@ -15,7 +15,9 @@
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 org.jsoup.Jsoup;
@@ -46,6 +48,10 @@
    @Autowired
    private CmsProperties properties;
    @Autowired
    private CompanyMapper companyMapper;
    private List<String> fileSuffix = Arrays.asList(".zip", ".html");
    @Override
@@ -53,6 +59,11 @@
        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("."));
@@ -91,14 +102,14 @@
                        continue;
                    }
                    insertTemplate(templateFile, companyId);
                    insertTemplate(templateFile, companyId, companyCode);
                }
            }
            if (".html".equals(suffix)) {
                FileUtil.touch(file);
                insertTemplate(file, companyId);
                insertTemplate(file, companyId, companyCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
@@ -106,13 +117,13 @@
        }
    }
    private void insertTemplate(File file, Long companyId) throws IOException {
    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");
        staticPathParser(parse, "link", "href");
        staticPathParser(parse, "script", "src");
        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());
@@ -133,7 +144,7 @@
        this.baseMapper.insert(cmsTemplate);
    }
    private void staticPathParser(Document document, String tagName, String attrKey) {
    private void staticPathParser(Document document, String tagName, String attrKey, String companyCode) {
        Elements elements = document.getElementsByTag(tagName);
        if (elements.isEmpty()) {
            return;
@@ -142,7 +153,7 @@
        for (Element element : elements) {
            String attr = element.attr(attrKey);
            if (StrUtil.isNotBlank(attr) && !attr.contains("http://") && !attr.contains("https://")) {
                element.attr(attrKey, cmsProperties.getStaticPath() + attr);
                element.attr(attrKey, cmsProperties.getStaticUrl() + companyCode + "/" + attr);
            }
        }
    }