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

com.qiniu.process.qiniu.VideoCensor Maven / Gradle / Ivy

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

import com.google.gson.JsonObject;
import com.qiniu.process.Base;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;
import com.qiniu.util.CloudApiUtils;
import com.qiniu.util.JsonUtils;
import com.qiniu.util.RequestUtils;

import java.io.IOException;
import java.util.Map;

public class VideoCensor extends Base> {

    private String protocol;
    private String domain;
    private String urlIndex;
    private JsonObject paramsJson;
    private Configuration configuration;
    private CensorManager censorManager;

    public VideoCensor(String accessKey, String secretKey, Configuration configuration, String protocol, String domain,
                       String urlIndex, String[] scenes, int interval, String saverBucket, String saverPrefix, String hookUrl)
            throws IOException {
        super("videocensor", accessKey, secretKey, null);
        set(configuration, protocol, domain, urlIndex, scenes, interval, saverBucket, saverPrefix, hookUrl);
        Auth auth = Auth.create(accessKey, secretKey);
        CloudApiUtils.checkQiniu(auth);
        censorManager = new CensorManager(auth, configuration);
    }

    public VideoCensor(String accessKey, String secretKey, Configuration configuration, String protocol, String domain,
                       String urlIndex, String[] scenes, int interval, String saverBucket, String saverPrefix, String hookUrl,
                       String savePath, int saveIndex) throws IOException {
        super("videocensor", accessKey, secretKey, null, savePath, saveIndex);
        set(configuration, protocol, domain, urlIndex, scenes, interval, saverBucket, saverPrefix, hookUrl);
        Auth auth = Auth.create(accessKey, secretKey);
        CloudApiUtils.checkQiniu(auth);
        censorManager = new CensorManager(auth, configuration);
    }

    public VideoCensor(String accesskey, String secretKey, Configuration configuration, String protocol, String domain,
                       String urlIndex, String[] scenes, int interval, String saverBucket, String saverPrefix, String hookUrl,
                       String savePath) throws IOException {
        this(accesskey, secretKey, configuration, protocol, domain, urlIndex, scenes, interval, saverBucket, saverPrefix,
                hookUrl, savePath, 0);
    }

    private void set(Configuration configuration, String protocol, String domain, String urlIndex, String[] scenes,
                     int interval, String saverBucket, String saverPrefix, String hookUrl) throws IOException {
        this.configuration = configuration;
        if (domain == null || "".equals(domain)) {
            if (urlIndex == null || "".equals(urlIndex)) {
                throw new IOException("please set one of domain and url-index.");
            } else {
                this.urlIndex = urlIndex;
            }
        } else {
            this.protocol = protocol == null || !protocol.matches("(http|https)") ? "http" : protocol;
            RequestUtils.lookUpFirstIpFromHost(domain);
            this.domain = domain;
            this.urlIndex = "url";
        }
        this.paramsJson = new JsonObject();
        paramsJson.add("scenes", CensorManager.getScenes(scenes));
        if ((saverBucket != null && !"".equals(saverBucket)) || (saverPrefix != null && !"".equals(saverPrefix))) {
            JsonObject saverJson = new JsonObject();
            saverJson.addProperty("bucket", saverBucket);
            saverJson.addProperty("prefix", saverPrefix);
            paramsJson.add("saver", saverJson);
        }
        if (interval > 0) {
            if (interval > 60000 || interval < 1000) throw new IOException("invalid cut_param.interval_msecs.");
            JsonObject pictureCutJson = new JsonObject();
            pictureCutJson.addProperty("interval_msecs", interval);
            paramsJson.add("cut_param", pictureCutJson);
        }
        if (hookUrl != null && !"".equals(hookUrl)) {
            paramsJson.addProperty("hook_url", hookUrl);
        }
    }

    @Override
    public VideoCensor clone() throws CloneNotSupportedException {
        VideoCensor videoCensor = (VideoCensor)super.clone();
        videoCensor.censorManager = new CensorManager(Auth.create(accessId, secretKey), configuration);
        return videoCensor;
    }

    @Override
    protected String resultInfo(Map line) {
        return domain == null ? line.get(urlIndex) : line.get("key");
    }

    @Override
    protected String singleResult(Map line) throws Exception {
        String url;
        String key = line.get("key");
        if (domain == null) {
            url = line.get(urlIndex);
            return String.join("\t", url,
                    JsonUtils.toJsonObject(censorManager.doVideoCensor(url, paramsJson)).get("job").getAsString());
        } else {
            if (key == null) throw new IOException("key is not exists or empty in " + line);
            url = String.join("", protocol, "://", domain, "/", key.replace("\\?", "%3f"));
            return String.join("\t", key,
                    JsonUtils.toJsonObject(censorManager.doVideoCensor(url, paramsJson)).get("job").getAsString());
        }
    }

    @Override
    public void closeResource() {
        super.closeResource();
        protocol = null;
        domain = null;
        urlIndex = null;
        paramsJson = null;
        configuration = null;
        censorManager = null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy