package com.ss.android.ugc.effectmanager.common.cachemanager;

import X.4N2;
import X.8Kc;
import X.8r0;
import X.8r1;
import X.M12;
import X.Q39;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.ies.abmock.SettingsManager;
import com.ss.android.ugc.effectmanager.common.utils.FileUtils;
import java.io.File;
import java.io.InputStream;
import java.util.regex.Pattern;

public class FileICache implements ICache {
    public String cacheDir;

    static {
        Covode.recordClassIndex(125253);
    }

    public final String getCacheDir() {
        return this.cacheDir;
    }

    public void clear() {
        FileUtils.INSTANCE.removeDir(this.cacheDir);
    }

    public final void setCacheDir(String str) {
        M12.LIZ(str);
        this.cacheDir = str;
    }

    public FileICache(String str) {
        M12.LIZ(str);
        this.cacheDir = str;
    }

    public boolean has(String str) {
        M12.LIZ(str);
        return FileUtils.INSTANCE.checkFileExists(generatePath(str));
    }

    public void removePattern(String str) {
        M12.LIZ(str);
        removePattern(Pattern.compile(str));
    }

    private final String generatePath(String str) {
        return this.cacheDir + File.separator + str;
    }

    public InputStream queryToStream(String str) {
        InputStream fileStream;
        MethodCollector.i(15018);
        M12.LIZ(str);
        String generatePath = generatePath(str);
        synchronized (FileICache.class) {
            try {
                fileStream = FileUtils.INSTANCE.getFileStream(generatePath);
            } finally {
                MethodCollector.o(15018);
            }
        }
        return fileStream;
    }

    public boolean remove(String str) {
        boolean removeFile;
        MethodCollector.i(15217);
        M12.LIZ(str);
        synchronized (FileICache.class) {
            try {
                removeFile = FileUtils.INSTANCE.removeFile(generatePath(str));
            } finally {
                MethodCollector.o(15217);
            }
        }
        return removeFile;
    }

    public String queryToValue(String str) {
        String fileContent;
        MethodCollector.i(15016);
        M12.LIZ(str);
        String generatePath = generatePath(str);
        synchronized (FileICache.class) {
            try {
                fileContent = FileUtils.INSTANCE.getFileContent(generatePath);
                if (TextUtils.isEmpty(fileContent)) {
                    fileContent = Q39.LIZJ;
                }
            } finally {
                MethodCollector.o(15016);
            }
        }
        return fileContent;
    }

    public void removePattern(Pattern pattern) {
        MethodCollector.i(15219);
        if (pattern != null) {
            synchronized (FileICache.class) {
                try {
                    File[] listFiles = new File(this.cacheDir).listFiles(new FileICache$removePattern$$inlined$apply$lambda$1(pattern, this));
                    if (listFiles != null) {
                        for (File INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_cachemanager_FileICache_com_ss_android_ugc_aweme_storage_FileLancet_delete : listFiles) {
                            INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_cachemanager_FileICache_com_ss_android_ugc_aweme_storage_FileLancet_delete(INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_cachemanager_FileICache_com_ss_android_ugc_aweme_storage_FileLancet_delete);
                        }
                        MethodCollector.o(15219);
                    }
                } finally {
                    MethodCollector.o(15219);
                }
            }
        } else {
            MethodCollector.o(15219);
        }
    }

    public static boolean INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_cachemanager_FileICache_com_ss_android_ugc_aweme_storage_FileLancet_delete(File file) {
        8r1 r3;
        MethodCollector.i(15404);
        try {
            if (8Kc.LIZIZ()) {
                r3 = (8r1) SettingsManager.LIZ().LIZ("storage_intercepter_key", 8r1.class, 4N2.LIZ);
            } else {
                r3 = 4N2.LIZ;
            }
            if (8r0.LIZ(file.getAbsolutePath(), r3)) {
                8r0.LIZ(file, new RuntimeException(), "exception_delete_log", 8r0.LIZ(r3));
            }
            if (8r0.LIZJ(file.getAbsolutePath(), r3)) {
                8r0.LIZ(file, new RuntimeException(), "exception_handle", 8r0.LIZ(r3));
                MethodCollector.o(15404);
                return false;
            }
        } catch (Throwable unused) {
        }
        boolean delete = file.delete();
        MethodCollector.o(15404);
        return delete;
    }

    public long save(String str, InputStream inputStream) {
        long writeToExternal;
        MethodCollector.i(15015);
        M12.LIZ(str, inputStream);
        synchronized (FileICache.class) {
            try {
                writeToExternal = FileUtils.INSTANCE.writeToExternal(inputStream, generatePath(str));
            } finally {
                MethodCollector.o(15015);
            }
        }
        return writeToExternal;
    }

    public long save(String str, String str2) {
        long writeToExternal;
        MethodCollector.i(14804);
        M12.LIZ(str, str2);
        synchronized (FileICache.class) {
            try {
                writeToExternal = FileUtils.INSTANCE.writeToExternal(str2, generatePath(str));
            } finally {
                MethodCollector.o(14804);
            }
        }
        return writeToExternal;
    }
}
