File size: 1,896 Bytes
d04c26a
 
 
 
 
 
1740b0e
 
d04c26a
 
 
 
 
1740b0e
d04c26a
 
 
1740b0e
 
d04c26a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1740b0e
d04c26a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.jetbrains.help.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

public interface FileTools {

    static final Logger LOGGER = Logger.getLogger(FileTools.class.getName());

    ApplicationHome application = new ApplicationHome();


    static boolean fileExists(String path) {
        return getFile(path).exists();
    }

    static File getFile(String path) {
        File homeDir = application.getDir();
        File source = application.getSource();
        ClassPathResource classPathResource = new ClassPathResource(path);
        return ObjectUtil.isNull(source) ? FileUtil.file(classPathResource.getPath()) : FileUtil.file(homeDir, path);
    }

    static File getFileOrCreat(String path) {
        File file = getFile(path);
        LOGGER.info(String.format("source: %s", application.getSource()));
        if (ObjectUtil.isNotNull(application.getSource())) {
            ClassPathResource classPathResource = new ClassPathResource(path);
            File classPathFile = FileUtil.file(classPathResource.getPath());
            if (classPathResource.exists() && !file.exists()) {
                try {
                    FileUtil.writeFromStream(classPathResource.getInputStream(), classPathFile);
                } catch (Exception e) {
                    throw new IllegalArgumentException(CharSequenceUtil.format("{} File read failed", classPathFile.getPath()), e);
                }
                FileUtil.copy(classPathFile, file, true);
            }
        }
        return file;

    }
}