huanggen 1 год назад
Родитель
Сommit
4ee39cd81f
2 измененных файлов с 119 добавлено и 121 удалено
  1. 41 32
      UCloudSDK/api.cpp
  2. 78 89
      UCloudSDK/api.h

+ 41 - 32
UCloudSDK/api.cpp

@@ -15,21 +15,22 @@
 
 using namespace qcloud_cos;
 CosConfig g_config;
+std::string g_strBucketName = "";
 
-void result2ufileError(ufile_error & retError, qcloud_cos::CosResult tRet)
+void result2ufileError(ufile_error & retError, qcloud_cos::CosResult * tRet)
 {
-	retError.errorCode = tRet.GetErrorCode();
-	retError.errorMessage = tRet.GetErrorMsg();
-	retError.resourceAddr = tRet.GetResourceAddr();
-	retError.cosRequestId = tRet.GetXCosRequestId();
-	retError.cosTraceId = tRet.GetXCosTraceId();
-	retError.httpStatus = tRet.GetHttpStatus();
-	retError.bIsSuccess = tRet.IsSucc();
+	strncpy_s(retError.errorCode, tRet->GetErrorCode().c_str(), sizeof(retError.errorCode));
+	strncpy_s(retError.errorMessage, tRet->GetErrorMsg().c_str(), sizeof(retError.errorMessage));
+	strncpy_s(retError.resourceAddr, tRet->GetResourceAddr().c_str(), sizeof(retError.resourceAddr));
+	strncpy_s(retError.cosRequestId, tRet->GetXCosRequestId().c_str(), sizeof(retError.cosRequestId));
+	strncpy_s(retError.cosTraceId, tRet->GetXCosTraceId().c_str(), sizeof(retError.cosTraceId));
+	retError.httpStatus = tRet->GetHttpStatus();
+	retError.bIsSuccess = tRet->IsSucc();
 }
 
 ufile_error ufile_load_config_from_json(const char * json_buf, ufile_config * cfg)
 {
-	ufile_error retError;
+	ufile_error retError = { false, 0, "", "", "", "", "" };
 	if (nullptr == cfg)
 		return retError;
 	Poco::JSON::Parser parser;
@@ -38,13 +39,11 @@ ufile_error ufile_load_config_from_json(const char * json_buf, ufile_config * cf
 		result = parser.parse(json_buf);
 	}
 	catch (Poco::JSON::JSONException& jsonErr) {
-		retError.errorMessage = jsonErr.message();
-		retError.bIsSuccess = false;
+		strncpy_s(retError.errorMessage, jsonErr.message().c_str(), sizeof(retError.errorMessage));
 		return retError;
 	}
 	if (result.type() != typeid(Poco::JSON::Object::Ptr)) {
-		retError.errorMessage = "failed to parse config file";
-		retError.bIsSuccess = false;
+		strncpy_s(retError.errorMessage, "failed to parse config file", sizeof(retError.errorMessage));
 		return retError;
 	}
 	Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>();
@@ -74,9 +73,10 @@ void ufile_free_config(ufile_config cfg)
 ufile_error ufile_sdk_initialize(const ufile_config cfg, int open_verbose)
 {
 	UNUSED_PARAM(open_verbose);
-	ufile_error retError;
+	ufile_error retError = { false, 0, "", "", "", "", "" };
 	CosConfig config(cfg.appid, cfg.public_key, cfg.private_key, cfg.region);
 	g_config = config;
+	g_strBucketName = cfg.bucket_host;
 	retError.bIsSuccess = true;
 	return retError;
 }
@@ -98,27 +98,28 @@ void ufile_free_file_info(ufile_file_info info)
 
 ufile_error ufile_put_buf(const char * bucket_name, const char * key, const char * mime_type, char * buffer, size_t buf_len)
 {
-	ufile_error retError;
-
+	//此函数无调用,暂且不实现
 	return ufile_error();
 }
 
 ufile_error ufile_put_file(const char * bucket_name, const char * key, const char * mime_type, FILE * file)
 {
-	ufile_error retError;
-
+	//此函数无调用,暂且不实现
 	return ufile_error();
 }
 
 ufile_error ufile_put_file_by_filepath(const char * bucket_name, const char * key, const char * mime_type, const char * file_path)
 {
 	UNUSED_PARAM(mime_type);
-	ufile_error retError;
-	qcloud_cos::PutObjectByFileReq req(bucket_name, key, file_path);
+	ufile_error retError = { false, 0, "", "", "", "", "" };
+	std::string strBucketName = bucket_name;
+	if (strBucketName.empty())
+		strBucketName = g_strBucketName;
+	qcloud_cos::PutObjectByFileReq req(strBucketName, key, file_path);
 	qcloud_cos::PutObjectByFileResp resp;
 	CosAPI cosApi(g_config);
 	CosResult result = cosApi.PutObject(req, &resp);
-	result2ufileError(retError, result);
+	result2ufileError(retError, &result);
 	return retError;
 }
 
@@ -128,12 +129,15 @@ void set_upload_company(int company)
 
 ufile_error ufile_delete(const char * bucket_name, const char * key)
 {
-	ufile_error retError;
-	qcloud_cos::DeleteObjectReq req(bucket_name, key);
+	ufile_error retError = { false, 0, "", "", "", "", "" };
+	std::string strBucketName = bucket_name;
+	if (strBucketName.empty())
+		strBucketName = g_strBucketName;
+	qcloud_cos::DeleteObjectReq req(strBucketName, key);
 	qcloud_cos::DeleteObjectResp resp;
 	CosAPI cosApi(g_config);
 	CosResult result = cosApi.DeleteObject(req, &resp);
-	result2ufileError(retError, result);
+	result2ufileError(retError, &result);
 	return ufile_error();
 }
 
@@ -163,14 +167,17 @@ ufile_error ufile_multiple_upload_abort(ufile_mutipart_state * self)
 
 ufile_error ufile_download(const char * bucket_name, const char * key, FILE * file, size_t * return_size, const char * path)
 {
-	ufile_error retError;
+	ufile_error retError = { false, 0, "", "", "", "", "" };
 	if (nullptr == file)
 	{
-		retError.errorMessage = "file is nullptr";
+		strncpy_s(retError.errorMessage, "file is nullptr", sizeof(retError.errorMessage));
 		return retError;
 	}
 	std::ostringstream os;
-	qcloud_cos::GetObjectByStreamReq req(bucket_name, key, os);
+	std::string strBucketName = bucket_name;
+	if (strBucketName.empty())
+		strBucketName = g_strBucketName;
+	qcloud_cos::GetObjectByStreamReq req(strBucketName, key, os);
 	qcloud_cos::GetObjectByStreamResp resp;
 	CosAPI cosApi(g_config);
 	CosResult result = cosApi.GetObject(req, &resp);
@@ -179,7 +186,7 @@ ufile_error ufile_download(const char * bucket_name, const char * key, FILE * fi
 		const char * tBuf = os.str().c_str();
 		fwrite(tBuf, sizeof(tBuf), strlen(tBuf), file);
 	}
-	result2ufileError(retError, result);
+	result2ufileError(retError, &result);
 	return retError;
 }
 
@@ -197,12 +204,14 @@ ufile_error ufile_bucket_create(const char * bucket_name, const char * region, c
 
 ufile_error ufile_bucket_delete(const char * bucket_name)
 {
-	ufile_error retError;
-	qcloud_cos::DeleteBucketReq req(bucket_name);
+	ufile_error retError = { false, 0, "", "", "", "", "" };
+	std::string strBucketName = bucket_name;
+	if (strBucketName.empty())
+		strBucketName = g_strBucketName;
+	qcloud_cos::DeleteBucketReq req(strBucketName);
 	qcloud_cos::DeleteBucketResp resp;
 	CosAPI cosApi(g_config);
 	CosResult result = cosApi.DeleteBucket(req, &resp);
-	result2ufileError(retError, result);
+	result2ufileError(retError, &result);
 	return ufile_error();
 }
-

+ 78 - 89
UCloudSDK/api.h

@@ -2,31 +2,20 @@
 #define _H_UFILESDK_C_UCLOUD_API_
 
 #include <stdio.h>
-#include <string>
 
 #ifdef __cplusplus
 extern "C" {
 #endif
-//*******************************common data********************************
-struct __declspec(dllexport) ufile_error {
-	std::string errorCode;
-	std::string errorMessage;
-	std::string resourceAddr;
-	std::string cosRequestId;
-	std::string cosTraceId;
-	bool bIsSuccess;
-	int httpStatus;
-	ufile_error()
-	{
-		errorCode = "";
-		errorMessage = "";
-		resourceAddr = "";
-		cosRequestId = "";
-		cosTraceId = "";
-		bIsSuccess = false;
-		httpStatus = 0;
-	}
-};
+	//*******************************common data********************************
+	struct __declspec(dllexport) ufile_error {
+		bool bIsSuccess;
+		int httpStatus;
+		char errorCode[32];
+		char resourceAddr[64];
+		char cosRequestId[64];
+		char errorMessage[128];
+		char cosTraceId[256];
+	};
 
 #define UFILE_OK  0
 #define UFILE_ERROR_CODE -1
@@ -41,114 +30,114 @@ struct __declspec(dllexport) ufile_error {
 #define CURL_INIT_ERROR_MSG "init curl failed."
 
 #define UFILE_HAS_ERROR(CODE) ((CODE) != 0 && (CODE)/100 != 2 )
-//****************************************************************** end
+	//****************************************************************** end
 
-//*********************************config
-struct __declspec(dllexport) ufile_config{
-    const char *public_key;   //json: public_key
-    const char *private_key;  //json: private_key
+	//*********************************config
+	struct __declspec(dllexport) ufile_config {
+		const char *public_key;   //json: public_key
+		const char *private_key;  //json: private_key
 
-    const char *file_host;    //json: file_host
-    const char *bucket_host;  //json: bucket_host
-	long long   appid;
-	const char *region;
-};
+		const char *file_host;    //json: file_host
+		const char *bucket_host;  //json: bucket_host
+		long long   appid;
+		const char *region;
+	};
 
-__declspec(dllexport) struct ufile_error
-ufile_load_config_from_json(const char* json_buf, struct ufile_config *cfg);
+	__declspec(dllexport) struct ufile_error
+		ufile_load_config_from_json(const char* json_buf, struct ufile_config *cfg);
 
 
-__declspec(dllexport) void
-ufile_free_config(struct ufile_config cfg);
-//*********************************end config
+	__declspec(dllexport) void
+		ufile_free_config(struct ufile_config cfg);
+	//*********************************end config
 
 
-__declspec(dllexport) struct ufile_error
-ufile_sdk_initialize(const struct ufile_config cfg, int open_verbose);
+	__declspec(dllexport) struct ufile_error
+		ufile_sdk_initialize(const struct ufile_config cfg, int open_verbose);
 
 
-__declspec(dllexport) void
-ufile_sdk_cleanup();
+	__declspec(dllexport) void
+		ufile_sdk_cleanup();
 
-//********************************************** file info
+	//********************************************** file info
 
-struct __declspec(dllexport) ufile_file_info{
-    long long bytes_len;
-    const char *etag;
-    const char *mime_type;
-};
+	struct __declspec(dllexport) ufile_file_info {
+		long long bytes_len;
+		const char *etag;
+		const char *mime_type;
+	};
 
 
-__declspec(dllexport) struct ufile_error
-ufile_head(const char* bucket_name, const char *key, struct ufile_file_info *info);
+	__declspec(dllexport) struct ufile_error
+		ufile_head(const char* bucket_name, const char *key, struct ufile_file_info *info);
 
 
-__declspec(dllexport) void
-ufile_free_file_info(struct ufile_file_info info);
-//***************************************************
+	__declspec(dllexport) void
+		ufile_free_file_info(struct ufile_file_info info);
+	//***************************************************
 
-__declspec(dllexport) struct ufile_error
-ufile_put_buf(const char* bucket_name, const char *key, const char *mime_type, char *buffer, size_t buf_len);
+	__declspec(dllexport) struct ufile_error
+		ufile_put_buf(const char* bucket_name, const char *key, const char *mime_type, char *buffer, size_t buf_len);
 
-__declspec(dllexport) struct ufile_error
-ufile_put_file(const char* bucket_name, const char *key, const char *mime_type, FILE *file);
+	__declspec(dllexport) struct ufile_error
+		ufile_put_file(const char* bucket_name, const char *key, const char *mime_type, FILE *file);
 
-__declspec(dllexport) struct ufile_error
-ufile_put_file_by_filepath(const char* bucket_name, const char *key, const char *mime_type, const char*file_path);
+	__declspec(dllexport) struct ufile_error
+		ufile_put_file_by_filepath(const char* bucket_name, const char *key, const char *mime_type, const char*file_path);
 
-enum{
-	company_ucloud, // ucloud 
-	company_tx, //ÌÚѶ
-};
-__declspec(dllexport) void
-set_upload_company(int company);
+	enum {
+		company_ucloud, // ucloud 
+		company_tx, //ÌÚѶ
+	};
+	__declspec(dllexport) void
+		set_upload_company(int company);
 
-__declspec(dllexport) struct ufile_error
-ufile_delete(const char* bucket_name, const char *key);
+	__declspec(dllexport) struct ufile_error
+		ufile_delete(const char* bucket_name, const char *key);
 
 
-__declspec(dllexport) struct ufile_mutipart_state{
+	__declspec(dllexport) struct ufile_mutipart_state {
 
-    const char *bucket_name;
+		const char *bucket_name;
 
-    const char *key;
+		const char *key;
 
-    size_t part_size;
-    const char *upload_id;
-    struct etag_slist *etags;
-    struct curls_list *curls;
-};
+		size_t part_size;
+		const char *upload_id;
+		struct etag_slist *etags;
+		struct curls_list *curls;
+	};
 
-__declspec(dllexport) struct ufile_error
-ufile_multiple_upload_init(struct ufile_mutipart_state *self, const char *bucket_name, const char *key, const char* mime_type);
+	__declspec(dllexport) struct ufile_error
+		ufile_multiple_upload_init(struct ufile_mutipart_state *self, const char *bucket_name, const char *key, const char* mime_type);
 
 
-__declspec(dllexport) struct ufile_error
-ufile_multiple_upload_part(struct ufile_mutipart_state *self, char *buffer, size_t buf_len, int part_number);
+	__declspec(dllexport) struct ufile_error
+		ufile_multiple_upload_part(struct ufile_mutipart_state *self, char *buffer, size_t buf_len, int part_number);
 
 
-__declspec(dllexport) struct ufile_error
-ufile_multiple_upload_finish(struct ufile_mutipart_state *self);
+	__declspec(dllexport) struct ufile_error
+		ufile_multiple_upload_finish(struct ufile_mutipart_state *self);
 
 
-__declspec(dllexport) struct ufile_error
-ufile_multiple_upload_abort(struct ufile_mutipart_state *self);
+	__declspec(dllexport) struct ufile_error
+		ufile_multiple_upload_abort(struct ufile_mutipart_state *self);
 
 
-__declspec(dllexport) struct ufile_error
-ufile_download(const char *bucket_name, const char *key, FILE *file, size_t *return_size,const char*path=0);
+	__declspec(dllexport) struct ufile_error
+		ufile_download(const char *bucket_name, const char *key, FILE *file, size_t *return_size, const char*path = 0);
 
 
-__declspec(dllexport) struct ufile_error
-ufile_download_piece(const char *bucket_name, const char *key, size_t start_position, char *buf, size_t buf_len, size_t *return_size);
+	__declspec(dllexport) struct ufile_error
+		ufile_download_piece(const char *bucket_name, const char *key, size_t start_position, char *buf, size_t buf_len, size_t *return_size);
 
 
-__declspec(dllexport) struct ufile_error
-ufile_bucket_create(const char *bucket_name, const char* region, const char* bucket_type);
+	__declspec(dllexport) struct ufile_error
+		ufile_bucket_create(const char *bucket_name, const char* region, const char* bucket_type);
 
 
-__declspec(dllexport) struct ufile_error
-ufile_bucket_delete(const char *bucket_name);
+	__declspec(dllexport) struct ufile_error
+		ufile_bucket_delete(const char *bucket_name);
 #ifdef __cplusplus
 }
 #endif