Skip to content
Merged

pfop #164

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/main/java/com/qiniu/processing/OperationManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.qiniu.processing;

import com.qiniu.common.Config;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.qiniu.util.StringUtils;

/**
* 触发持久化处理
* 针对七牛空间文件,触发异步文件处理。如异步视频转码等
*/
public class OperationManager {
private final Client client;
private final Auth auth;

public OperationManager(Auth auth) {
this.auth = auth;
this.client = new Client();
}

/**
* 触发 空间 文件 的 pfop 操作
*
* @param bucket 空间名
* @param key 文件名
* @param fops fop指令
* @return persistentId
* @throws QiniuException 触发失败异常,包含错误响应等信息
* @link http://developer.qiniu.com/docs/v6/api/reference/fop/pfop/pfop.html
*/
public String pfop(String bucket, String key, String fops) throws QiniuException {
return pfop(bucket, key, fops, null);
}

/**
* 触发 空间 文件 的 pfop 操作
*
* @param bucket 空间名
* @param key 文件名
* @param fops fop指令
* @param params notifyURL、force、pipeline 等参数
* @return persistentId
* @throws QiniuException 触发失败异常,包含错误响应等信息
* @link http://developer.qiniu.com/docs/v6/api/reference/fop/pfop/pfop.html
*/
public String pfop(String bucket, String key, String fops, StringMap params) throws QiniuException {
params = params == null ? new StringMap() : params;
params.put("bucket", bucket).put("key", key).put("fops", fops);
byte[] data = StringUtils.utf8Bytes(params.formString());
String url = Config.API_HOST + "/pfop/";
StringMap headers = auth.authorization(url, data, Client.FormMime);
Response response = client.post(url, data, headers, Client.FormMime);
PfopStatus status = response.jsonToObject(PfopStatus.class);
return status.persistentId;
}

private class PfopStatus {
public String persistentId;
}
}
102 changes: 102 additions & 0 deletions src/test/java/com/qiniu/processing/PfopTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.qiniu.processing;

import com.qiniu.TestConfig;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

import com.qiniu.util.UrlSafeBase64;
import org.junit.Test;

import java.util.UUID;

import static org.junit.Assert.*;

public class PfopTest {
private Auth auth = TestConfig.testAuth;
private OperationManager operater = new OperationManager(auth);

@Test
public void testAvthumb() {
String bucket = "testres";
String key = "sintel_trailer.mp4";

String notifyURL = "";
boolean force = true;
String pipeline = "";
StringMap params = new StringMap().putNotEmpty("notifyURL", notifyURL)
.putWhen("force", 1, force).putNotEmpty("pipeline", pipeline);
// CHECKSTYLE:OFF
String fops = "avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240|saveas/amF2YXNkazptcDRfODVmZWVjY2ItMGZmOS00NTg5LTk3MTMtZDM1ZmYzZGQ4ZjM2";
// CHECKSTYLE:ON
try {
String id = operater.pfop(bucket, key, fops, params);
assertNotNull(id);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertNotNull (id) 这是什么意思

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是单元测试。。。

assertNotEquals("", id);
String purl = "http://api.qiniu.com/status/get/prefop?id=" + id;
System.out.println(purl);
} catch (QiniuException e) {
Response res = e.response;
System.out.println(res);
try {
System.out.println(res.bodyString());
} catch (QiniuException e1) {
e1.printStackTrace();
}
fail();
}
}

@Test
public void testMuAvthumb() {
String bucket = "testres";
String key = "sintel_trailer.mp4";
// CHECKSTYLE:OFF
String fops = "avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240|saveas/amF2YXNkazptcDRfM19mMDNjZjQ4Mi1kMzgxLTQ5NWUtOThiNC04NjZkYTVkMDVlMTY=;avthumb/m3u8/segtime/10/vcodec/libx264/s/480x480|saveas/amF2YXNkazptcDRfNF8wYmQ2NjQxOS04MjZmLTRiZDItYjQ5MS00ZDRmM2Y1ZmQ1Mjk=;avthumb/m3u8/segtime/10/vcodec/libx264/s/720x720|saveas/amF2YXNkazptcDRfN19hMTM1ZmRlNS1hYTNlLTQ3MWItYjdhMi0yNmQ5MWEyZTc5MzM=;avthumb/mp4/vcodec/libx264";
// CHECKSTYLE:ON
StringMap params = new StringMap().putWhen("force", 1, true);
try {
String id = operater.pfop(bucket, key, fops, params);
assertNotNull(id);
assertNotEquals("", id);
String purl = "http://api.qiniu.com/status/get/prefop?id=" + id;
System.out.println(purl);
} catch (QiniuException e) {
Response res = e.response;
System.out.println(res);
try {
System.out.println(res.bodyString());
} catch (QiniuException e1) {
e1.printStackTrace();
}
fail();
}
}

@Test
public void testMkzip() {
String bucket = "testres";
String key = "sintel_trailer.mp4";
// CHECKSTYLE:OFF
String fops = "mkzip/2/url/aHR0cDovL3Rlc3RyZXMucWluaXVkbi5jb20vZ29nb3BoZXIuanBn/alias/Z29nb3BoZXIuanBn/url/aHR0cDovL3Rlc3RyZXMucWluaXVkbi5jb20vZ29nb3BoZXIuanBn";
fops += "|saveas/" + UrlSafeBase64.encodeToString("javasdk" + ":" + key + "_" + UUID.randomUUID());
// CHECKSTYLE:ON
try {
String id = operater.pfop(bucket, key, fops);
assertNotNull(id);
assertNotEquals("", id);
String purl = "http://api.qiniu.com/status/get/prefop?id=" + id;
System.out.println(purl);
} catch (QiniuException e) {
Response res = e.response;
System.out.println(res);
try {
System.out.println(res.bodyString());
} catch (QiniuException e1) {
e1.printStackTrace();
}
fail();
}
}
}