Skip to content

Commit

Permalink
Add file upload support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérémy Filhoulaud committed Nov 25, 2020
1 parent e01a2f2 commit eab4b15
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 6 deletions.
1 change: 1 addition & 0 deletions AddOrChangeAssetPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<TextBox x:Uid="AddAssetNameTextBox" x:Name="TextBoxName" Margin="20,2" MaxLength="30" PlaceholderText="" />
<TextBlock x:Uid="AddAssetUrl" Text="" HorizontalAlignment="Left" Margin="20, 20, 20, 2"/>
<TextBox x:Uid="AddAssetUrlTextBox" x:Name="TextBoxUrl" Margin="20,2" PlaceholderText="" />
<Button x:Uid="AddAssetUrlFileButton" Grid.Column="1" x:Name="ButtonFile" Content="" Width="150" HorizontalAlignment="Left" Margin="20, 2" Click="ButtonFile_Click"/>
<TextBlock x:Uid="AddAssetType" Text="" HorizontalAlignment="Left" Margin="20, 20, 20, 2"/>
<ComboBox x:Name="ComboBoxAssetType" HorizontalAlignment="Left" Margin="20, 2" HorizontalContentAlignment="Stretch" ItemsSource="{x:Bind MimeTypes}" DisplayMemberPath="Item2" SelectedValuePath="Item1" Loaded="ComboBoxAssetType_Loaded"/>
<TextBlock x:Uid="AddAssetStartDate" Text="" HorizontalAlignment="Left" Margin="20, 20, 20, 2"/>
Expand Down
36 changes: 33 additions & 3 deletions AddOrChangeAssetPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
Expand All @@ -22,6 +24,8 @@ public sealed partial class AddOrChangeAssetPage : Page
public List<Tuple<string, string>> MimeTypes { get; set; }
private bool IsAnUpdate { get; set; }

private byte[] UploadedFile { get; set; }

private Windows.ApplicationModel.Resources.ResourceLoader Loader;

public AddOrChangeAssetPage()
Expand All @@ -37,9 +41,14 @@ public AddOrChangeAssetPage()
new Tuple<string, string>("video", this.Loader.GetString("Video"))
};

this.UploadedFile = null;

this.InitializeComponent();

this.DatePickerEnd.Date = DateTime.Now.AddDays(1);
this.DatePickerStart.Date = DateTime.Now;
this.TimePickerStart.Time = DatePickerStart.Date.TimeOfDay;
this.DatePickerEnd.Date = this.DatePickerStart.Date.AddDays(1);
this.TimePickerEnd.Time = this.TimePickerStart.Time;
}

protected override async void OnNavigatedTo(NavigationEventArgs e)
Expand Down Expand Up @@ -108,7 +117,10 @@ private async void ButtonSubmit_Click(object sender, RoutedEventArgs e)
{
Asset a = this.AssetToUpdate;
a.Name = this.TextBoxName.Text;
a.Uri = this.TextBoxUrl.Text;
if (this.TextBoxUrl.Text.StartsWith("{"))
a.LocalToken = this.TextBoxUrl.Text;
else
a.Uri = this.TextBoxUrl.Text;
a.StartDate = (this.DatePickerStart.Date.Date + this.TimePickerStart.Time).ToUniversalTime();
a.EndDate = (this.DatePickerEnd.Date.Date + this.TimePickerEnd.Time).ToUniversalTime();
a.Duration = this.TextBoxDuration.Text;
Expand All @@ -131,7 +143,7 @@ private async void ButtonSubmit_Click(object sender, RoutedEventArgs e)
{
var devicesSelected = this.GridViewDevices.SelectedItems.ToList();
foreach (var device in devicesSelected)
await (device as Device).CreateAsset(a);
await (device as Device).CreateAssetAsync(a);
}
else
{
Expand Down Expand Up @@ -172,5 +184,23 @@ private void DatePickerStart_DateChanged(object sender, DatePickerValueChangedEv
{
this.DatePickerEnd.Date = this.DatePickerStart.Date.AddDays(1);
}

private async void ButtonFile_Click(object sender, RoutedEventArgs e)
{
var picker = new FileOpenPicker();
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".mp4");
picker.FileTypeFilter.Add(".avi");
picker.FileTypeFilter.Add(".mkv");

Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
// Application now has read/write access to the picked file
this.TextBoxUrl.Text = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
}
}
}
3 changes: 3 additions & 0 deletions Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public bool IsEnabledSwitch
}
}

[Newtonsoft.Json.JsonIgnore]
public string LocalToken { get; set; }

[Newtonsoft.Json.JsonProperty(PropertyName = "nocache")]
public Int32 NoCache { get; set; }

Expand Down
48 changes: 47 additions & 1 deletion Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.AccessCache;

namespace ScreenlyManager
{
Expand Down Expand Up @@ -244,8 +246,18 @@ public async Task UpdateOrderAssetsAsync(string newOrder)
/// </summary>
/// <param name="a">New asset to create on Raspberry</param>
/// <returns></returns>
public async Task CreateAsset(Asset a)
public async Task CreateAssetAsync(Asset a)
{
if (a.LocalToken != null)
{
var localFile = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(a.LocalToken);
Stream stream = await localFile.OpenStreamForReadAsync();
byte[] result = new byte[(int)stream.Length];
await stream.ReadAsync(result, 0, (int)stream.Length);
a.Uri = await this.PostFileAssetAsync(result, localFile.Name, localFile.ContentType);
a.Name += $" - {localFile.Name}";
}

Asset returnedAsset = new Asset();
JsonSerializerSettings settings = new JsonSerializerSettings();
IsoDateTimeConverter dateConverter = new IsoDateTimeConverter
Expand Down Expand Up @@ -325,6 +337,40 @@ public async Task<Asset> GetAssetAsync(string assetId)
return null;
}

/// <summary>
/// Send file to create asset (image or video)
/// </summary>
/// <param name="itemToSend">Byte array of the file</param>
/// <param name="fileName">File's name</param>
/// <param name="contentType">File's content type </param>
/// <returns></returns>
public async Task<string> PostFileAssetAsync(byte[] itemToSend, string fileName, string contentType)
{
string resultJson = string.Empty;
string parameters = $"/api/{this.ApiVersion}file_asset";

try
{
HttpClient request = new HttpClient();
var content = new MultipartFormDataContent();
var itemContent = new ByteArrayContent(itemToSend);
itemContent.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
content.Add(itemContent, "file_upload", fileName);
using (HttpResponseMessage response = await request.PostAsync(this.HttpLink + parameters, content))
{
resultJson = await response.Content.ReadAsStringAsync();
}

if (!resultJson.Equals(string.Empty))
return JsonConvert.DeserializeObject<string>(resultJson);
}
catch (Exception ex)
{
throw new Exception($"[Device = {this.Name}; IP = {this.IpAddress}] Error while sending file.", ex);
}
return null;
}

#endregion
}
}
Expand Down
4 changes: 4 additions & 0 deletions Strings/de-DE/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,8 @@
<value>Asset kopieren "{0}"</value>
<comment>AddOrChangeAssetPage.xaml</comment>
</data>
<data name="AddAssetUrlFileButton.Content" xml:space="preserve">
<value>Kies een bestand</value>
<comment>AddOrChangeAssetPage.xaml</comment>
</data>
</root>
6 changes: 5 additions & 1 deletion Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
<comment>AddOrChangeAssetPage.xaml</comment>
</data>
<data name="AddAssetUrlTextBox.PlaceholderText" xml:space="preserve">
<value>e.g. https://www.screenly.io/</value>
<value>e.g. https://www.screenly.io/ or choose a file below</value>
<comment>AddOrChangeAssetPage.xaml</comment>
</data>
<data name="AddDeviceCancel.Content" xml:space="preserve">
Expand Down Expand Up @@ -401,4 +401,8 @@
<value>Duplicate asset "{0}"</value>
<comment>AddOrChangeAssetPage.xaml</comment>
</data>
<data name="AddAssetUrlFileButton.Content" xml:space="preserve">
<value>Choose a file</value>
<comment>AddOrChangeAssetPage.xaml</comment>
</data>
</root>
6 changes: 5 additions & 1 deletion Strings/fr-FR/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
<comment>AddOrChangeAssetPage.xaml</comment>
</data>
<data name="AddAssetUrlTextBox.PlaceholderText" xml:space="preserve">
<value>Exemple : https://www.screenly.io/</value>
<value>Exemple : https://www.screenly.io/ ou choisir un fichier ci-dessous</value>
<comment>AddOrChangeAssetPage.xaml</comment>
</data>
<data name="AddDeviceCancel.Content" xml:space="preserve">
Expand Down Expand Up @@ -401,4 +401,8 @@
<value>Dupliquer l'affichage "{0}"</value>
<comment>AddOrChangeAssetPage.xaml</comment>
</data>
<data name="AddAssetUrlFileButton.Content" xml:space="preserve">
<value>Choisir un fichier</value>
<comment>AddOrChangeAssetPage.xaml</comment>
</data>
</root>
4 changes: 4 additions & 0 deletions Strings/nl-NL/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,8 @@
<value>Dubbele asset "{0}"</value>
<comment>AddOrChangeAssetPage.xaml</comment>
</data>
<data name="AddAssetUrlFileButton.Content" xml:space="preserve">
<value>Wählen Sie eine Datei</value>
<comment>AddOrChangeAssetPage.xaml</comment>
</data>
</root>

0 comments on commit eab4b15

Please sign in to comment.