|
1 |
| -using System.Net.Http; |
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Globalization; |
| 4 | +using System.Linq; |
| 5 | +using System.Net.Http; |
2 | 6 | using System.Threading.Tasks;
|
3 | 7 | using Cuiliang.AliyunOssSdk.Api.Base;
|
| 8 | +using Cuiliang.AliyunOssSdk.Api.Common.Consts; |
4 | 9 | using Cuiliang.AliyunOssSdk.Entites;
|
5 | 10 | using Cuiliang.AliyunOssSdk.Request;
|
| 11 | +using Cuiliang.AliyunOssSdk.Utility; |
6 | 12 |
|
7 | 13 | namespace Cuiliang.AliyunOssSdk.Api.Object.Get
|
8 | 14 | {
|
@@ -43,12 +49,47 @@ public override async Task<OssResult<GetObjectResult>> ParseResultAsync(HttpResp
|
43 | 49 | var result = new GetObjectResult();
|
44 | 50 | result.Headers = response.Headers;
|
45 | 51 | result.Content = response.Content;
|
| 52 | + result.Metadata = Deserialize(response); |
46 | 53 |
|
47 | 54 | return new OssResult<GetObjectResult>()
|
48 | 55 | {
|
49 | 56 | IsSuccess = true,
|
50 | 57 | SuccessResult = result
|
51 | 58 | };
|
52 | 59 | }
|
| 60 | + |
| 61 | + private ObjectMetadata Deserialize(HttpResponseMessage response) |
| 62 | + { |
| 63 | + |
| 64 | + var metadata = new ObjectMetadata(); |
| 65 | + foreach (var header in response.Headers) |
| 66 | + { |
| 67 | + if (header.Key.StartsWith(OssHeaders.OssUserMetaPrefix, false, CultureInfo.InvariantCulture)) |
| 68 | + { |
| 69 | + // The key of user in the metadata should not contain the prefix. |
| 70 | + metadata.UserMetadata.Add(header.Key.Substring(OssHeaders.OssUserMetaPrefix.Length), |
| 71 | + header.Value.FirstOrDefault()); |
| 72 | + } |
| 73 | + else if (string.Equals(header.Key, HttpHeaders.ContentLength, StringComparison.InvariantCultureIgnoreCase)) |
| 74 | + { |
| 75 | + // Content-Length. Parse should not fail. |
| 76 | + metadata.ContentLength = long.Parse(header.Value.FirstOrDefault(), CultureInfo.InvariantCulture); |
| 77 | + } |
| 78 | + else if (string.Equals(header.Key, HttpHeaders.ETag, StringComparison.InvariantCultureIgnoreCase)) |
| 79 | + { |
| 80 | + metadata.ETag = OssUtils.TrimETag(header.Value.FirstOrDefault()); |
| 81 | + } |
| 82 | + else if (string.Equals(header.Key, HttpHeaders.LastModified, StringComparison.InvariantCultureIgnoreCase)) |
| 83 | + { |
| 84 | + metadata.LastModified = DateUtils.ParseRfc822Date(header.Value.FirstOrDefault()); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + // Treat the other headers just as strings. |
| 89 | + metadata.AddHeader(header.Key, header.Value); |
| 90 | + } |
| 91 | + } |
| 92 | + return metadata; |
| 93 | + } |
53 | 94 | }
|
54 | 95 | }
|
0 commit comments