All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cq1080.pages.PagesAutoConfiguration Maven / Gradle / Ivy

The newest version!
package com.cq1080.pages;


import com.cq1080.pages.bean.config.PageConfig;
import com.cq1080.pages.bean.vo.UserAndPassword;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.DigestUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.FileTemplateResolver;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Configuration
@ComponentScan(basePackages = "com.cq1080.pages")
@EnableConfigurationProperties(PageConfig.class)
public class PagesAutoConfiguration {

    public PagesAutoConfiguration(TemplateEngine templateEngine, SpringResourceTemplateResolver defaultTemplateResolver, PageConfig pageConfig) {
        if (!StringUtils.isEmpty(pageConfig.getPageRootPath())) {
            initPaths(pageConfig);
            addTemplateResolver(templateEngine, pageConfig.getPageRootPath() + "templates/", 1, "html");
            defaultTemplateResolver.setOrder(1);
            defaultTemplateResolver.setCacheable(false);
            templateEngine.addTemplateResolver(defaultTemplateResolver);
        }
    }

    private void initPaths(PageConfig pageConfig) {
        //模版路径
        if (!Files.exists(Paths.get(pageConfig.getTemplatePath()))) {
            try {
                Files.createDirectory(Paths.get(pageConfig.getTemplatePath()));
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
        ;
        //静态路径
        if (!Files.exists(Paths.get(pageConfig.getStaticPath()))) {
            try {
                Files.createDirectory(Paths.get(pageConfig.getStaticPath()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        ;
//        //更新缓存路径
//        if (!Files.exists(Paths.get(pageConfig.getCachePath()))) {
//            try {
//                Files.createDirectory(Paths.get(pageConfig.getCachePath()));
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }

        initUserInfo(pageConfig);
    }

    private void initUserInfo(PageConfig pageConfig) {
        File file = new File(pageConfig.getPageRootPath() + ".users");
        if (file.exists()) {
            return;
        }
        List users = new ArrayList<>();
        UserAndPassword userAndPassword = new UserAndPassword();
        userAndPassword.setAccount("cq1080");
        userAndPassword.setPassword(DigestUtils.md5DigestAsHex(pageConfig.getInitialPassword().getBytes()));
        users.add(userAndPassword);
        String data = new Gson().toJson(users);
        try {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(data.getBytes());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void addTemplateResolver(TemplateEngine templateEngine, String path, int order, String suffix) {
        FileTemplateResolver resolver = new FileTemplateResolver();
        resolver.setPrefix(path);
        resolver.setSuffix("." + suffix);
        resolver.setOrder(order);
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setCacheable(false);
        resolver.setCharacterEncoding("UTF-8");
        templateEngine.addTemplateResolver(resolver);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy