Skip to content

Commit

Permalink
change album cache names once again
Browse files Browse the repository at this point in the history
  • Loading branch information
th0mk committed Apr 16, 2024
1 parent 5525c6f commit c36f1cc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/FMBot.Bot/Builders/AlbumBuilders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -840,15 +840,15 @@ public class AlbumBuilders
var footer = new StringBuilder();

footer.Append($"{albumTracks.Count} total tracks");

if (albumTracks.All(a => a.DurationSeconds.HasValue))
{
var totalLength = TimeSpan.FromSeconds(albumTracks.Sum(s => s.DurationSeconds ?? 0));
var formattedTrackLength =
$"{(totalLength.Hours == 0 ? "" : $"{totalLength.Hours}:")}{totalLength.Minutes}:{totalLength.Seconds:D2}";
footer.Append($"{formattedTrackLength}");
}

footer.AppendLine();
footer.Append(spotifySource ? "Album source: Spotify | " : "Album source: Last.fm | ");
footer.Append($"{userSettings.DisplayName} has {albumSearch.Album.UserPlaycount} total scrobbles on this album");
Expand Down Expand Up @@ -1076,7 +1076,7 @@ public class AlbumBuilders
response.Spoiler = safeForChannel == CensorService.CensorResult.Nsfw;


var cacheFilePath = ChartService.AlbumUrlToCacheFilePath(albumCoverUrl);
var cacheFilePath = ChartService.AlbumUrlToCacheFilePath(albumSearch.Album.AlbumName, albumSearch.Album.ArtistName);
await ChartService.OverwriteCache(cacheStream, cacheFilePath);

await cacheStream.DisposeAsync();
Expand Down
12 changes: 5 additions & 7 deletions src/FMBot.Bot/Services/ChartService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ public async Task<SKImage> GenerateChartAsync(ChartSettings chart)
SKBitmap chartImage;
var validImage = true;

var localPath = AlbumUrlToCacheFilePath(album.AlbumCoverUrl);
var localPath = AlbumUrlToCacheFilePath(album.AlbumName, album.ArtistName);

if (File.Exists(localPath) && cacheEnabled)
if (localPath != null && File.Exists(localPath) && cacheEnabled)
{
chartImage = SKBitmap.Decode(localPath);
Statistics.LastfmCachedImageCalls.Inc();
Expand Down Expand Up @@ -323,12 +323,10 @@ public async Task<SKImage> GenerateChartAsync(ChartSettings chart)
}
}

public static string AlbumUrlToCacheFilePath(string albumUrl)
public static string AlbumUrlToCacheFilePath(string albumName, string artistName)
{
var encodedId = EncodeToBase64(albumUrl
.Replace("https://lastfm.freetls.fastly.net/", "")
.Replace("https://i.scdn.co/image/", ""));
var localAlbumId = StringExtensions.TruncateLongString($"album_{encodedId}", 80);
var encodedId = EncodeToBase64($"{StringExtensions.TruncateLongString(albumName, 40)}--{StringExtensions.TruncateLongString(artistName, 40)}");
var localAlbumId = StringExtensions.TruncateLongString($"album_{encodedId}", 100);

var fileName = localAlbumId + ".png";
var localPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cache", fileName);
Expand Down

0 comments on commit c36f1cc

Please sign in to comment.