CamScanner.apk(点击下载) / BaiduCloudApi.java


package com.intsig.webstorage.baidu;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.ArrayMap;
import com.facebook.appevents.AppEventsConstants;
import com.intsig.log.LogUtils;
import com.intsig.okgo.utils.GsonUtils;
import com.intsig.scanner.ScannerFormat;
import com.intsig.utils.FileUtil;
import com.intsig.utils.ListUtils;
import com.intsig.utils.MD5Utils;
import com.intsig.utils.net.OkHttpUtil;
import com.intsig.webstorage.RemoteFile;
import com.intsig.webstorage.UploadProgressListener;
import com.intsig.webstorage.WebStorageApi;
import com.intsig.webstorage.WebstorageException;
import com.intsig.webstorage.baidu.BaiduListModel;
import com.intsig.webstorage.util.CloudServiceUtils;
import com.intsig.webstorage.util.ParamCheckUtil;
import com.microsoft.services.msa.QueryParameters;
import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Response;

public class BaiduCloudApi extends WebStorageApi {

    /* renamed from: c  reason: collision with root package name */
    private String f41002c;

    /* renamed from: d  reason: collision with root package name */
    private String f41003d;

    /* renamed from: e  reason: collision with root package name */
    private String f41004e;

    /* renamed from: f  reason: collision with root package name */
    private String f41005f;

    /* renamed from: g  reason: collision with root package name */
    private long f41006g;

    public BaiduCloudApi(Context context) {
        super(context, 6);
        q();
    }

    private List<RemoteFile> n(List<BaiduListModel.ListBean> list) {
        ArrayList arrayList = new ArrayList();
        if (list == null || list.size() <= 0) {
            RemoteFile remoteFile = new RemoteFile();
            remoteFile.f40981e = "/apps/扫描全能王/CamScanner";
            remoteFile.f40980d = 0;
            arrayList.add(remoteFile);
        } else {
            CloudServiceUtils.a("BaiduCloudApi", "fileInfo.size=" + list.size());
            for (BaiduListModel.ListBean listBean : list) {
                RemoteFile remoteFile2 = new RemoteFile();
                CloudServiceUtils.a("BaiduCloudApi", "fileInfo.getPath = " + listBean.getPath());
                remoteFile2.f40979c = new File(listBean.getPath());
                remoteFile2.f40978b = listBean.getServer_filename();
                remoteFile2.f40981e = listBean.getPath();
                int i10 = 1;
                if (listBean.getIsdir() == 1) {
                    i10 = 0;
                }
                remoteFile2.f40980d = i10;
                arrayList.add(remoteFile2);
            }
        }
        return arrayList;
    }

    private int o(String str, long j2, String str2, String str3) {
        String str4 = "https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=" + this.f41002c;
        ArrayMap arrayMap = new ArrayMap();
        arrayMap.put("isdir", AppEventsConstants.EVENT_PARAM_VALUE_NO);
        arrayMap.put("block_list", str3);
        arrayMap.put("path", str);
        arrayMap.put("uploadid", str2);
        arrayMap.put(ScannerFormat.TAG_CANVAS_SIZE, String.valueOf(j2));
        int i10 = -1;
        try {
            Response f10 = OkHttpUtil.t().f(str4, arrayMap);
            if (f10 != null) {
                i10 = f10.p();
                String string = f10.e().string();
                LogUtils.a("BaiduCloudApi", "createFile = " + string);
                BaiduCreateModel baiduCreateModel = (BaiduCreateModel) GsonUtils.b(string, BaiduCreateModel.class);
                if (baiduCreateModel != null && baiduCreateModel.getErrno() == 0) {
                    return 0;
                }
            }
        } catch (Exception e10) {
            LogUtils.e("BaiduCloudApi", e10);
        }
        return i10;
    }

    private String p(File file) {
        ArrayList arrayList = new ArrayList();
        int length = file.length() % 4194304 == 0 ? (int) (file.length() / 4194304) : ((int) (file.length() / 4194304)) + 1;
        for (int i10 = 1; i10 <= length; i10++) {
            long j2 = ((long) (i10 - 1)) * 4194304;
            if (j2 < 0) {
                j2 = 0;
            }
            byte[] m10 = FileUtil.m(j2, file, new byte[4194304]);
            if (m10 != null) {
                String c9 = MD5Utils.c(m10);
                if (!TextUtils.isEmpty(c9)) {
                    arrayList.add(c9);
                }
            }
        }
        return GsonUtils.e(arrayList);
    }

    private void q() {
        SharedPreferences sharedPreferences = this.f40999a.getSharedPreferences("camscanner", 0);
        this.f41006g = sharedPreferences.getLong("1353051907231", 0);
        String string = sharedPreferences.getString("dx6d", "");
        this.f41005f = string;
        if (!TextUtils.isEmpty(string)) {
            if (Math.abs(System.currentTimeMillis() - this.f41006g) + BaiduConstant.f41007a > Long.parseLong(this.f41005f) * 1000) {
                BaiduAuthUtil.a(this.f40999a);
                return;
            }
        }
        this.f41002c = sharedPreferences.getString("de3vr", "");
        this.f41003d = sharedPreferences.getString("see133", "");
        this.f41005f = sharedPreferences.getString("dx6d", "");
        this.f41004e = sharedPreferences.getString("karant", "");
    }

    private int r(File file, String str, String str2, List<String> list) {
        ArrayList arrayList;
        ArrayList arrayList2 = new ArrayList();
        long length = file.length();
        try {
            if (ListUtils.b(list)) {
                ArrayList arrayList3 = new ArrayList();
                arrayList3.add(AppEventsConstants.EVENT_PARAM_VALUE_NO);
                arrayList = arrayList3;
            } else {
                arrayList = list;
            }
            if (arrayList.size() == 1) {
                String t10 = t(file, str, str2, null, AppEventsConstants.EVENT_PARAM_VALUE_NO);
                if (!TextUtils.isEmpty(t10)) {
                    arrayList2.add(t10);
                }
            } else {
                s(file, str, str2, arrayList, arrayList2);
            }
            if (!ListUtils.b(arrayList2)) {
                return o(str, length, str2, GsonUtils.e(arrayList2));
            }
            return -1;
        } catch (Exception e10) {
            LogUtils.e("BaiduCloudApi", e10);
            return -1;
        }
    }

    private void s(File file, String str, String str2, List<String> list, List<String> list2) throws Exception {
        int min = Math.min(file.length() % 4194304 == 0 ? (int) (file.length() / 4194304) : ((int) (file.length() / 4194304)) + 1, list != null ? list.size() - 1 : 0);
        for (int i10 = 0; i10 <= min; i10++) {
            long j2 = ((long) i10) * 4194304;
            if (j2 < 0) {
                j2 = 0;
            }
            byte[] m10 = FileUtil.m(j2, file, new byte[4194304]);
            if (m10 != null) {
                String t10 = t(file, str, str2, m10, ListUtils.b(list) ? AppEventsConstants.EVENT_PARAM_VALUE_NO : list.get(i10));
                if (!TextUtils.isEmpty(t10)) {
                    list2.add(t10);
                }
            }
        }
    }

    private String t(File file, String str, String str2, byte[] bArr, String str3) throws Exception {
        Response response;
        String str4 = "https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?method=upload&type=tmpfile&access_token=" + this.f41002c + "&path=" + URLEncoder.encode(str, "utf-8") + "&uploadid=" + str2 + "&partseq=" + str3;
        if (bArr == null) {
            ArrayMap arrayMap = new ArrayMap();
            arrayMap.put("file", file);
            response = OkHttpUtil.t().k(str4, null, arrayMap);
        } else {
            response = OkHttpUtil.t().j(str4, "file", file.getName(), bArr);
        }
        if (response != null) {
            String string = response.e().string();
            LogUtils.a("BaiduCloudApi", "upload piece body = " + string);
            BaiduSuperfileModel baiduSuperfileModel = (BaiduSuperfileModel) GsonUtils.b(string, BaiduSuperfileModel.class);
            if (baiduSuperfileModel != null) {
                return baiduSuperfileModel.getMd5();
            }
        }
        return null;
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public boolean b(int i10) {
        q();
        if (!TextUtils.isEmpty(this.f41002c)) {
            return true;
        }
        this.f40999a.startActivity(new Intent(this.f40999a, BaiduAuthActivity.class));
        return false;
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public int c(RemoteFile remoteFile, RemoteFile remoteFile2) {
        String str;
        String str2 = "https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=" + this.f41002c;
        ArrayMap arrayMap = new ArrayMap();
        arrayMap.put("isdir", "1");
        arrayMap.put("rtype", AppEventsConstants.EVENT_PARAM_VALUE_NO);
        if (remoteFile == null || TextUtils.isEmpty(remoteFile.f40978b)) {
            str = "/apps/扫描全能王/CamScanner";
        } else {
            str = "/apps/扫描全能王/" + remoteFile.f40978b;
        }
        arrayMap.put("path", str);
        int i10 = -1;
        try {
            Response f10 = OkHttpUtil.t().f(str2, arrayMap);
            if (f10 != null) {
                i10 = f10.p();
                String string = f10.e().string();
                CloudServiceUtils.a("BaiduCloudApi", "createFolder.body = " + string);
                BaiduCreateModel baiduCreateModel = (BaiduCreateModel) GsonUtils.b(string, BaiduCreateModel.class);
                if (baiduCreateModel != null) {
                    if (baiduCreateModel.getErrno() != 0) {
                        if (baiduCreateModel.getErrno() != -8) {
                            return -4;
                        }
                    }
                    return 0;
                }
            }
        } catch (Exception e10) {
            LogUtils.e("BaiduCloudApi", e10);
        }
        return i10;
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public String g() {
        return this.f40999a.getSharedPreferences("camscanner", 0).getString("karant", "");
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public boolean h() {
        q();
        return !TextUtils.isEmpty(this.f41002c);
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public List<RemoteFile> i(RemoteFile remoteFile) throws WebstorageException {
        if (!TextUtils.isEmpty(this.f41002c)) {
            ArrayList arrayList = new ArrayList();
            String str = (remoteFile == null || TextUtils.isEmpty(remoteFile.f40981e)) ? "/apps/扫描全能王" : remoteFile.f40981e;
            ArrayMap arrayMap = new ArrayMap();
            arrayMap.put(QueryParameters.METHOD, "list");
            arrayMap.put("access_token", this.f41002c);
            arrayMap.put("dir", str);
            try {
                Response c9 = OkHttpUtil.t().c("https://pan.baidu.com/rest/2.0/xpan/file", arrayMap);
                if (c9 != null) {
                    String string = c9.e().string();
                    CloudServiceUtils.a("BaiduCloudApi", "listAllItems.body = " + string);
                    BaiduListModel baiduListModel = (BaiduListModel) GsonUtils.b(string, BaiduListModel.class);
                    if (baiduListModel != null) {
                        if (baiduListModel.getErrno() == 0) {
                            return n(baiduListModel.getList());
                        }
                        if (baiduListModel.getErrno() == -9) {
                            RemoteFile remoteFile2 = new RemoteFile();
                            remoteFile2.f40980d = 0;
                            remoteFile2.f40978b = "CamScanner";
                            c(remoteFile2, null);
                            if (remoteFile == null) {
                                RemoteFile remoteFile3 = new RemoteFile();
                                remoteFile3.f40981e = "/apps/扫描全能王/CamScanner";
                                remoteFile3.f40980d = 0;
                                arrayList.add(remoteFile3);
                                return arrayList;
                            }
                        }
                    }
                }
            } catch (Exception e10) {
                LogUtils.e("BaiduCloudApi", e10);
            }
            return arrayList;
        }
        throw new WebstorageException(-8, "mAccessToken is Empty ");
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public void k() {
        BaiduAuthUtil.a(this.f40999a);
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public boolean l() {
        return false;
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public int m(RemoteFile remoteFile, RemoteFile remoteFile2, UploadProgressListener uploadProgressListener) {
        String str;
        if (ParamCheckUtil.b(remoteFile)) {
            return -7;
        }
        String name = remoteFile.f40979c.getName();
        if (TextUtils.isEmpty(this.f41002c)) {
            CloudServiceUtils.b("BaiduCloudApi", "upload mAccessToken = " + this.f41002c);
            return -8;
        }
        c(null, null);
        int i10 = -1;
        try {
            long length = remoteFile.f40979c.length();
            boolean z10 = length > 5242880;
            LogUtils.a("BaiduCloudApi", "upload needBlockUpload=" + z10 + ", fileLength=" + length);
            String a10 = MD5Utils.a(remoteFile.f40979c);
            if (z10) {
                str = p(remoteFile.f40979c);
            } else {
                str = "[\"" + a10 + "\"]";
            }
            String str2 = "https://pan.baidu.com/rest/2.0/xpan/file?method=precreate&access_token=" + this.f41002c;
            String str3 = "/apps/扫描全能王/CamScanner/" + name;
            ArrayMap arrayMap = new ArrayMap();
            arrayMap.put("isdir", AppEventsConstants.EVENT_PARAM_VALUE_NO);
            arrayMap.put("autoinit", "1");
            arrayMap.put("path", str3);
            arrayMap.put("block_list", str);
            arrayMap.put("content-md5", a10);
            String c9 = MD5Utils.c(FileUtil.m(0, remoteFile.f40979c, new byte[262144]));
            if (!TextUtils.isEmpty(c9)) {
                arrayMap.put("slice-md5", c9);
            }
            arrayMap.put(ScannerFormat.TAG_CANVAS_SIZE, String.valueOf(remoteFile.f40979c.length()));
            Response f10 = OkHttpUtil.t().f(str2, arrayMap);
            if (f10 != null) {
                i10 = f10.p();
                String string = f10.e().string();
                LogUtils.a("BaiduCloudApi", "precreate body = " + string);
                BaiduPreCreateModel baiduPreCreateModel = (BaiduPreCreateModel) GsonUtils.b(string, BaiduPreCreateModel.class);
                if (baiduPreCreateModel != null && baiduPreCreateModel.getErrno() == 0) {
                    if (baiduPreCreateModel.getReturn_type() == 2) {
                        return 0;
                    }
                    return r(remoteFile.f40979c, str3, baiduPreCreateModel.getUploadid(), baiduPreCreateModel.getBlock_list());
                }
            }
        } catch (Exception e10) {
            LogUtils.e("BaiduCloudApi", e10);
        }
        return i10;
    }
}