forked from leancloud/cpp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploaderManager.cpp
More file actions
147 lines (119 loc) · 4.85 KB
/
UploaderManager.cpp
File metadata and controls
147 lines (119 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* @file UploaderManager.cpp
* @author yang chaozhong <cyang@avoscloud.com>
* @date Mon Aug 18 14:49:00 2014
*
* @brief
*
* Copyright 2014 AVOS Cloud Inc. All rights reserved.
*/
#include "AVFile/UploaderManager.h"
#include <cstdlib>
#include <string>
#include <exception>
#include <sstream>
#include <boost/log/trivial.hpp>
#include "json/json.h"
#include "Request/AVPaasClient.h"
NS_AV_BEGIN
UploaderManager* UploaderManager::_instance = nullptr;
std::mutex UploaderManager::_lock;
UploaderManager* UploaderManager::sharedInstance() {
std::lock_guard<std::mutex> locker(_lock);
if (_instance == nullptr) {
_instance = new UploaderManager();
}
return _instance;
}
std::string UploaderManager::generateRandomString(int length) {
std::string letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
std::string randomString;
for (int i = 0; i < length; ++i) {
randomString.append(StringUtils::string_format("%C", letters[rand() % letters.length()]));
}
return randomString;
}
std::string UploaderManager::generateQiniuKey() {
return UploaderManager::generateRandomString(16);
}
void UploaderManager::uploadWithAVFileAndCallback(AVFile* file,
AVBooleanResultCallback resultCallback) {
std::string key = UploaderManager::generateQiniuKey();
std::string extension = StringUtils::getExtentionFromFileName(file->name);
if (extension.length() > 0) {
key.append(StringUtils::string_format(".%s", extension));
}
Json::Value dict;
dict["key"] = key;
dict["name"] = file->name;
dict["mime_type"] = "application/octet-stream";
dict["__type"] = "File";
dict["metaData"] = file->updateMetadata();
AVPaasClient::sharedInstance()->
postObject("qiniu", dict, [&](Json::Value const & root, AVError const & error) {
if (error.domain.length() == 0) {
std::string token = root["token"].asString();
std::string bucket = root["bucket"].asString();
std::string objectId = root["objectId"].asString();
file->url = root["url"].asString();
file->bucket = bucket;
file->objectId = objectId;
this->uploadFileToBucketWithTokenAndCallback(bucket, token, file, key, [&](bool const& succeeded, AVError const& error) {
if (error.domain.length() == 0) {
if (file->name.length() <= 0) {
file->name = objectId;
}
BOOST_LOG_TRIVIAL(info) << "remote file link:" << file->url;
}
resultCallback(succeeded, error);
});
} else {
resultCallback(false, error);
}
});
}
void UploaderManager::uploadFileToBucketWithTokenAndCallback(std::string bucket,
std::string token,
AVFile* file,
std::string key,
AVBooleanResultCallback resultCallback) {
std::string bodyStr = this->generateQiniuMultipartBodyString(bucket, token, file, key);
AVPaasClient::sharedInstance()->uploadFileToQiniuWithBodyAndCallback(bodyStr, resultCallback);
}
void UploaderManager::release() {
AV_SAFE_DELETE(this);
}
////////////////////////////// private methods //////////////////////////////
std::string UploaderManager::generateQiniuMultipartBodyString(std::string bucket,
std::string token,
AVFile* file,
std::string key) {
try {
std::string k_crlf = "\r\n";
std::string startBoundaryStr = "--28e84231563f43b08b1cc55659e9b3ac";
std::string endBoundaryStr = "--28e84231563f43b08b1cc55659e9b3ac--";
std::ostringstream os;
os << startBoundaryStr << k_crlf;
os << "Content-Disposition: form-data; name=\"token\"" << k_crlf << k_crlf;
os << token << k_crlf;
os << startBoundaryStr << k_crlf;
os << "Content-Disposition: form-data; name=\"key\"" << k_crlf << k_crlf;
os << key << k_crlf;
std::ifstream ifs(file->path);
ifs >> std::noskipws;
std::string fileStr((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
os << startBoundaryStr << k_crlf;
os << StringUtils::string_format("Content-Disposition: %s",
StringUtils::string_format("form-data; name=\"file\"; filename=\"%s\"",
file->name))
<< k_crlf << k_crlf;
os << fileStr << k_crlf;
os << endBoundaryStr;
return os.str();
} catch (std::exception & e) {
BOOST_LOG_TRIVIAL(error) << e.what();
std::string str;
return str;
}
}
NS_AV_END