implements an upload
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uploads the given stream to
|
||||
/// /api/fs, in order to be saved under the given file name
|
||||
/// </summary>
|
||||
/// <param name="inputStream"></param>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<bool> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user