imagesearch.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <map>
  2. #include <sstream>
  3. #include <iostream>
  4. #include "../utils.h"
  5. #include "alibabacloud/core/AlibabaCloud.h"
  6. #include "alibabacloud/core/CommonClient.h"
  7. using namespace std;
  8. using namespace AlibabaCloud;
  9. typedef unsigned char u8_t;
  10. const string catId = "88888888"; // ref: https://help.aliyun.com/document_detail/66623.html
  11. const string itemId = "1234";
  12. string custContent = "{\"key\":\"value\"}";
  13. map<string, string> picList;
  14. map<int, string> mapStudent;
  15. const string jpg1 = "1.jpg";
  16. const string jpg2 = "2.jpg";
  17. static const u8_t base64_table[65] =
  18. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  19. /**
  20. * base64_encode - Base64 encode
  21. * @src: Data to be encoded
  22. * @len: Length of the data to be encoded
  23. * @out_len: Pointer to output length variable, or %NULL if not used
  24. * Returns: Allocated buffer of out_len bytes of encoded data,
  25. * or empty string on failure
  26. */
  27. string base64_encode(const unsigned char *src, size_t len) {
  28. unsigned char *out, *pos;
  29. const unsigned char *end, *in;
  30. size_t olen;
  31. olen = 4*((len + 2) / 3); /* 3-byte blocks to 4-byte */
  32. if (olen < len)
  33. return std::string(); /* integer overflow */
  34. std::string outStr;
  35. outStr.resize(olen);
  36. out = (unsigned char*)&outStr[0];
  37. end = src + len;
  38. in = src;
  39. pos = out;
  40. while (end - in >= 3) {
  41. *pos++ = base64_table[in[0] >> 2];
  42. *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
  43. *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
  44. *pos++ = base64_table[in[2] & 0x3f];
  45. in += 3;
  46. }
  47. if (end - in) {
  48. *pos++ = base64_table[in[0] >> 2];
  49. if (end - in == 1) {
  50. *pos++ = base64_table[(in[0] & 0x03) << 4];
  51. *pos++ = '=';
  52. }
  53. else {
  54. *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
  55. *pos++ = base64_table[(in[1] & 0x0f) << 2];
  56. }
  57. *pos++ = '=';
  58. }
  59. return outStr;
  60. }
  61. string imageBase64(string file) {
  62. FILE* img = nullptr;
  63. #ifdef _WIN32
  64. fopen_s(&img, file.c_str(), "rb");
  65. #else
  66. img = fopen(file.c_str(), "rb");
  67. #endif
  68. if (!img) {
  69. perror("open file error");
  70. return "";
  71. }
  72. fseek(img, 0L, SEEK_END);
  73. int size = ftell(img);
  74. fseek(img, 0L, SEEK_SET);
  75. u8_t* buf = (u8_t*)malloc(size);
  76. int bytes = fread(buf, 1, size, img);
  77. fclose(img);
  78. string encoded = base64_encode(buf, bytes);
  79. free(buf);
  80. return encoded;
  81. }
  82. string buildContent(map<string, string> params) {
  83. string meta = "";
  84. string body = "";
  85. int start = 0;
  86. for(auto item = params.begin(); item != params.end(); item++) {
  87. auto k = item->first;
  88. auto v = item->second;
  89. if (meta.size() > 0) {
  90. meta += "#";
  91. }
  92. std::stringstream ss1, ss2;
  93. ss1 << start;
  94. ss2 << start + v.size();
  95. meta += k + "," + ss1.str() + "," + ss2.str();
  96. body += v;
  97. start += v.size();
  98. }
  99. return meta + "^" + body;
  100. }
  101. string buildAddContent(map<string, string> picList) {
  102. if (itemId.empty() || catId.empty() ) {
  103. return "";
  104. }
  105. if (custContent.empty()) {
  106. custContent = "";
  107. }
  108. map<string, string> params;
  109. params["item_id"] = itemId;
  110. params["cat_id"] = catId;
  111. params["cust_content"] = custContent;
  112. string picListStr = "";
  113. for (auto item = picList.begin(); item != picList.end(); item++) {
  114. auto v = item->second;
  115. auto k = item->first;
  116. if (k.empty()) {
  117. continue;
  118. }
  119. picListStr += k + ",";
  120. params[k] = v;
  121. }
  122. params["pic_list"] = picListStr.substr(0, picListStr.size() - 1);
  123. return buildContent(params);
  124. }
  125. int main(int argc, char** argv) {
  126. utUtils utils;
  127. string key = utils.get_env("ENV_AccessKeyId");
  128. string secret = utils.get_env("ENV_AccessKeySecret");
  129. InitializeSdk();
  130. ClientConfiguration configuration("cn-shanghai");
  131. CommonClient client(key, secret, configuration);
  132. CommonRequest request(CommonRequest::RoaPattern);
  133. request.setScheme("http");
  134. request.setDomain("imagesearch.cn-shanghai.aliyuncs.com");
  135. request.setResourcePath("/item/add");
  136. request.setHeaderParameter("instanceName", "sdkimagetest1");
  137. request.setHttpMethod(HttpRequest::Post);
  138. char dir[1024];
  139. utils.get_dir_exec(dir, nullptr);
  140. #ifdef _WIN32
  141. // windows binary has an extra Release/Debug/... dir
  142. string image1_path = string(dir) + "\\..\\1.jpg";
  143. string image2_path = string(dir) + "\\..\\2.jpg";
  144. #else
  145. string image1_path = string(dir) + "1.jpg";
  146. string image2_path = string(dir) + "2.jpg";
  147. #endif
  148. picList[base64_encode((u8_t*)jpg1.c_str(), jpg1.size())] = imageBase64(image1_path);
  149. picList[base64_encode((u8_t*)jpg2.c_str(), jpg2.size())] = imageBase64(image2_path);
  150. string cc = buildAddContent(picList);
  151. const char* data = cc.c_str();
  152. request.setContent(cc.c_str(), cc.size());
  153. request.setHeaderParameter("Content-Type", "application/octet-stream;charset=utf-8");
  154. request.setHeaderParameter("Accept", "application/json");
  155. request.setVersion("2018-01-20");
  156. auto out = client.commonResponse(request);
  157. if (!out.isSuccess()) {
  158. cout << "error code: " << out.error().errorCode() << endl;
  159. cout << "error message: " << out.error().errorMessage() << endl;
  160. cout << "error host: " << out.error().host() << endl;
  161. cout << "error requestId: " << out.error().requestId() << endl;
  162. cout << "error detail: " << out.error().detail() << endl;
  163. ShutdownSdk();
  164. return -1;
  165. }
  166. cout << endl << "add item returns: " << out.result().payload() << endl << endl;
  167. ShutdownSdk();
  168. return 0;
  169. }