Skip to content

Commit

Permalink
using modernhttpclient for uploading file streams
Browse files Browse the repository at this point in the history
  • Loading branch information
edatkinvey committed Jan 16, 2015
1 parent 4dc2469 commit d1118dd
Show file tree
Hide file tree
Showing 43 changed files with 60,104 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Kinvey-Utils/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ namespace KinveyUtils
{
public class Logger
{
public static bool initialized {get; set;} = false;
public static bool initialized {get; set;}

public static Action<string> logIt {get; set;}

public static void initialize(Action<string> logAction){
if (logAction != null) {
logIt = logAction;
initialized = true;
} else {
initialized = false;
}
}

Expand Down
17 changes: 17 additions & 0 deletions Kinvey-Xamarin/Async/AsyncFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ public void upload(FileMetaData metadata, byte[] content, KinveyDelegate<FileMet

}

/// <summary>
/// Upload the specified stream to Kinvey's file storage. The FileMetaData contains extra data about the file.
/// </summary>
/// <param name="metadata">Metadata associated with the file, supports arbitrary key/value pairs</param>
/// <param name="content">the actual bytes of the file to upload.</param>
/// <param name="delegates">Delegates for success or failure.</param>
public void upload(FileMetaData metadata, Stream content, KinveyDelegate<FileMetaData> delegates){
Task.Run (() => {
try {
FileMetaData entity = base.uploadBlocking (metadata).executeAndUploadFrom (content);
delegates.onSuccess (entity);
} catch (Exception e) {
delegates.onError (e);
}
});
}

/// <summary>
/// Uploads metadata associated with a file, without changing the file itself. Do not modify the id or filename using this method-- it's for any other key/value pairs.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions Kinvey-Xamarin/File/FileMetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

using System;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace KinveyXamarin
{
Expand Down Expand Up @@ -83,6 +84,9 @@ public FileMetaData ()
/// <value><c>true</c> if public; otherwise, <c>false</c>.</value>
[JsonProperty("_public")]
public bool _public {get; set;}

[JsonProperty("_requiredHeaders")]
public Dictionary<string, string> headers;
}
}

40 changes: 39 additions & 1 deletion Kinvey-Xamarin/File/KinveyFileRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using System.Collections.Generic;
using RestSharp;
using System.IO;
using System.Net.Http;
using ModernHttpClient;

namespace KinveyXamarin
{
Expand Down Expand Up @@ -44,6 +46,12 @@ public FileMetaData executeAndUploadFrom(byte[] input){
uploadFile (metadata, input);
return metadata;
}

public FileMetaData executeAndUploadFrom(Stream stream){
FileMetaData metadata = base.Execute ();
uploadFile (metadata, stream);
return metadata;
}

private void downloadFile(FileMetaData metadata, Stream stream){
string downloadURL = metadata.downloadURL;
Expand Down Expand Up @@ -83,13 +91,43 @@ private void uploadFile(FileMetaData metadata, byte[] input){
} else {
request.Method = Method.POST;
}

//TODO what are these parameters for `name` and `filename` used for?
request.AddFile ("test", input, "filenameTest");

var req = client.ExecuteAsync (request);
var response = req.Result;
}

private void uploadFile(FileMetaData metadata, Stream input){
string uploadURL = metadata.uploadUrl;

var httpClient = new HttpClient(new NativeMessageHandler());
Uri requestURI = new Uri (uploadURL);

foreach (string key in metadata.headers.Keys) {
httpClient.DefaultRequestHeaders.Add(key, metadata.headers[key]);


}

//StreamContent content = new StreamContent ();
if (input.CanSeek) {
input.Position = 0;
// httpClient.DefaultRequestHeaders.Add ("Content-Length", "1024");
// httpClient.
}

//var content = new MultipartContent ();
//content.Add(new StreamContent(input));


var req = httpClient.PutAsync(requestURI, new StreamContent(input));
var response = req.Result;
response.EnsureSuccessStatusCode();


}
}
}

3 changes: 3 additions & 0 deletions Kinvey-Xamarin/Kinvey-Xamarin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
<Reference Include="SQLite.Net.Async">
<HintPath>..\packages\SQLite.Net.Async-PCL.2.5\lib\portable-win8+net45+wp8+wpa81+MonoAndroid1+MonoTouch1\SQLite.Net.Async.dll</HintPath>
</Reference>
<Reference Include="ModernHttpClient">
<HintPath>..\packages\modernhttpclient.2.1.5\lib\Portable-Net45+WinRT45+WP8+WPA81\ModernHttpClient.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RestSharp.Portable\RestSharp.Portable.csproj">
Expand Down
5 changes: 5 additions & 0 deletions Kinvey-Xamarin/Offline/AbstractKinveyOfflineClientRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,15 @@ public async override Task<T> ExecuteAsync(){
}

} else if (policy == OfflinePolicy.ONLINE_FIRST) {
bool failed = false;
try {
ret = await offlineFromServiceAsync ();
} catch (Exception e) {
Logger.Log (e);
failed = true;

}
if (failed) {
ret = await offlineFromStoreAsync ();
}
}
Expand Down
1 change: 1 addition & 0 deletions Kinvey-Xamarin/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="portable-net45+win+wp80+MonoTouch10+MonoAndroid10+xamarinmac20+xamarinios10" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="portable-net45+win+wp80+MonoTouch10+MonoAndroid10+xamarinmac20+xamarinios10" />
<package id="Microsoft.Net.Http" version="2.2.28" targetFramework="portable-net45+win+wp80+MonoTouch10+MonoAndroid10+xamarinmac20+xamarinios10" />
<package id="modernhttpclient" version="2.1.5" targetFramework="portable-net45+win+wp80+MonoTouch10+MonoAndroid10+xamarinmac20+xamarinios10" />
<package id="Newtonsoft.Json" version="6.0.7" targetFramework="portable-net45+win+wp80+MonoTouch10+MonoAndroid10+xamarinmac20+xamarinios10" />
<package id="SQLite.Net.Async-PCL" version="2.5" targetFramework="portable-net45+win+wp80+MonoTouch10+MonoAndroid10+xamarinmac20+xamarinios10" />
<package id="SQLite.Net-PCL" version="2.5" targetFramework="portable-net45+win+wp80+MonoTouch10+MonoAndroid10+xamarinmac20+xamarinios10" />
Expand Down
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit d1118dd

Please sign in to comment.