11using Microsoft . AspNetCore . Authorization ;
22using Microsoft . AspNetCore . Http ;
33using Microsoft . AspNetCore . Mvc ;
4+ using Microsoft . Net . Http . Headers ;
45using Newtonsoft . Json ;
56using SimplCommerce . Infrastructure . Data ;
67using SimplCommerce . Module . Cms . ViewModels ;
78using SimplCommerce . Module . Core . Models ;
9+ using SimplCommerce . Module . Core . Services ;
810using System ;
911using System . Collections . Generic ;
12+ using System . IO ;
1013using System . Linq ;
1114using System . Threading . Tasks ;
1215
@@ -18,11 +21,13 @@ public class SpaceBarWidgetApiContorller : Controller
1821 {
1922 private readonly IRepository < WidgetInstance > _widgetInstanceRepository ;
2023 private readonly IRepository < Widget > _widgetRespository ;
24+ private readonly IMediaService _mediaService ;
2125
22- public SpaceBarWidgetApiContorller ( IRepository < WidgetInstance > widgetInstanceRepository , IRepository < Widget > widgetRepository )
26+ public SpaceBarWidgetApiContorller ( IRepository < WidgetInstance > widgetInstanceRepository , IRepository < Widget > widgetRepository , IMediaService mediaService )
2327 {
2428 _widgetInstanceRepository = widgetInstanceRepository ;
2529 _widgetRespository = widgetRepository ;
30+ _mediaService = mediaService ;
2631 }
2732
2833 [ HttpGet ( "{id}" ) ]
@@ -39,16 +44,24 @@ public IActionResult Get(long id)
3944 DisplayOrder = widgetInstance . DisplayOrder ,
4045 Items = JsonConvert . DeserializeObject < IList < SpaceBarWidgetSetting > > ( widgetInstance . Data )
4146 } ;
42-
47+ foreach ( var item in model . Items )
48+ {
49+ item . ImageUrl = _mediaService . GetMediaUrl ( item . Image ) ;
50+ }
4351 return Json ( model ) ;
4452 }
4553
4654 [ HttpPost ]
47- public IActionResult Post ( IFormCollection formCollection )
55+ public async Task < IActionResult > Post ( IFormCollection formCollection )
4856 {
4957 var model = ToSpaceBarWidgetFormModel ( formCollection ) ;
5058 if ( ModelState . IsValid )
5159 {
60+ foreach ( var item in model . Items )
61+ {
62+ item . Image = await SaveFile ( item . UploadImage ) ;
63+ }
64+
5265 var widgetInstance = new WidgetInstance
5366 {
5467 Name = model . Name ,
@@ -66,9 +79,21 @@ public IActionResult Post(IFormCollection formCollection)
6679 return new BadRequestObjectResult ( ModelState ) ;
6780 }
6881 [ HttpPut ( "{id}" ) ]
69- public IActionResult Put ( long id , IFormCollection formCollection )
82+ public async Task < IActionResult > Put ( long id , IFormCollection formCollection )
7083 {
7184 var model = ToSpaceBarWidgetFormModel ( formCollection ) ;
85+
86+ foreach ( var item in model . Items )
87+ {
88+ if ( item . UploadImage != null )
89+ {
90+ if ( ! string . IsNullOrWhiteSpace ( item . Image ) )
91+ {
92+ await _mediaService . DeleteMediaAsync ( item . Image ) ;
93+ }
94+ item . Image = await SaveFile ( item . UploadImage ) ;
95+ }
96+ }
7297 if ( ModelState . IsValid )
7398 {
7499 var widgetInstance = _widgetInstanceRepository . Query ( ) . FirstOrDefault ( x => x . Id == id ) ;
@@ -106,9 +131,18 @@ private SpaceBarWidgetForm ToSpaceBarWidgetFormModel(IFormCollection formCollect
106131 item . Title = formCollection [ $ "items[{ i } ][title]"] ;
107132 item . Description = formCollection [ $ "items[{ i } ][description]"] ;
108133 item . IconHtml = formCollection [ $ "items[{ i } ][iconHtml]"] ;
134+ item . Image = formCollection [ $ "items[{ i } ][image]"] ;
135+ item . UploadImage = formCollection . Files [ $ "items[{ i } ][uploadImage]"] ;
109136 model . Items . Add ( item ) ;
110137 }
111138 return model ;
112139 }
140+ private async Task < string > SaveFile ( IFormFile file )
141+ {
142+ var originalFileName = ContentDispositionHeaderValue . Parse ( file . ContentDisposition ) . FileName . Value . Trim ( '"' ) ;
143+ var fileName = $ "{ Guid . NewGuid ( ) } { Path . GetExtension ( originalFileName ) } ";
144+ await _mediaService . SaveMediaAsync ( file . OpenReadStream ( ) , fileName , file . ContentType ) ;
145+ return fileName ;
146+ }
113147 }
114148}
0 commit comments