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

com.qiniu.process.filtration.SeniorChecker Maven / Gradle / Ivy

There is a newer version: 8.4.8
Show newest version
package com.qiniu.process.filtration;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.qiniu.config.JsonFile;
import com.qiniu.util.JsonConvertUtils;

import java.io.IOException;
import java.util.*;

public class SeniorChecker {

    final private String checkName;
    private Set extMimeList;
    private Set extMimeTypeList;
    private static List checkList = new ArrayList(){{
        add("mime");
    }};

    public SeniorChecker(String checkName, String configPath, boolean rewrite) throws IOException {
        this.checkName = checkName;
        this.extMimeList = new HashSet<>();
        this.extMimeTypeList = new HashSet<>();
        if (configPath != null && !"".equals(configPath)) {
            JsonFile customJson = new JsonFile(configPath);
            JsonElement jsonElement = customJson.getElement("ext-mime");
            this.extMimeTypeList = new HashSet<>(JsonConvertUtils.fromJsonArray(jsonElement.getAsJsonArray(),
                    new TypeToken>(){}));
        }
        if (checkMime() && !rewrite) {
            JsonFile jsonFile = new JsonFile("resources" + System.getProperty("file.separator") + "check.json");
            JsonObject extMime = jsonFile.getElement("ext-mime").getAsJsonObject();
            List defaultList = JsonConvertUtils.fromJsonArray(extMime.get("image").getAsJsonArray(),
                    new TypeToken>(){});
            defaultList.addAll(JsonConvertUtils.fromJsonArray(extMime.get("audio").getAsJsonArray(),
                    new TypeToken>(){}));
            defaultList.addAll(JsonConvertUtils.fromJsonArray(extMime.get("video").getAsJsonArray(),
                    new TypeToken>(){}));
            this.extMimeList.addAll(defaultList);
            this.extMimeTypeList.addAll(JsonConvertUtils.fromJsonArray(extMime.get("other").getAsJsonArray(),
                    new TypeToken>(){}));
        }
    }

    public String getCheckName() {
        return checkName;
    }

    public boolean checkMime() {
        return "mime".equals(checkName);
    }

    public boolean isValid() {
        return checkList.contains(checkName);
    }

    public List> checkMimeType(List> lineList) {
        String key;
        List> filteredList = new ArrayList<>();
        for (Map line : lineList) {
            key = line.get("key");
            if (key != null && key.contains(".")) {
                String finalKeyMimePair = key.substring(key.lastIndexOf(".") + 1) + ":" + line.get("mimeType");
                if (extMimeList.parallelStream().anyMatch(extMime ->
                        finalKeyMimePair.split("/")[0].equalsIgnoreCase(extMime))) {
                    break;
                }
                if (extMimeTypeList.parallelStream().noneMatch(extMime -> finalKeyMimePair.startsWith(extMime) ||
                        finalKeyMimePair.equalsIgnoreCase(extMime))) {
                    filteredList.add(line);
                }
            }
        }
        return filteredList;
    }

    public boolean checkMimeType(Map line) {
        if (line.get("key") != null && line.get("key").contains(".")) {
            String finalKeyMimePair = line.get("key").substring(line.get("key").lastIndexOf(".") + 1) +
                    ":" + line.get("mimeType");
            if (extMimeList.parallelStream().anyMatch(extMime ->
                    finalKeyMimePair.split("/")[0].equalsIgnoreCase(extMime))) {
                return false;
            }
            return extMimeTypeList.parallelStream().noneMatch(extMime -> finalKeyMimePair.startsWith(extMime) ||
                    finalKeyMimePair.equalsIgnoreCase(extMime));
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy