Skip to content

Commit 51659db

Browse files
Merge pull request #93 from jackyding2679/win_widechar
Win widechar
2 parents 9b7d590 + 90ddc5b commit 51659db

File tree

16 files changed

+445
-272
lines changed

16 files changed

+445
-272
lines changed

demo/cos_demo.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,36 @@ void MultiUploadObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
655655
<< std::endl;
656656
}
657657

658+
#ifdef _WIN32
659+
// 上传宽字符文件
660+
void MultiUploadObjectWideChar(qcloud_cos::CosAPI& cos,
661+
const std::string& bucket_name) {
662+
std::string local_file = "./안녕하십니까.mp4";
663+
std::string object_name = "안녕하십니까.mp4";
664+
qcloud_cos::MultiUploadObjectReq req(bucket_name, object_name, local_file);
665+
req.SetWideCharPath(); // 需要调用该函数
666+
req.SetRecvTimeoutInms(1000 * 60);
667+
qcloud_cos::MultiUploadObjectResp resp;
668+
qcloud_cos::CosResult result = cos.MultiUploadObject(req, &resp);
669+
670+
if (result.IsSucc()) {
671+
std::cout << "MultiUploadObjectWideChar Succ." << std::endl;
672+
std::cout << resp.GetLocation() << std::endl;
673+
std::cout << resp.GetKey() << std::endl;
674+
std::cout << resp.GetBucket() << std::endl;
675+
std::cout << resp.GetEtag() << std::endl;
676+
} else {
677+
std::cout << "MultiUploadObjectWideChar Fail." << std::endl;
678+
}
679+
std::cout << "===================MultiUploadObjectWideChar==================="
680+
"=========="
681+
<< std::endl;
682+
PrintResult(result, resp);
683+
std::cout << "========================================================"
684+
<< std::endl;
685+
}
686+
#endif
687+
658688
//限速多线程上传
659689
void MultiUploadObjectLimitTraffic(qcloud_cos::CosAPI& cos,
660690
const std::string& bucket_name,
@@ -2445,8 +2475,7 @@ int main(int argc, char** argv) {
24452475
// PutObjectByFile(cos, bucket_name, "/中文文件",
24462476
// "/data/sevenyou/temp/seven_0821_10M");
24472477
//// 简单上传(文件), 大文件
2448-
// PutObjectByFile(cos, bucket_name, "sevenyou_0803_2g",
2449-
// "/data/sevenyou/cos-cpp-sdk-26/testdata/seven_2g.tmp");
2478+
// PutObjectByFile(cos, bucket_name, "test", "./config.json");
24502479

24512480
// PutObjectByStream(cos, bucket_name);
24522481

@@ -2766,5 +2795,10 @@ int main(int argc, char** argv) {
27662795
// GetSnapshot(cos, bucket_name, "1920_1080.mp4", "snapshot.jpg");
27672796
// GetMediaInfo(cos, bucket_name, "1920_1080.mp4");
27682797
//}
2798+
2799+
#if defined(_WIN32)
2800+
// MultiUploadObjectWideChar(cos, bucket_name);
2801+
system("pause");
2802+
#endif
27692803
return 0;
27702804
}

include/cos_defines.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef COS_DEFINE_H
22
#define COS_DEFINE_H
3+
#include <inttypes.h>
34
#include <stdint.h>
45
#include <stdio.h>
56

include/op/object_op.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ class ObjectOp : public BaseOp {
4444
const std::string& bucket_name,
4545
const std::string& object_name,
4646
const std::string& uploadid,
47-
const std::string& localpath,
4847
std::vector<std::string>& already_exist);
4948

50-
bool CheckSinglePart(const std::string& local_file_path, uint64_t offset,
49+
bool CheckSinglePart(const MultiUploadObjectReq& req, uint64_t offset,
5150
uint64_t local_part_size, uint64_t size,
5251
const std::string& etag);
5352

include/request/object_req.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include <map>
1010
#include <sstream>
1111
#include <vector>
12-
12+
#if defined(_WIN32)
13+
#include <codecvt>
14+
#endif
1315
#include "cos_sys_config.h"
1416
#include "request/base_req.h"
1517
#include "trsf/transfer_handler.h"
@@ -708,7 +710,12 @@ class MultiUploadObjectReq : public ObjectReq {
708710
MultiUploadObjectReq(const std::string& bucket_name,
709711
const std::string& object_name,
710712
const std::string& local_file_path = "")
711-
: ObjectReq(bucket_name, object_name) {
713+
: ObjectReq(bucket_name, object_name)
714+
#if defined(_WIN32)
715+
,
716+
mb_is_widechar_path(false)
717+
#endif
718+
{
712719
// 默认使用配置文件配置的分块大小和线程池大小
713720
m_part_size = CosSysConfig::GetUploadPartSize();
714721
m_thread_pool_size = CosSysConfig::GetUploadThreadPoolSize();
@@ -829,15 +836,28 @@ class MultiUploadObjectReq : public ObjectReq {
829836
}
830837

831838
std::string GetUploadID() const { return m_uploadid; }
839+
#if defined(_WIN32)
840+
/// \brief 如果本地文件路径为宽字符(中文,韩文等),需要调用该函数
841+
void SetWideCharPath() { mb_is_widechar_path = true; }
842+
843+
bool IsWideCharPath() const { return mb_is_widechar_path; }
832844

845+
std::wstring GetWideCharLocalFilePath() const {
846+
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
847+
return converter.from_bytes(m_local_file_path);
848+
}
849+
#endif
833850
private:
834851
std::string m_local_file_path;
835852
uint64_t m_part_size;
836853
int m_thread_pool_size;
837854
std::map<std::string, std::string> m_xcos_meta;
838855
bool mb_set_meta;
839856
std::string m_uploadid;
840-
};
857+
#if defined(_WIN32)
858+
bool mb_is_widechar_path; // 标识文件路径是否为宽字符
859+
#endif
860+
}; // namespace qcloud_cos
841861

842862
class AbortMultiUploadReq : public ObjectReq {
843863
public:

include/util/file_util.h

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,29 @@
88

99
namespace qcloud_cos {
1010

11-
class FileUtil {
12-
public:
13-
//获取文件内容
14-
static std::string GetFileContent(const std::string& path);
15-
16-
//返回文件大小
17-
static uint64_t GetFileLen(const std::string& path);
18-
19-
static bool IsDirectoryExists(const std::string& path);
20-
21-
static bool IsDirectory(const std::string& path);
11+
class FileUtil {
12+
public:
13+
// 获取文件内容
14+
static std::string GetFileContent(const std::string& path);
15+
16+
// 获取文件大小
17+
static uint64_t GetFileLen(const std::string& path);
18+
#if defined(_WIN32)
19+
static uint64_t GetFileLen(const std::wstring& path);
20+
#endif
21+
static bool IsDirectoryExists(const std::string& path);
2222

23-
static std::string GetDirectory(const std::string& path);
23+
static bool IsDirectory(const std::string& path);
2424

25-
static uint64_t GetFileCrc64(const std::string& file);
25+
static std::string GetDirectory(const std::string& path);
2626

27-
static std::string GetFileMd5(const std::string& file);
28-
};
29-
} // namespace qcloud_cos
27+
// 获取文件CRC64
28+
static uint64_t GetFileCrc64(const std::string& file);
29+
#if defined(_WIN32)
30+
static uint64_t GetFileCrc64(const std::wstring& file);
31+
#endif
32+
static std::string GetFileMd5(const std::string& file);
33+
};
34+
} // namespace qcloud_cos
3035

3136
#endif

src/op/cos_result.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,18 @@ bool CosResult::ParseFromHttpResponse(
3333
m_x_cos_trace_id = c_itr->second;
3434
}
3535

36+
std::string tmp_body = body;
3637
rapidxml::xml_document<> doc;
37-
char* cstr = new char[body.size() + 1];
38-
strcpy(cstr, body.c_str());
39-
cstr[body.size()] = '\0';
4038

41-
if (!StringUtil::StringToXml(cstr, &doc)) {
42-
SDK_LOG_INFO("Parse string to xml doc error, xml_body=%s", body.c_str());
43-
SetErrorMsg(body);
44-
delete[] cstr;
39+
if (!StringUtil::StringToXml(&tmp_body[0], &doc)) {
40+
SDK_LOG_ERR("Parse string to xml doc error, xml_body=%s", body.c_str());
4541
return false;
4642
}
4743

4844
rapidxml::xml_node<>* root = doc.first_node(kErrorRoot.c_str());
4945
if (NULL == root) {
5046
// SDK_LOG_INFO("Miss root node=Error, xml_body=%s", body.c_str());
5147
SetErrorMsg(body);
52-
delete[] cstr;
5348
return false;
5449
}
5550

@@ -73,7 +68,6 @@ bool CosResult::ParseFromHttpResponse(
7368
node_name.c_str(), body.c_str());
7469
}
7570
}
76-
delete[] cstr;
7771
return true;
7872
}
7973

0 commit comments

Comments
 (0)