55using Microsoft . AspNetCore . Http ;
66using Microsoft . AspNetCore . Mvc ;
77using Oqtane . Controllers ;
8+ using Oqtane . Enums ;
89using Oqtane . Infrastructure ;
10+ using Oqtane . Repository ;
11+ using Oqtane . Services ;
12+ using Oqtane . Shared ;
13+ using SixLabors . ImageSharp ;
14+ using System ;
915using System . Collections . Generic ;
1016using System . IO ;
1117using System . Threading . Tasks ;
12- using System ;
13- using Oqtane . Shared ;
14- using Oqtane . Enums ;
18+
1519
1620namespace GIBS . Module . BusinessDirectory . Server . Controllers
1721{
@@ -20,25 +24,30 @@ public class BusinessCompanyController : ModuleControllerBase
2024 {
2125 private readonly IBusinessCompanyRepository _businessCompanyRepository ;
2226 private readonly IWebHostEnvironment _env ;
23- // private readonly IFolderRepository _folders;
27+ private readonly IFileRepository _files ;
28+ private readonly IImageService _imageService ;
2429
2530 public BusinessCompanyController (
2631 IBusinessCompanyRepository businessCompanyRepository ,
2732 ILogManager logger ,
2833 IHttpContextAccessor accessor ,
29- IWebHostEnvironment env
34+ IWebHostEnvironment env ,
35+ IFileRepository files ,
36+ IImageService imageService
3037 ) : base ( logger , accessor )
3138 {
3239 _businessCompanyRepository = businessCompanyRepository ;
3340 _env = env ;
41+ _files = files ;
42+ _imageService = imageService ;
3443 }
3544
3645 // GET: api/BusinessCompany?moduleid=x
3746 [ HttpGet ]
3847 [ Authorize ( Policy = PolicyNames . ViewModule ) ]
3948 public async Task < IEnumerable < BusinessCompany > > Get ( string moduleid )
4049 {
41-
50+
4251 int moduleId ;
4352 if ( int . TryParse ( moduleid , out moduleId ) && IsAuthorizedEntityId ( EntityNames . Module , moduleId ) )
4453 {
@@ -157,6 +166,74 @@ public async Task<IActionResult> UploadImage([FromForm] IFormFile file)
157166 return Ok ( imageUrl ) ;
158167 }
159168
169+ [ HttpPost ( "resize-image" ) ]
170+ [ Authorize ( Policy = PolicyNames . EditModule ) ]
171+ public async Task < ActionResult < Oqtane . Models . File > > ResizeImage ( [ FromBody ] ResizeRequest request )
172+ {
173+ var file = _files . GetFile ( request . FileId ) ;
174+ if ( file == null || ! IsAuthorizedEntityId ( EntityNames . Module , request . ModuleId ) )
175+ {
176+ _logger . Log ( LogLevel . Error , this , LogFunction . Security , "Unauthorized Image Resize Attempt for FileId {FileId}" , request . FileId ) ;
177+ return Forbid ( ) ;
178+ }
179+
180+ string originalFilePath = null ;
181+ string tempFilePath = null ;
182+
183+ try
184+ {
185+ originalFilePath = _files . GetFilePath ( file ) ;
186+ if ( System . IO . File . Exists ( originalFilePath ) )
187+ {
188+ // 1. Create a temporary path for the resized image
189+ tempFilePath = Path . ChangeExtension ( originalFilePath , ".tmp" + Path . GetExtension ( originalFilePath ) ) ;
190+
191+ // 2. Use the IImageService to create the resized image at the temporary path
192+ var resizedImagePath = _imageService . CreateImage ( originalFilePath , request . Width , request . Height , "medium" , "center" , "white" , "" , file . Extension , tempFilePath ) ;
193+
194+ if ( string . IsNullOrEmpty ( resizedImagePath ) || ! System . IO . File . Exists ( resizedImagePath ) )
195+ {
196+ throw new Exception ( "Image resizing failed during temporary file creation." ) ;
197+ }
198+
199+ // 3. Update file metadata
200+ var fileInfo = new FileInfo ( resizedImagePath ) ;
201+ using ( var image = await Image . LoadAsync ( resizedImagePath ) )
202+ {
203+ file . ImageWidth = image . Width ;
204+ file . ImageHeight = image . Height ;
205+ }
206+ file . Size = ( int ) fileInfo . Length ;
207+
208+ // 4. The file is now resized and metadata is updated, but we need to replace the original file
209+ // At this point, all handles to the original file should be closed.
210+ System . IO . File . Delete ( originalFilePath ) ;
211+ System . IO . File . Move ( resizedImagePath , originalFilePath ) ;
212+
213+ _files . UpdateFile ( file ) ;
214+
215+ _logger . Log ( LogLevel . Information , this , LogFunction . Update , "Image Resized Successfully {FileId}" , request . FileId ) ;
216+ return Ok ( file ) ;
217+ }
218+ else
219+ {
220+ _logger . Log ( LogLevel . Error , this , LogFunction . Read , "File Not Found For Resizing {FileId}" , request . FileId ) ;
221+ return NotFound ( ) ;
222+ }
223+ }
224+ catch ( Exception ex )
225+ {
226+ // Clean up temporary file if it exists
227+ if ( ! string . IsNullOrEmpty ( tempFilePath ) && System . IO . File . Exists ( tempFilePath ) )
228+ {
229+ System . IO . File . Delete ( tempFilePath ) ;
230+ }
231+
232+ _logger . Log ( LogLevel . Error , this , LogFunction . Update , ex , "Error Resizing Image {FileId}" , request . FileId ) ;
233+ return StatusCode ( 500 , "An error occurred while resizing the image." ) ;
234+ }
235+ }
236+
160237 [ HttpPost ( "upload" ) ]
161238 public async Task < IActionResult > UploadFile ( [ FromForm ] string folder , [ FromForm ] IFormFile file )
162239 {
@@ -282,5 +359,13 @@ public class UpdateCompanyAttributesRequest
282359 public int ModuleId { get ; set ; }
283360 public List < int > AttributeIds { get ; set ; }
284361 }
362+
363+ public class ResizeRequest
364+ {
365+ public int FileId { get ; set ; }
366+ public int Width { get ; set ; }
367+ public int Height { get ; set ; }
368+ public int ModuleId { get ; set ; }
369+ }
285370 }
286371}
0 commit comments