Skip to content

Commit 4c2f676

Browse files
committed
fix bug in CreateUploadDestinationForResourceAsync
1 parent b599672 commit 4c2f676

File tree

5 files changed

+64
-34
lines changed

5 files changed

+64
-34
lines changed

Source/FikaAmazonAPI.SampleCode/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ static async Task Main(string[] args)
3434
MarketPlace = MarketPlace.GetMarketPlaceByID(config.GetSection("FikaAmazonAPI:MarketPlaceID").Value),
3535
});
3636

37+
38+
39+
40+
41+
42+
3743
var GetCatalogItem202204 = await amazonConnection.CatalogItem.GetCatalogItem202204Async(new Parameter.CatalogItems.ParameterGetCatalogItem
3844
{
3945
ASIN = "B00JK2YANC",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using FikaAmazonAPI.Parameter.Upload;
2+
using FikaAmazonAPI.Utils;
3+
4+
namespace FikaAmazonAPI.SampleCode
5+
{
6+
public class UploadSample
7+
{
8+
AmazonConnection amazonConnection;
9+
public UploadSample(AmazonConnection amazonConnection)
10+
{
11+
this.amazonConnection = amazonConnection;
12+
}
13+
14+
public async Task CreateUploadDestinationForResourceAsync()
15+
{
16+
var imageHash = EasyMD5.GetMD5HashFromFile(@"C:\DataSave\tmp\amazon.png");
17+
var fileExtension = "png";
18+
var uploadRequest = new ParameterCreateUploadDestinationForResource
19+
{
20+
contentMD5 = imageHash,
21+
resource = "aplus/2020-11-01/contentDocuments",
22+
contentType = "image/" + fileExtension
23+
};
24+
25+
var uploadResponse = await amazonConnection.Upload.CreateUploadDestinationForResourceAsync(uploadRequest);
26+
}
27+
}
28+
}

Source/FikaAmazonAPI/Parameter/Upload/ParameterCreateUploadDestinationForResource.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using System.Collections.Generic;
1+
using FikaAmazonAPI.Search;
2+
using System.Collections.Generic;
23

34
namespace FikaAmazonAPI.Parameter.Upload
45
{
5-
public class ParametercreateUploadDestinationForResource
6+
public class ParameterCreateUploadDestinationForResource : ParameterBased
67
{
7-
public IList<string> marketplaceIds { get; set; }
8+
public IList<string> marketplaceIds { get; set; } = new List<string>();
89
public string contentMD5 { get; set; }
910
public string resource { get; set; }
1011
public string contentType { get; set; }

Source/FikaAmazonAPI/Services/UploadService.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ public UploadService(AmazonCredential amazonCredential) : base(amazonCredential)
1212

1313
}
1414

15-
public UploadDestination CreateUploadDestinationForResource(ParametercreateUploadDestinationForResource parameterObj) =>
15+
public UploadDestination CreateUploadDestinationForResource(ParameterCreateUploadDestinationForResource parameterObj) =>
1616
Task.Run(() => CreateUploadDestinationForResourceAsync(parameterObj)).ConfigureAwait(false).GetAwaiter().GetResult();
17-
public async Task<UploadDestination> CreateUploadDestinationForResourceAsync(ParametercreateUploadDestinationForResource parameterObj)
17+
public async Task<UploadDestination> CreateUploadDestinationForResourceAsync(ParameterCreateUploadDestinationForResource parameterObj)
1818
{
19+
if (parameterObj.marketplaceIds == null || parameterObj.marketplaceIds.Count == 0)
20+
parameterObj.marketplaceIds.Add(AmazonCredential.MarketPlace.ID);
1921

20-
await CreateAuthorizedRequestAsync(UploadApiUrls.CreateUploadDestinationForResource(parameterObj.resource), RestSharp.Method.POST, postJsonObj: parameterObj);
22+
var parameter = parameterObj.getParameters();
23+
24+
await CreateAuthorizedRequestAsync(UploadApiUrls.CreateUploadDestinationForResource(parameterObj.resource), RestSharp.Method.POST, parameter);
2125
var response = await ExecuteRequestAsync<CreateUploadDestinationResponse>(RateLimitType.Upload_CreateUploadDestinationForResource);
2226
return response.Payload;
2327
}

Source/FikaAmazonAPI/Utils/EasyMD5.cs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,37 @@
11
using System;
22
using System.IO;
3-
using System.Security.Cryptography;
43
using System.Text;
54

65
namespace FikaAmazonAPI.Utils
76
{
87
public class EasyMD5
98
{
10-
private static string GetMd5Hash(byte[] data)
9+
public static string GetMD5HashFromFile(string filepath)
1110
{
12-
StringBuilder sBuilder = new StringBuilder();
13-
for (int i = 0; i < data.Length; i++)
14-
sBuilder.Append(data[i].ToString("x2"));
15-
return sBuilder.ToString();
16-
}
11+
if (filepath == null)
12+
throw new ArgumentNullException("filepath");
13+
if (File.Exists(filepath) == false)
14+
throw new InvalidOperationException("file '" + filepath + "' doesn't exist");
1715

18-
private static bool VerifyMd5Hash(byte[] data, string hash)
19-
{
20-
return 0 == StringComparer.OrdinalIgnoreCase.Compare(GetMd5Hash(data), hash);
21-
}
16+
FileStream file = new FileStream(filepath, FileMode.Open);
17+
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
18+
byte[] bytes = md5.ComputeHash(file);
19+
file.Close();
2220

23-
public static string Hash(string data)
24-
{
25-
using (var md5 = MD5.Create())
26-
return GetMd5Hash(md5.ComputeHash(Encoding.UTF8.GetBytes(data)));
27-
}
28-
public static string Hash(FileStream data)
29-
{
30-
using (var md5 = MD5.Create())
31-
return GetMd5Hash(md5.ComputeHash(data));
21+
return GetMD5HashFromBytes(bytes);
3222
}
3323

34-
public static bool Verify(string data, string hash)
24+
public static string GetMD5HashFromBytes(byte[] bytes)
3525
{
36-
using (var md5 = MD5.Create())
37-
return VerifyMd5Hash(md5.ComputeHash(Encoding.UTF8.GetBytes(data)), hash);
38-
}
26+
if (bytes == null)
27+
throw new ArgumentNullException("bytes");
3928

40-
public static bool Verify(FileStream data, string hash)
41-
{
42-
using (var md5 = MD5.Create())
43-
return VerifyMd5Hash(md5.ComputeHash(data), hash);
29+
StringBuilder sb = new StringBuilder();
30+
for (int i = 0; i < bytes.Length; i++)
31+
{
32+
sb.Append(bytes[i].ToString("x2"));
33+
}
34+
return sb.ToString();
4435
}
4536
}
4637
}

0 commit comments

Comments
 (0)