forked from forhappy/OSSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oss_object_metadata.c
358 lines (314 loc) · 9.52 KB
/
oss_object_metadata.c
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <stdlib.h>
#include <stdlib.h>
#include <memory.h>
#include <stdio.h>
#include "include/oss_object_metadata.h"
#include "include/oss_constants.h"
static inline void
_object_metadata_add_user_metadata(oss_object_metadata_t *metadata,
const char *key,
const char *value)
{
oss_map_t *oss_map = metadata->user_metadata;
oss_map_put(oss_map, key, value);
}
static inline void
_object_metadata_add_default_metadata(oss_object_metadata_t *metadata,
const char *key,
const char *value)
{
oss_map_t *oss_map = metadata->metadata;
oss_map_put(oss_map, key, value);
}
static inline const char *
_object_metadata_get_cache_control(oss_object_metadata_t *metadata)
{
char *buf;
int result;
oss_map_t *oss_map = metadata->metadata;
result = oss_map_get(oss_map, OSS_CACHE_CONTROL, NULL, 0);
if (result == 0)
return NULL;
buf = (char *)malloc(sizeof(char) * result);
memset(buf, '\0', result);
result = oss_map_get(oss_map, OSS_CACHE_CONTROL, buf, result);
if (result == 0)
return NULL;
return buf;
}
static inline const char *
_object_metadata_get_content_disposition(oss_object_metadata_t *metadata)
{
char *buf;
int result;
oss_map_t *oss_map = metadata->metadata;
result = oss_map_get(oss_map, OSS_CONTENT_DISPOSITION, NULL, 0);
if (result == 0)
return NULL;
buf = (char *)malloc(sizeof(char) * result);
memset(buf, '\0', result);
result = oss_map_get(oss_map, OSS_CONTENT_DISPOSITION, buf, result);
if (result == 0)
return NULL;
return buf;
}
static inline const char *
_object_metadata_get_content_encoding(oss_object_metadata_t *metadata)
{
char *buf;
int result;
oss_map_t *oss_map = metadata->metadata;
result = oss_map_get(oss_map, OSS_CONTENT_ENCOING, NULL, 0);
if (result == 0)
return NULL;
buf = (char *)malloc(sizeof(char) * result);
memset(buf, '\0', result);
result = oss_map_get(oss_map, OSS_CONTENT_ENCOING, buf, result);
if (result == 0)
return NULL;
return buf;
}
static inline long
_object_metadata_get_content_length(oss_object_metadata_t *metadata)
{
char *buf;
int result;
int len;
oss_map_t *oss_map = metadata->metadata;
result = oss_map_get(oss_map, OSS_CONTENT_LENGTH, NULL, 0);
if (result == 0)
return 0;
buf = (char *)malloc(sizeof(char) * result);
memset(buf, '\0', result);
result = oss_map_get(oss_map, OSS_CONTENT_LENGTH, buf, result);
if (result == 0)
return 0;
len = atoi(buf);
free(buf);
return len;
}
/**
* 获取Content-Type请求头,表示Object内容的类型,为标准的MIME类型
*/
static inline const char *
_object_metadata_get_content_type(oss_object_metadata_t *metadata)
{
char *buf;
int result;
oss_map_t *oss_map = metadata->metadata;
result = oss_map_get(oss_map, OSS_CONTENT_TYPE, NULL, 0);
if (result == 0)
return NULL;
buf = (char *)malloc(sizeof(char) * result);
memset(buf, '\0', result);
result = oss_map_get(oss_map, OSS_CONTENT_TYPE, buf, result);
if (result == 0)
return NULL;
return buf;
}
/**
* 获取一个值表示与Object相关的hex编码的128位MD5摘要
*/
static inline const char *
_object_metadata_get_etag(oss_object_metadata_t *metadata)
{
char *buf;
int result;
oss_map_t *oss_map = metadata->metadata;
result = oss_map_get(oss_map, OSS_CONTENT_MD5, NULL, 0);
if (result == 0)
return NULL;
buf = (char *)malloc(sizeof(char) * result);
memset(buf, '\0', result);
result = oss_map_get(oss_map, OSS_CONTENT_MD5, buf, result);
if (result == 0)
return NULL;
return buf;
}
/**
* 获取Expires请求头
*/
static inline const char *
_object_metadata_get_expiration_time(oss_object_metadata_t *metadata)
{
char *buf;
int result;
oss_map_t *oss_map = metadata->metadata;
result = oss_map_get(oss_map, OSS_EXPIRES, NULL, 0);
if (result == 0)
return NULL;
buf = (char *)malloc(sizeof(char) * result);
memset(buf, '\0', result);
result = oss_map_get(oss_map, OSS_EXPIRES, buf, result);
if (result == 0)
return NULL;
return buf;
}
/**
* 获取Last-Modified请求头的值,表示Object最后一次修改的时间
*/
static inline const char *
_object_metadata_get_last_modified(oss_object_metadata_t *metadata)
{
char *buf;
int result;
oss_map_t *oss_map = metadata->metadata;
result = oss_map_get(oss_map, OSS_LAST_MODIFIED, NULL, 0);
if (result == 0)
return NULL;
buf = (char *)malloc(sizeof(char) * result);
memset(buf, '\0', result);
result = oss_map_get(oss_map, OSS_LAST_MODIFIED, buf, result);
if (result == 0)
return NULL;
return buf;
}
/**
* 返回内部保存的请求头的元数据(内部使用)
*/
static inline oss_map_t *
_object_metadata_get_raw_metadata(oss_object_metadata_t *metadata)
{
oss_map_t *oss_map = metadata->metadata;
return oss_map;
}
/**
* 获取用户自定义的元数据
*/
static inline oss_map_t *
_object_metadata_get_user_metadata(oss_object_metadata_t *metadata)
{
oss_map_t *oss_map = metadata->user_metadata;
return oss_map;
}
/**
* 设置Cache-Control请求头,表示用户指定的HTTP请求/回复链的缓存行为
*/
static inline void
_object_metadata_set_cache_control(oss_object_metadata_t *metadata,
const char *cache_control)
{
oss_map_t *oss_map = metadata->metadata;
oss_map_put(oss_map, OSS_CACHE_CONTROL, cache_control);
}
/**
* 设置Content-Disposition请求头,表示MIME用户代理如何显示附加的文件
*/
static inline void
_object_metadata_set_content_disposition(oss_object_metadata_t *metadata,
const char *disposition)
{
oss_map_t *oss_map = metadata->metadata;
oss_map_put(oss_map, OSS_CONTENT_DISPOSITION, disposition);
}
static inline void
_object_metadata_set_etag(oss_object_metadata_t *metadata,
const char *etag)
{
oss_map_t *oss_map = metadata->metadata;
oss_map_put(oss_map, OSS_CONTENT_MD5, etag);
}
/**
* 设置Content-Encoding请求头,表示Object内容的编码方式
*/
static inline void
_object_metadata_set_content_encoding(oss_object_metadata_t *metadata,
const char *encoding)
{
oss_map_t *oss_map = metadata->metadata;
oss_map_put(oss_map, OSS_CONTENT_ENCOING, encoding);
}
/**
* 设置Content-Length请求头,表示Object内容的大小
*/
static inline void
_object_metadata_set_content_length(oss_object_metadata_t *metadata,
long content_length)
{
char content_len[32] = {0};
sprintf(content_len, "%ld", content_length);
oss_map_t *oss_map = metadata->metadata;
oss_map_put(oss_map, OSS_CONTENT_LENGTH, content_len);
}
/**
* 获取Content-Type请求头,表示Object内容的类型,为标准的MIME类型
*/
static inline void
_object_metadata_set_content_type(oss_object_metadata_t *metadata,
const char *content_type)
{
oss_map_t *oss_map = metadata->metadata;
oss_map_put(oss_map, OSS_CONTENT_TYPE, content_type);
}
/**
* 设置Expires请求头
*/
static inline void
_object_metadata_set_expiration_time(oss_object_metadata_t *metadata,
const char *expiration_time)
{
oss_map_t *oss_map = metadata->metadata;
oss_map_put(oss_map, OSS_EXPIRES, expiration_time);
}
/**
* 设置请求头(内部使用)
*/
static inline void
_object_metadata_set_header(oss_object_metadata_t *metadata,
const char *key,
const char *value)
{
oss_map_t *oss_map = metadata->metadata;
oss_map_put(oss_map, key, value);
}
/**
* 设置Last-Modified请求头的值,表示Object最后一次修改的时间(内部使用)
*/
static inline void
_object_metadata_set_last_modified(oss_object_metadata_t *metadata,
const char *last_modified)
{
oss_map_t *oss_map = metadata->metadata;
oss_map_put(oss_map, OSS_LAST_MODIFIED, last_modified);
}
/**
* 设置用户自定义的元数据,表示以x-oss-meta-为前缀的请求头
*/
static inline void
_object_metadata_set_user_metadata(oss_object_metadata_t *metadata,
oss_map_t *user_metadata)
{
oss_map_t *oss_map = metadata->user_metadata;
oss_map_delete(oss_map);
metadata->user_metadata = user_metadata;
}
oss_object_metadata_t *object_metadata_initialize()
{
oss_object_metadata_t *metadata;
metadata = (oss_object_metadata_t *)malloc(sizeof(oss_object_metadata_t));
metadata->metadata = oss_map_new(128);
metadata->user_metadata = oss_map_new(128);
metadata->add_user_metadata = _object_metadata_add_user_metadata;
metadata->add_default_metadata = _object_metadata_add_default_metadata;
metadata->get_cache_control = _object_metadata_get_cache_control;
metadata->get_content_disposition = _object_metadata_get_content_disposition;
metadata->get_content_encoding = _object_metadata_get_content_encoding;
metadata->get_content_length = _object_metadata_get_content_length;
metadata->get_content_type = _object_metadata_get_content_type;
metadata->get_etag = _object_metadata_get_etag;
metadata->get_expiration_time = _object_metadata_get_expiration_time;
metadata->get_last_modified = _object_metadata_get_last_modified;
metadata->get_raw_metadata = _object_metadata_get_raw_metadata;
metadata->get_user_metadata = _object_metadata_get_user_metadata;
metadata->set_cache_control = _object_metadata_set_cache_control;
metadata->set_content_disposition = _object_metadata_set_content_disposition;
metadata->set_content_encoding = _object_metadata_set_content_encoding;
metadata->set_content_length = _object_metadata_set_content_length;
metadata->set_content_type = _object_metadata_set_content_type;
metadata->set_expiration_time = _object_metadata_set_expiration_time;
metadata->set_etag = _object_metadata_set_etag;
metadata->set_header = _object_metadata_set_header;
metadata->set_last_modified = _object_metadata_set_last_modified;
metadata->set_user_metadata = _object_metadata_set_user_metadata;
return metadata;
}