From b36c67f870db0079843e6008eb31d114ea7d8a96 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 3 Oct 2016 18:40:22 +0200 Subject: [PATCH] implements an upload --- BookAStar/BookAStar/Helpers/UserHelpers.cs | 46 +++++++++++++++++++--- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/BookAStar/BookAStar/Helpers/UserHelpers.cs b/BookAStar/BookAStar/Helpers/UserHelpers.cs index f1d43366..d57b4f65 100644 --- a/BookAStar/BookAStar/Helpers/UserHelpers.cs +++ b/BookAStar/BookAStar/Helpers/UserHelpers.cs @@ -1,8 +1,12 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; +using System.IO; using System.Linq; +using System.Net.Http; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using Xamarin.Forms; @@ -12,17 +16,49 @@ namespace BookAStar.Helpers { public static ImageSource Avatar(string avatarPath) { - /*foreach (var r in App.Current.Resources) - { - Debug.WriteLine($"#R# {r.Key} : {r.GetType().Name}"); - }*/ var result = avatarPath == null ? - ImageSource.FromResource(/* "BookAStar.icon_user.png" */ "BookAStar.Images.icon_user.png") : + ImageSource.FromResource( "BookAStar.Images.icon_user.png") : avatarPath.StartsWith("res://") ? ImageSource.FromResource(avatarPath.Substring(6)) : ImageSource.FromUri(new Uri(avatarPath)); var test = ImageSource.FromResource("none.resource.png"); return result; } + + public static HttpClient CreateClient() + { + var client = new HttpClient(); + client.DefaultRequestHeaders.Authorization = + new System.Net.Http.Headers.AuthenticationHeaderValue( + "Bearer", MainSettings.CurrentUser.YavscTokens.AccessToken); + return client; + } + + /// + /// Uploads the given stream to + /// /api/fs, in order to be saved under the given file name + /// + /// + /// + /// + public static async Task Upload(Stream inputStream, string fileName) + { + using (var client = CreateClient()) + { + using (var content = + new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture))) + { + content.Add(new StreamContent(inputStream), "Files", fileName); + + using ( + var message = + await client.PostAsync(Constants.FsUrl, content)) + { + return (message.IsSuccessStatusCode); + } + } + } + } + } }