Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions RestAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WooCommerceNET.Base;

Expand Down Expand Up @@ -164,9 +165,16 @@ public virtual async Task<string> SendHttpClientRequest<T>(string endpoint, Requ
if (JWTRequestFilter != null)
JWTRequestFilter.Invoke(request);

var buffer = Encoding.UTF8.GetBytes($"username={wc_key}&password={wc_secret}");
var convKey = ConvertAmpersandsToUTF8Hex(wc_key);
var convSecret = ConvertAmpersandsToUTF8Hex(wc_secret);

var buffer = Encoding.UTF8.GetBytes($"username={convKey}&password={convSecret}");
Stream dataStream = await request.GetRequestStreamAsync().ConfigureAwait(false);
dataStream.Write(buffer, 0, buffer.Length);
dataStream.Close();

request.ContentLength = buffer.Length;

WebResponse response = await request.GetResponseAsync().ConfigureAwait(false);
Stream resStream = response.GetResponseStream();
string result = await GetStreamContent(resStream, "UTF-8").ConfigureAwait(false);
Expand Down Expand Up @@ -233,7 +241,12 @@ public virtual async Task<string> SendHttpClientRequest<T>(string endpoint, Requ
httpWebRequest.ContentType = "application/x-www-form-urlencoded";

Stream dataStream = await httpWebRequest.GetRequestStreamAsync().ConfigureAwait(false);
FileStream fileStream = new FileStream(parms["path"], FileMode.Open, FileAccess.Read);

// If the given path is a physical path, open file. If not, read from URL.
var fileStream = parms["source"] == "local" ?
new FileStream(parms["path"], FileMode.Open, FileAccess.Read) :
(new WebClient()).OpenRead(parms["path"]);

byte[] buffer = new byte[4096];
int bytesRead = 0;

Expand Down Expand Up @@ -451,6 +464,12 @@ public string DateTimeFormat
return IsLegacy ? "yyyy-MM-ddTHH:mm:ssZ" : "yyyy-MM-ddTHH:mm:ss";
}
}

private string ConvertAmpersandsToUTF8Hex(string original)
{
var pattern = new Regex("[&]");
return pattern.Replace(original, "%26");
}
}

public class WP_JWT_Object
Expand Down
3 changes: 2 additions & 1 deletion WordPress/v2/WPObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ public WPMediaItem(RestAPI api) : base(api)
API = api;
}

public async Task<T8> Add(string fileName, string filePath)
public async Task<T8> Add(string fileName, string filePath, bool fromUrl = false)
{
Dictionary<string, string> ps = new Dictionary<string, string>();
ps.Add("name", fileName);
ps.Add("path", filePath);
ps.Add("source", fromUrl ? "url" : "local");

return API.DeserializeJSon<T8>(await API.PostRestful(APIEndpoint, "fileupload", ps).ConfigureAwait(false));
}
Expand Down