From 4fb953b9df93be797201349d70cfb7a25ee0194f Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Tue, 23 May 2017 17:27:26 +0200 Subject: Reverted to using Imgur for storing images. --- .../ApartmentManager/ApartmentManager.csproj | 2 +- .../Common/ByteArrayToImageConverter.cs | 45 -------------------- .../ApartmentManager/Handler/ApartmentHandler.cs | 29 +------------ .../ApartmentManager/Model/Apartment.cs | 2 +- .../ApartmentManager/Model/Resident.cs | 2 +- ApartmentManager/ApartmentManager/Model/User.cs | 2 +- .../Persistency/ImgurPhotoUploader.cs | 48 ++++++++++++++++++++++ .../View/ApartmentResidentsPage.xaml | 6 +-- ApartmentManager/HousingWebApi/DataModel.cs | 24 +++++++++++ .../HousingWebApi/HousingWebApi.csproj | 23 ++++++----- ApartmentManager/HousingWebApi/Models/Apartment.cs | 3 +- .../HousingWebApi/Models/ChangeDocument.cs | 3 +- .../HousingWebApi/Models/DefectPicture.cs | 3 +- ApartmentManager/HousingWebApi/Models/PastUser.cs | 3 +- ApartmentManager/HousingWebApi/Models/Resident.cs | 3 +- ApartmentManager/HousingWebApi/Models/User.cs | 3 +- 16 files changed, 102 insertions(+), 99 deletions(-) delete mode 100644 ApartmentManager/ApartmentManager/Common/ByteArrayToImageConverter.cs create mode 100644 ApartmentManager/ApartmentManager/Persistency/ImgurPhotoUploader.cs diff --git a/ApartmentManager/ApartmentManager/ApartmentManager.csproj b/ApartmentManager/ApartmentManager/ApartmentManager.csproj index e628635..f267a50 100644 --- a/ApartmentManager/ApartmentManager/ApartmentManager.csproj +++ b/ApartmentManager/ApartmentManager/ApartmentManager.csproj @@ -98,7 +98,6 @@ AppShell.xaml - @@ -106,6 +105,7 @@ + diff --git a/ApartmentManager/ApartmentManager/Common/ByteArrayToImageConverter.cs b/ApartmentManager/ApartmentManager/Common/ByteArrayToImageConverter.cs deleted file mode 100644 index 64526a1..0000000 --- a/ApartmentManager/ApartmentManager/Common/ByteArrayToImageConverter.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Windows.Storage; -using Windows.Storage.Streams; -using Windows.UI.Xaml.Data; -using Windows.UI.Xaml.Media.Imaging; - -namespace ApartmentManager.Common -{ - class ByteArrayToImageConverter : IValueConverter - { - public object Convert(object value, Type targetType, object parameter, string language) - { - if (value != null) - { - BitmapImage image = new BitmapImage(); - using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream()) - { - stream.AsStreamForWrite().Write((byte[])value, 0, ((byte[])value).Length); - image.SetSource(stream); - } - return image; - } - else return null; - } - - public object ConvertBack(object value, Type targetType, object parameter, string language) - { - throw new NotImplementedException(); - } - - public async Task ConvertToByte(object value, Type targetType, object parameter, string language) - { - IRandomAccessStream random = await RandomAccessStreamReference.CreateFromFile((StorageFile)value).OpenReadAsync(); - Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(random); - Windows.Graphics.Imaging.PixelDataProvider pixelData = await decoder.GetPixelDataAsync(); - byte[] bytes = pixelData.DetachPixelData(); - return bytes; - } - } -} diff --git a/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs b/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs index 79b1682..c5dd6f0 100644 --- a/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs +++ b/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs @@ -144,41 +144,14 @@ namespace ApartmentManager.Handler { try { - //Create new file picker - FileOpenPicker fp = new FileOpenPicker() - { - SuggestedStartLocation = PickerLocationId.PicturesLibrary, - ViewMode = PickerViewMode.Thumbnail, - CommitButtonText = "Upload" - }; - fp.FileTypeFilter.Add(".jpg"); - fp.FileTypeFilter.Add(".jpeg"); - fp.FileTypeFilter.Add(".png"); - fp.FileTypeFilter.Add(".gif"); - fp.FileTypeFilter.Add(".apng"); - fp.FileTypeFilter.Add(".tiff"); - fp.FileTypeFilter.Add(".tif"); - fp.FileTypeFilter.Add(".bmp"); - fp.FileTypeFilter.Add(".pdf"); - fp.FileTypeFilter.Add(".xcf"); - fp.FileTypeFilter.Add(".webp"); - - //Get image file with picker - StorageFile file = await fp.PickSingleFileAsync(); - - ByteArrayToImageConverter converter = new ByteArrayToImageConverter(); - ApartmentViewModel.NewResident.Picture = (byte[]) await converter.ConvertToByte(file, typeof(System.Byte), null, ""); - + ApartmentViewModel.NewResident.Picture = await ImgurPhotoUploader.UploadPhotoAsync(); var tmp = ApartmentViewModel.NewResident; ApartmentViewModel.NewResident = new Resident(); ApartmentViewModel.NewResident = tmp; } catch (Exception e) { - - } - } } } diff --git a/ApartmentManager/ApartmentManager/Model/Apartment.cs b/ApartmentManager/ApartmentManager/Model/Apartment.cs index 06b427d..74fe410 100644 --- a/ApartmentManager/ApartmentManager/Model/Apartment.cs +++ b/ApartmentManager/ApartmentManager/Model/Apartment.cs @@ -8,7 +8,7 @@ public double? MonthlyCharge { get; set; } public int? Floor { get; set; } public string Address { get; set; } - public byte[] PlanPicture { get; set; } + public string PlanPicture { get; set; } public Apartment() { } diff --git a/ApartmentManager/ApartmentManager/Model/Resident.cs b/ApartmentManager/ApartmentManager/Model/Resident.cs index d3d18d3..ac20ffa 100644 --- a/ApartmentManager/ApartmentManager/Model/Resident.cs +++ b/ApartmentManager/ApartmentManager/Model/Resident.cs @@ -11,7 +11,7 @@ namespace ApartmentManager.Model public DateTime BirthDate { get; set; } public string Phone { get; set; } public string Email { get; set; } - public byte[] Picture { get; set; } + public string Picture { get; set; } public Resident() { diff --git a/ApartmentManager/ApartmentManager/Model/User.cs b/ApartmentManager/ApartmentManager/Model/User.cs index ea0e004..42964af 100644 --- a/ApartmentManager/ApartmentManager/Model/User.cs +++ b/ApartmentManager/ApartmentManager/Model/User.cs @@ -18,7 +18,7 @@ namespace ApartmentManager.Model public DateTime BirthDate { get; set; } public string Phone { get; set; } public string Email { get; set; } - public byte[] Picture { get; set; } + public string Picture { get; set; } public DateTime? MoveInDate { get; set; } public DateTime? MoveOutDate { get; set; } diff --git a/ApartmentManager/ApartmentManager/Persistency/ImgurPhotoUploader.cs b/ApartmentManager/ApartmentManager/Persistency/ImgurPhotoUploader.cs new file mode 100644 index 0000000..7e9ebf2 --- /dev/null +++ b/ApartmentManager/ApartmentManager/Persistency/ImgurPhotoUploader.cs @@ -0,0 +1,48 @@ +using Imgur.API.Authentication.Impl; +using Imgur.API.Endpoints.Impl; +using Imgur.API.Models; +using System; +using System.IO; +using System.Threading.Tasks; +using Windows.Storage; +using Windows.Storage.Pickers; + +namespace ApartmentManager.Persistency +{ + public static class ImgurPhotoUploader + { + /// + /// Opens file picker, uploads chosen image and returns link to it. + /// + public async static Task UploadPhotoAsync() + { + //Create new file picker + FileOpenPicker fp = new FileOpenPicker() + { + SuggestedStartLocation = PickerLocationId.PicturesLibrary, + ViewMode = PickerViewMode.Thumbnail, + CommitButtonText = "Upload" + }; + fp.FileTypeFilter.Add(".jpg"); + fp.FileTypeFilter.Add(".jpeg"); + fp.FileTypeFilter.Add(".png"); + fp.FileTypeFilter.Add(".gif"); + fp.FileTypeFilter.Add(".apng"); + fp.FileTypeFilter.Add(".tiff"); + fp.FileTypeFilter.Add(".tif"); + fp.FileTypeFilter.Add(".bmp"); + fp.FileTypeFilter.Add(".pdf"); + fp.FileTypeFilter.Add(".xcf"); + fp.FileTypeFilter.Add(".webp"); + + //Get image file with picker + StorageFile file = await fp.PickSingleFileAsync(); + + //Upload to Imgur and return link + var client = new ImgurClient("7b05a61ed8df74f", "ade6f79163e19f92f852bc553bbe399d7d4218fe"); + var endpoint = new ImageEndpoint(client); + IImage image = await endpoint.UploadImageStreamAsync(await file.OpenStreamForReadAsync()); + return image.Link; + } + } +} \ No newline at end of file diff --git a/ApartmentManager/ApartmentManager/View/ApartmentResidentsPage.xaml b/ApartmentManager/ApartmentManager/View/ApartmentResidentsPage.xaml index 3816b09..ca92dd2 100644 --- a/ApartmentManager/ApartmentManager/View/ApartmentResidentsPage.xaml +++ b/ApartmentManager/ApartmentManager/View/ApartmentResidentsPage.xaml @@ -9,10 +9,6 @@ x:Class="ApartmentManager.View.ApartmentResidentsPage" mc:Ignorable="d"> - - - - @@ -83,7 +79,7 @@ - + diff --git a/ApartmentManager/HousingWebApi/DataModel.cs b/ApartmentManager/HousingWebApi/DataModel.cs index 485c697..26b2608 100644 --- a/ApartmentManager/HousingWebApi/DataModel.cs +++ b/ApartmentManager/HousingWebApi/DataModel.cs @@ -31,6 +31,10 @@ namespace HousingWebApi .Property(e => e.Address) .IsUnicode(false); + modelBuilder.Entity() + .Property(e => e.PlanPicture) + .IsUnicode(false); + modelBuilder.Entity() .HasMany(e => e.ApartmentChanges) .WithRequired(e => e.Apartment) @@ -82,6 +86,10 @@ namespace HousingWebApi .Property(e => e.Comment) .IsUnicode(false); + modelBuilder.Entity() + .Property(e => e.Document) + .IsUnicode(false); + modelBuilder.Entity() .Property(e => e.Name) .IsUnicode(false); @@ -108,6 +116,10 @@ namespace HousingWebApi .Property(e => e.Picture) .IsUnicode(false); + modelBuilder.Entity() + .Property(e => e.Picture) + .IsUnicode(false); + modelBuilder.Entity() .Property(e => e.Username) .IsUnicode(false); @@ -132,6 +144,10 @@ namespace HousingWebApi .Property(e => e.Email) .IsUnicode(false); + modelBuilder.Entity() + .Property(e => e.Picture) + .IsUnicode(false); + modelBuilder.Entity() .Property(e => e.FirstName) .IsUnicode(false); @@ -148,6 +164,10 @@ namespace HousingWebApi .Property(e => e.Email) .IsUnicode(false); + modelBuilder.Entity() + .Property(e => e.Picture) + .IsUnicode(false); + modelBuilder.Entity() .Property(e => e.Username) .IsUnicode(false); @@ -172,6 +192,10 @@ namespace HousingWebApi .Property(e => e.Email) .IsUnicode(false); + modelBuilder.Entity() + .Property(e => e.Picture) + .IsUnicode(false); + modelBuilder.Entity() .Property(e => e.FirstName) .IsUnicode(false); diff --git a/ApartmentManager/HousingWebApi/HousingWebApi.csproj b/ApartmentManager/HousingWebApi/HousingWebApi.csproj index b009a06..4d60cac 100644 --- a/ApartmentManager/HousingWebApi/HousingWebApi.csproj +++ b/ApartmentManager/HousingWebApi/HousingWebApi.csproj @@ -151,9 +151,9 @@ - - - + + + @@ -186,8 +186,8 @@ - - + + @@ -200,16 +200,16 @@ - - - + + + Global.asax - + - - + + @@ -267,6 +267,7 @@ + diff --git a/ApartmentManager/HousingWebApi/Models/Apartment.cs b/ApartmentManager/HousingWebApi/Models/Apartment.cs index d1ac1d5..94c9b11 100644 --- a/ApartmentManager/HousingWebApi/Models/Apartment.cs +++ b/ApartmentManager/HousingWebApi/Models/Apartment.cs @@ -33,7 +33,8 @@ namespace HousingWebApi [StringLength(100)] public string Address { get; set; } - public byte[] PlanPicture { get; set; } + [StringLength(50)] + public string PlanPicture { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection ApartmentChanges { get; set; } diff --git a/ApartmentManager/HousingWebApi/Models/ChangeDocument.cs b/ApartmentManager/HousingWebApi/Models/ChangeDocument.cs index 9e910dc..7e869df 100644 --- a/ApartmentManager/HousingWebApi/Models/ChangeDocument.cs +++ b/ApartmentManager/HousingWebApi/Models/ChangeDocument.cs @@ -15,7 +15,8 @@ namespace HousingWebApi public int ChangeId { get; set; } - public byte[] Document { get; set; } + [StringLength(50)] + public string Document { get; set; } public virtual ApartmentChange ApartmentChange { get; set; } } diff --git a/ApartmentManager/HousingWebApi/Models/DefectPicture.cs b/ApartmentManager/HousingWebApi/Models/DefectPicture.cs index 6a83338..5d27ba3 100644 --- a/ApartmentManager/HousingWebApi/Models/DefectPicture.cs +++ b/ApartmentManager/HousingWebApi/Models/DefectPicture.cs @@ -15,7 +15,8 @@ namespace HousingWebApi public int DefectId { get; set; } - public byte[] Picture { get; set; } + [StringLength(50)] + public string Picture { get; set; } public virtual Defect Defect { get; set; } } diff --git a/ApartmentManager/HousingWebApi/Models/PastUser.cs b/ApartmentManager/HousingWebApi/Models/PastUser.cs index f258d34..d0bb504 100644 --- a/ApartmentManager/HousingWebApi/Models/PastUser.cs +++ b/ApartmentManager/HousingWebApi/Models/PastUser.cs @@ -36,7 +36,8 @@ namespace HousingWebApi [StringLength(50)] public string Email { get; set; } - public byte[] Picture { get; set; } + [StringLength(50)] + public string Picture { get; set; } [Column(TypeName = "date")] public DateTime? MoveInDate { get; set; } diff --git a/ApartmentManager/HousingWebApi/Models/Resident.cs b/ApartmentManager/HousingWebApi/Models/Resident.cs index 0509d40..4d29f16 100644 --- a/ApartmentManager/HousingWebApi/Models/Resident.cs +++ b/ApartmentManager/HousingWebApi/Models/Resident.cs @@ -28,7 +28,8 @@ namespace HousingWebApi [StringLength(50)] public string Email { get; set; } - public byte[] Picture { get; set; } + [StringLength(50)] + public string Picture { get; set; } public virtual Apartment Apartment { get; set; } } diff --git a/ApartmentManager/HousingWebApi/Models/User.cs b/ApartmentManager/HousingWebApi/Models/User.cs index 019e788..12438c6 100644 --- a/ApartmentManager/HousingWebApi/Models/User.cs +++ b/ApartmentManager/HousingWebApi/Models/User.cs @@ -36,7 +36,8 @@ namespace HousingWebApi [StringLength(50)] public string Email { get; set; } - public byte[] Picture { get; set; } + [StringLength(50)] + public string Picture { get; set; } [Column(TypeName = "date")] public DateTime? MoveInDate { get; set; } -- cgit v1.2.3