Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.qiniu.process.qiniu.ChangeMime Maven / Gradle / Ivy
Go to download
qiniu-suits is a efficient tools for qiniu api implemented by java8.
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 ChangeMime extends Base> {
private String mimeType;
private String encodedMime;
private String mimeIndex;
private String encodedCondition;
private ArrayList ops;
private List> lines;
private Auth auth;
private Configuration configuration;
private Client client;
private static String requestUrl;
public ChangeMime(String accessKey, String secretKey, Configuration configuration, String bucket, String mimeType,
String mimeIndex, String condition) throws IOException {
super("mime", accessKey, secretKey, bucket);
this.mimeType = mimeType;
if (mimeType != null) encodedMime = UrlSafeBase64.encodeToString(mimeType);
this.mimeIndex = mimeIndex == null ? "mime" : mimeIndex;
if (condition != null && !condition.isEmpty()) encodedCondition = UrlSafeBase64.encodeToString(condition);
this.configuration = configuration;
CloudApiUtils.checkQiniu(accessKey, secretKey, configuration, bucket);
this.auth = Auth.create(accessKey, secretKey);
this.configuration = configuration;
this.client = new Client(configuration.clone());
requestUrl = configuration.useHttpsDomains ? "https://rs.qiniu.com/chgm/" : "http://rs.qiniu.com/chgm/";
}
public ChangeMime(String accessKey, String secretKey, Configuration configuration, String bucket, String mimeType,
String mimeIndex, String condition, String savePath, int saveIndex) throws IOException {
super("mime", accessKey, secretKey, bucket, savePath, saveIndex);
this.mimeType = mimeType;
if (mimeType != null) encodedMime = UrlSafeBase64.encodeToString(mimeType);
this.mimeIndex = mimeIndex == null ? "mime" : mimeIndex;
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());
requestUrl = configuration.useHttpsDomains ? CloudApiUtils.QINIU_RS_BATCH_URL.replace("http://", "https://")
: CloudApiUtils.QINIU_RS_BATCH_URL;
}
public ChangeMime(String accessKey, String secretKey, Configuration configuration, String bucket, String mimeType,
String mimeIndex, String condition, String savePath) throws IOException {
this(accessKey, secretKey, configuration, bucket, mimeType, mimeIndex, condition, savePath, 0);
}
@Override
public ChangeMime clone() throws CloneNotSupportedException {
ChangeMime changeType = (ChangeMime)super.clone();
changeType.auth = Auth.create(accessId, secretKey);
changeType.client = new Client(configuration.clone());
if (fileSaveMapper != null) {
changeType.ops = new ArrayList<>(batchSize);
changeType.lines = new ArrayList<>(batchSize);
}
return changeType;
}
@Override
public void batchSizeTrigger() {
if (batchSize <= 1) {
requestUrl = configuration.useHttpsDomains ? "https://rs.qiniu.com/chgm/" : "http://rs.qiniu.com/chgm/";
}
}
@Override
protected String resultInfo(Map line) {
if (mimeType == null) {
return String.join("\t", line.get("key"), line.get(mimeIndex));
} else {
return line.get("key");
}
}
@Override
protected List> putBatchOperations(List> processList) throws IOException {
ops.clear();
lines.clear();
String key;
StringBuilder pathBuilder;
if (mimeType == null) {
String mime;
for (Map map : processList) {
key = map.get("key");
mime = map.get(mimeIndex);
if (key != null && mime != null) {
lines.add(map);
pathBuilder = new StringBuilder("/chgm/")
.append(UrlSafeBase64.encodeToString(String.join(":", bucket, key)))
.append("/mime/").append(UrlSafeBase64.encodeToString(mime));
if (encodedCondition != null) pathBuilder.append("/cond/").append(encodedCondition);
ops.add(pathBuilder.toString());
} else {
fileSaveMapper.writeError("key or mime is not exists or empty in " + map, false);
}
}
} else {
for (Map map : processList) {
key = map.get("key");
if (key != null) {
lines.add(map);
pathBuilder = new StringBuilder("/chgm/")
.append(UrlSafeBase64.encodeToString(String.join(":", bucket, key)))
.append("/mime/").append(encodedMime);
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(requestUrl, body, auth.authorization(requestUrl, 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);
StringBuilder urlBuilder;
if (mimeType == null) {
String mime = line.get(mimeIndex);
if (mime == null) throw new IOException("mime is not exists or empty in " + line);
urlBuilder = new StringBuilder(requestUrl)
.append(UrlSafeBase64.encodeToString(String.join(":", bucket, key)))
.append("/mime/").append(UrlSafeBase64.encodeToString(mime));
if (encodedCondition != null) urlBuilder.append("/cond/").append(encodedCondition);
String url = urlBuilder.toString();
Response response = client.post(url, null, auth.authorization(url, null, Client.FormMime), Client.FormMime);
if (response.statusCode != 200) throw new QiniuException(response);
response.close();
return String.join("\t", key, mime, "200");
} else {
urlBuilder = new StringBuilder(requestUrl)
.append(UrlSafeBase64.encodeToString(String.join(":", bucket, key)))
.append("/mime/").append(encodedMime);
if (encodedCondition != null) urlBuilder.append("/cond/").append(encodedCondition);
String url = urlBuilder.toString();
Response response = client.post(url, null, auth.authorization(url, null, Client.FormMime), Client.FormMime);
if (response.statusCode != 200) throw new QiniuException(response);
response.close();
return String.join("\t", key, "200");
}
}
@Override
public void closeResource() {
super.closeResource();
mimeType = null;
mimeIndex = null;
encodedCondition = null;
if (ops != null) ops.clear();
ops = null;
if (lines != null) lines.clear();
auth = null;
client = null;
configuration = null;
}
}