Skip to content

Commit 4a2e9e7

Browse files
committed
Change favicon directory
1 parent 2250c74 commit 4a2e9e7

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

HomeSpeaker.Server2/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@
150150
//app.UseHttpsRedirection();
151151
app.UseBlazorFrameworkFiles();
152152
app.UseStaticFiles();
153+
154+
// Serve favicons from the media folder (writable volume) at /favicons
155+
var faviconsPath = Path.Combine(app.Configuration[ConfigKeys.MediaFolder] ?? "/music", "favicons");
156+
Directory.CreateDirectory(faviconsPath);
157+
app.UseStaticFiles(new StaticFileOptions
158+
{
159+
FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(faviconsPath),
160+
RequestPath = "/favicons"
161+
});
162+
153163
app.MapHealthChecks("/health", new HealthCheckOptions
154164
{
155165
ResponseWriter = async (context, report) =>

HomeSpeaker.Server2/Services/RadioStreamService.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,29 @@ public class RadioStreamService
88
{
99
private readonly MusicContext _dbContext;
1010
private readonly ILogger<RadioStreamService> _logger;
11-
private readonly IWebHostEnvironment _environment;
1211
private readonly IMemoryCache _cache;
1312
private readonly HttpClient _httpClient;
13+
private readonly string _faviconsDirectory;
1414

1515
private const string CACHE_KEY = "radio_streams_all";
1616
private static readonly TimeSpan CACHE_DURATION = TimeSpan.FromMinutes(5);
1717

1818
public RadioStreamService(
1919
MusicContext dbContext,
2020
ILogger<RadioStreamService> logger,
21-
IWebHostEnvironment environment,
21+
IConfiguration configuration,
2222
IMemoryCache cache,
2323
HttpClient httpClient)
2424
{
2525
_dbContext = dbContext;
2626
_logger = logger;
27-
_environment = environment;
2827
_cache = cache;
2928
_httpClient = httpClient;
3029
_httpClient.Timeout = TimeSpan.FromSeconds(10);
30+
31+
// Store favicons in the media folder (volume-mounted, writable) rather than wwwroot (read-only in container)
32+
var mediaFolder = configuration[ConfigKeys.MediaFolder] ?? "/music";
33+
_faviconsDirectory = Path.Combine(mediaFolder, "favicons");
3134
}
3235

3336
public async Task<IEnumerable<RadioStream>> GetAllStreamsAsync()
@@ -131,10 +134,9 @@ public async Task UpdateStreamAsync(int id, string name, string url, string? fav
131134
var baseName = GetSafeFileName(Path.GetFileNameWithoutExtension(file.FileName));
132135
var uniqueName = baseName + Guid.NewGuid().ToString("N")[..8] + extension;
133136

134-
var faviconDir = Path.Combine(_environment.WebRootPath, "favicons");
135-
Directory.CreateDirectory(faviconDir);
137+
Directory.CreateDirectory(_faviconsDirectory);
136138

137-
var filePath = Path.Combine(faviconDir, uniqueName);
139+
var filePath = Path.Combine(_faviconsDirectory, uniqueName);
138140
using var stream = File.Create(filePath);
139141
await file.CopyToAsync(stream);
140142

@@ -185,11 +187,10 @@ public async Task IncrementPlayCountAsync(int streamId)
185187

186188
// Generate safe filename from stream name
187189
var safeFileName = GetSafeFileName(streamName) + extension;
188-
var faviconDir = Path.Combine(_environment.WebRootPath, "favicons");
189-
var faviconPath = Path.Combine(faviconDir, safeFileName);
190+
var faviconPath = Path.Combine(_faviconsDirectory, safeFileName);
190191

191192
// Ensure favicons directory exists
192-
Directory.CreateDirectory(faviconDir);
193+
Directory.CreateDirectory(_faviconsDirectory);
193194

194195
var bytes = await response.Content.ReadAsByteArrayAsync();
195196
await File.WriteAllBytesAsync(faviconPath, bytes);
@@ -208,7 +209,7 @@ private void DeleteFavicon(string fileName)
208209
{
209210
try
210211
{
211-
var path = Path.Combine(_environment.WebRootPath, "favicons", fileName);
212+
var path = Path.Combine(_faviconsDirectory, fileName);
212213
if (File.Exists(path))
213214
{
214215
File.Delete(path);

0 commit comments

Comments
 (0)