22using FilesAPI . ViewModels ;
33using FilesAPI . ViewModels . Mapper ;
44using Microsoft . AspNetCore . Mvc ;
5- using Microsoft . Extensions . DependencyInjection ;
65using Models ;
6+ using Services ;
77using System ;
88using System . Collections . Generic ;
99using System . Linq ;
@@ -17,10 +17,12 @@ namespace FilesAPI.Controllers;
1717public class StorageController : ControllerBase
1818{
1919 private readonly IStorageService _storageService ;
20+ private readonly EventHandlerContainer _eventContainer ;
2021
21- public StorageController ( IStorageService storageService )
22+ public StorageController ( IStorageService storageService , EventHandlerContainer eventContainer )
2223 {
2324 _storageService = storageService ;
25+ _eventContainer = eventContainer ;
2426 }
2527
2628 //Example from https://dottutorials.net/dotnet-core-web-api-multipart-form-data-upload-file/
@@ -56,27 +58,8 @@ public async Task<IActionResult> DownLoadFile(string id, CancellationToken token
5658 {
5759 var ( content , details ) = await _storageService . DownloadFileAsync ( id , token ) ;
5860
59- // Record analytics
60- var userAgent = Request . Headers [ "User-Agent" ] . ToString ( ) ;
61- var ipAddress = HttpContext . Connection . RemoteIpAddress ? . ToString ( ) ;
62- var referrer = Request . Headers [ "Referer" ] . ToString ( ) ;
63-
64- // Fire and forget analytics recording
65- _ = Task . Run ( async ( ) =>
66- {
67- try
68- {
69- var analyticsService = HttpContext . RequestServices . GetService < IAnalyticsService > ( ) ;
70- if ( analyticsService != null )
71- {
72- await analyticsService . RecordDownloadAsync ( id , userAgent , ipAddress , referrer , "download" , token ) ;
73- }
74- }
75- catch
76- {
77- // Ignore analytics errors to not affect file download
78- }
79- } , token ) ;
61+ // Record analytics for view
62+ await RecordAnalyticsAsync ( details , token ) ;
8063
8164 this . Response . ContentLength = details . Size ;
8265 this . Response . Headers [ "Accept-Ranges" ] = "bytes" ;
@@ -90,26 +73,7 @@ public async Task<FileStreamResult> DownloadView(string id, CancellationToken to
9073 var ( stream , details ) = await _storageService . DownloadFileAsync ( id , token ) ;
9174
9275 // Record analytics for view
93- var userAgent = Request . Headers [ "User-Agent" ] . ToString ( ) ;
94- var ipAddress = HttpContext . Connection . RemoteIpAddress ? . ToString ( ) ;
95- var referrer = Request . Headers [ "Referer" ] . ToString ( ) ;
96-
97- // Fire and forget analytics recording
98- _ = Task . Run ( async ( ) =>
99- {
100- try
101- {
102- var analyticsService = HttpContext . RequestServices . GetService < IAnalyticsService > ( ) ;
103- if ( analyticsService != null )
104- {
105- await analyticsService . RecordDownloadAsync ( id , userAgent , ipAddress , referrer , "view" , token ) ;
106- }
107- }
108- catch
109- {
110- // Ignore analytics errors to not affect file download
111- }
112- } , token ) ;
76+ await RecordAnalyticsAsync ( details , token ) ;
11377
11478 this . Response . ContentLength = details . Size ;
11579 this . Response . Headers [ "Accept-Ranges" ] = "bytes" ;
@@ -152,4 +116,22 @@ public async Task<ActionResult<string>> DeleteFileAsync(string id, CancellationT
152116 string deletedId = await _storageService . DeleteFileAsync ( id , token ) ;
153117 return Ok ( $ "Deleted '{ deletedId } ' successfully.") ;
154118 }
119+
120+ private async Task RecordAnalyticsAsync ( FileDetails details , CancellationToken token )
121+ {
122+ var userAgent = Request . Headers [ "User-Agent" ] . ToString ( ) ;
123+ var ipAddress = HttpContext . Connection . RemoteIpAddress ? . ToString ( ) ;
124+ var referrer = Request . Headers [ "Referer" ] . ToString ( ) ;
125+ var enhancedFileDownloadedEvent = new EnhancedFileDownloadedEvent
126+ {
127+ DownloadMethod = "view" ,
128+ DownloadStartTime = DateTime . UtcNow ,
129+ FileDetails = details ,
130+ UserAgent = userAgent ,
131+ IpAddress = ipAddress ,
132+ Referrer = referrer ,
133+ RequestId = HttpContext . TraceIdentifier
134+ } ;
135+ await _eventContainer . PublishAsync ( enhancedFileDownloadedEvent , token ) ;
136+ }
155137}
0 commit comments