package com.bytedance.forest.chain.fetchers;

import X.2Pr;
import X.C1391Kbu;
import X.C1544KeN;
import X.C3117L8y;
import X.C4949LsC;
import X.L6L;
import X.L6P;
import X.L6S;
import X.L6W;
import X.M12;
import com.bytedance.covode.number.Covode;
import java.io.File;

public final class MemoryFetcher extends ResourceFetcher {
    static {
        Covode.recordClassIndex(26005);
    }

    public final void cancel() {
    }

    /* JADX INFO: super call moved to the top of the method (can break code semantics) */
    public MemoryFetcher(L6S l6s) {
        super(l6s);
        M12.LIZ((Object) l6s);
    }

    private final void recordFinish(L6L l6l) {
        l6l.LIZ("memory_finish", (Long) null);
    }

    private final void finishWithCallback(L6L l6l, C4949LsC<? super L6L, 2Pr> lsC) {
        recordFinish(l6l);
        lsC.invoke(l6l);
    }

    public final void fetchSync(C1544KeN keN, L6L l6l) {
        M12.LIZ((Object) keN, (Object) l6l);
        fetchAsync(keN, l6l, C3117L8y.LIZ);
    }

    public final void fetchAsync(C1544KeN keN, L6L l6l, C4949LsC<? super L6L, 2Pr> lsC) {
        M12.LIZ(keN, l6l, lsC);
        l6l.LIZ("memory_start", (Long) null);
        String LIZ = L6P.LIZIZ.LIZ(keN);
        if (LIZ == null) {
            l6l.LJI.LIZLLL(1, "cannot get cache identifier");
            finishWithCallback(l6l, lsC);
            return;
        }
        L6L LIZIZ = L6P.LIZIZ.LIZIZ(LIZ);
        if (LIZIZ == null) {
            l6l.LJI.LIZLLL(2, "could not found memory cache");
            finishWithCallback(l6l, lsC);
            return;
        }
        File file = new File(LIZIZ.LJII);
        if (!file.exists() || file.isDirectory()) {
            L6W l6w = l6l.LJI;
            l6w.LIZLLL(4, file.getAbsoluteFile() + " not exists or a directory");
            L6P.LIZIZ.LIZIZ(LIZIZ);
            finishWithCallback(l6l, lsC);
            return;
        }
        if (LIZIZ.LJIIIIZZ == C1391Kbu.CDN) {
            String str = LIZIZ.LJ.LJIL;
            if (LIZIZ.LJII != null) {
                L6S forest = getForest();
                M12.LIZ(forest, str, file);
                if (!file.exists() || forest.LIZJ.LIZ.LIZ(str, file)) {
                    L6P.LIZIZ.LIZIZ(LIZIZ);
                    l6l.LJI.LIZLLL(3, "cdn cache expired");
                    finishWithCallback(l6l, lsC);
                    return;
                }
            }
        }
        l6l.LJFF = true;
        l6l.LJII = LIZIZ.LJII;
        l6l.LJIIIIZZ = LIZIZ.LJIIIIZZ;
        l6l.LJIIJ = true;
        l6l.LJIIJJI = LIZIZ.LJIIJJI;
        byte[] LIZJ = L6P.LIZIZ.LIZJ(LIZ);
        if (LIZJ != null) {
            l6l.LIZ(LIZJ);
            l6l.LJIIIIZZ = C1391Kbu.MEMORY;
            l6l.LJIIIZ = LIZIZ.LJIIIIZZ;
        }
        finishWithCallback(l6l, lsC);
    }
}
