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

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

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

import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.process.Base;
import com.qiniu.storage.Configuration;
import com.qiniu.util.*;

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

public class ChangeMetadata extends Base> {

    private Map metadata;
    private String encodedCondition;
    private ArrayList ops;
    private List> lines;
    private Auth auth;
    private Configuration configuration;
    private Client client;

    public ChangeMetadata(String accessKey, String secretKey, Configuration configuration, String bucket,
                          Map metadata, String condition) throws IOException {
        super("metadata", accessKey, secretKey, bucket);
        if (metadata == null) throw new IOException("metadata can not be null");
        this.metadata = metadata;
        if (condition != null && !condition.isEmpty()) encodedCondition = UrlSafeBase64.encodeToString(condition);
        CloudApiUtils.checkQiniu(accessKey, secretKey, configuration, bucket);
        this.auth = Auth.create(accessKey, secretKey);
        this.configuration = configuration;
        this.client = new Client(configuration.clone());
    }

    public ChangeMetadata(String accessKey, String secretKey, Configuration configuration, String bucket,
                          Map metadata, String condition, String savePath, int saveIndex) throws IOException {
        super("metadata", accessKey, secretKey, bucket, savePath, saveIndex);
        if (metadata == null) throw new IOException("metadata can not be null");
        this.metadata = metadata;
        if (condition != null && !condition.isEmpty()) encodedCondition = UrlSafeBase64.encodeToString(condition);
        this.batchSize = 1000;
        this.ops = new ArrayList<>(1000);
        this.lines = new ArrayList<>(1000);
        CloudApiUtils.checkQiniu(accessKey, secretKey, configuration, bucket);
        this.auth = Auth.create(accessKey, secretKey);
        this.configuration = configuration;
        this.client = new Client(configuration.clone());
    }

    public ChangeMetadata(String accessKey, String secretKey, Configuration configuration, String bucket,
                          Map metadata, String condition, String savePath) throws IOException {
        this(accessKey, secretKey, configuration, bucket, metadata, condition, savePath, 0);
    }

    @Override
    public ChangeMetadata clone() throws CloneNotSupportedException {
        ChangeMetadata changeMetadata = (ChangeMetadata)super.clone();
        if (fileSaveMapper == null) {
            changeMetadata.ops = new ArrayList<>(batchSize);
            changeMetadata.lines = new ArrayList<>(batchSize);
        }
        changeMetadata.auth = Auth.create(accessId, secretKey);
        changeMetadata.client = new Client(configuration.clone());
        return changeMetadata;
    }

    @Override
    protected String resultInfo(Map line) {
        return line.get("key");
    }

    @Override
    protected List> putBatchOperations(List> processList) throws IOException {
        ops.clear();
        lines.clear();
        String key;
//        String encodedMetaValue;
//        String path;
        StringBuilder pathBuilder;
        for (Map map : processList) {
            key = map.get("key");
            if (key != null) {
                lines.add(map);
//                path = String.format("/chgm/%s", BucketManager.encodedEntry(bucket, key));
                pathBuilder = new StringBuilder("/chgm/").append(UrlSafeBase64.encodeToString(String.join(":", bucket, key)));
                for (String k : metadata.keySet()) {
//                    encodedMetaValue = UrlSafeBase64.encodeToString(metadata.get(k));
//                    path = String.format("%s/x-qn-meta-!%s/%s", path, k, encodedMetaValue);
                    pathBuilder.append("/x-qn-meta-!").append(k).append("/").append(UrlSafeBase64.encodeToString(metadata.get(k)));
                }
//                if (condition != null) path = String.format("%s/cond/%s", path, condition);
//                ops.add(path);
                if (encodedCondition != null) pathBuilder.append("/cond/").append(encodedCondition);
                ops.add(pathBuilder.toString());
            } else {
                fileSaveMapper.writeError("key is not exists or empty in " + map, false);
            }
        }
        return lines;
    }

    @Override
    protected String batchResult(List> lineList) throws IOException {
        byte[] body = StringUtils.utf8Bytes(StringUtils.join(ops, "&op=", "op="));
        return HttpRespUtils.getResult(client.post(CloudApiUtils.QINIU_RS_BATCH_URL, body,
                auth.authorization(CloudApiUtils.QINIU_RS_BATCH_URL, body, Client.FormMime), Client.FormMime));
    }

    @Override
    protected String singleResult(Map line) throws IOException {
        String key = line.get("key");
        if (key == null) throw new IOException("key is not exists or empty in " + line);
//        String path = String.format("/chgm/%s", BucketManager.encodedEntry(bucket, key));
        StringBuilder urlBuilder = new StringBuilder("http://rs.qiniu.com/chgm/")
                .append(UrlSafeBase64.encodeToString(String.join(":", bucket, key)));
        for (String k : metadata.keySet()) {
//            path = String.format("%s/x-qn-meta-!%s/%s", path, k, UrlSafeBase64.encodeToString(metadata.get(k)));
            urlBuilder.append("/x-qn-meta-!").append(k).append("/").append(UrlSafeBase64.encodeToString(metadata.get(k)));
        }
//        if (condition != null) path = String.format("%s/cond/%s", path, condition);
//        String url = "http://rs.qiniu.com" + path;
        if (encodedCondition != null) urlBuilder.append("/cond/").append(encodedCondition);
        StringMap headers = auth.authorization(urlBuilder.toString(), null, Client.FormMime);
        Response response = client.post(urlBuilder.toString(), null, headers, Client.FormMime);
        if (response.statusCode != 200) throw new QiniuException(response);
        response.close();
        return String.join("\t", key, "200");
    }

    @Override
    public void closeResource() {
        super.closeResource();
        if (metadata != null) metadata.clear();
        metadata = null;
        encodedCondition = null;
        if (ops != null) ops.clear();
        ops = null;
        if (lines != null) lines.clear();
        lines = null;
        auth = null;
        configuration = null;
        client = null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy