Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite cache directory handling #1279

Merged
merged 5 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions TwitchDownloaderCLI/Modes/CacheHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using TwitchDownloaderCLI.Modes.Arguments;
using TwitchDownloaderCore.Services;

namespace TwitchDownloaderCLI.Modes
{
Expand Down Expand Up @@ -42,19 +43,17 @@ private static void PromptClearCache()

private static void ClearTempCache()
{
var defaultCacheDirectory = Path.Combine(Path.GetTempPath(), "TwitchDownloader");
var defaultCacheDirectory = CacheDirectoryService.GetCacheDirectory(Path.GetTempPath());
if (Directory.Exists(defaultCacheDirectory))
{
Console.WriteLine("Clearing cache...");
try
if (CacheDirectoryService.ClearCacheDirectory(Path.GetTempPath(), out var exception))
{
Directory.Delete(defaultCacheDirectory, true);
Console.WriteLine("Cache cleared successfully.");
return;
}
catch (UnauthorizedAccessException)
{
Console.WriteLine("Insufficient access to clear cache folder.");
}

Console.WriteLine($"Failed to clear cache: {exception.Message}");
return;
}

Expand Down
14 changes: 7 additions & 7 deletions TwitchDownloaderCore/ChatDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using TwitchDownloaderCore.Chat;
using TwitchDownloaderCore.Interfaces;
using TwitchDownloaderCore.Options;
using TwitchDownloaderCore.Services;
using TwitchDownloaderCore.Tools;
using TwitchDownloaderCore.TwitchObjects;
using TwitchDownloaderCore.TwitchObjects.Gql;
Expand All @@ -20,6 +21,7 @@ public sealed class ChatDownloader
{
private readonly ChatDownloadOptions downloadOptions;
private readonly ITaskProgress _progress;
private readonly string _cacheDir;

private static readonly HttpClient HttpClient = new()
{
Expand All @@ -37,9 +39,7 @@ public ChatDownloader(ChatDownloadOptions chatDownloadOptions, ITaskProgress pro
{
downloadOptions = chatDownloadOptions;
_progress = progress;
downloadOptions.TempFolder = Path.Combine(
string.IsNullOrWhiteSpace(downloadOptions.TempFolder) ? Path.GetTempPath() : Path.GetFullPath(downloadOptions.TempFolder),
"TwitchDownloader");
_cacheDir = CacheDirectoryService.GetCacheDirectory(downloadOptions.TempFolder);
}

private static async Task<List<Comment>> DownloadSection(Range downloadRange, string videoId, IProgress<int> progress, ITaskLogger logger, ChatFormat format, CancellationToken cancellationToken)
Expand Down Expand Up @@ -471,13 +471,13 @@ private async Task EmbedImages(ChatRoot chatRoot, CancellationToken cancellation

// This is the exact same process as in ChatUpdater.cs but not in a task oriented manner
// TODO: Combine this with ChatUpdater in a different file
List<TwitchEmote> thirdPartyEmotes = await TwitchHelper.GetThirdPartyEmotes(chatRoot.comments, chatRoot.streamer.id, downloadOptions.TempFolder, _progress, bttv: downloadOptions.BttvEmotes, ffz: downloadOptions.FfzEmotes, stv: downloadOptions.StvEmotes, cancellationToken: cancellationToken);
List<TwitchEmote> thirdPartyEmotes = await TwitchHelper.GetThirdPartyEmotes(chatRoot.comments, chatRoot.streamer.id, _cacheDir, _progress, bttv: downloadOptions.BttvEmotes, ffz: downloadOptions.FfzEmotes, stv: downloadOptions.StvEmotes, cancellationToken: cancellationToken);
_progress.ReportProgress(25);
List<TwitchEmote> firstPartyEmotes = await TwitchHelper.GetEmotes(chatRoot.comments, downloadOptions.TempFolder, _progress, cancellationToken: cancellationToken);
List<TwitchEmote> firstPartyEmotes = await TwitchHelper.GetEmotes(chatRoot.comments, _cacheDir, _progress, cancellationToken: cancellationToken);
_progress.ReportProgress(50);
List<ChatBadge> twitchBadges = await TwitchHelper.GetChatBadges(chatRoot.comments, chatRoot.streamer.id, downloadOptions.TempFolder, _progress, cancellationToken: cancellationToken);
List<ChatBadge> twitchBadges = await TwitchHelper.GetChatBadges(chatRoot.comments, chatRoot.streamer.id, _cacheDir, _progress, cancellationToken: cancellationToken);
_progress.ReportProgress(75);
List<CheerEmote> twitchBits = await TwitchHelper.GetBits(chatRoot.comments, downloadOptions.TempFolder, chatRoot.streamer.id.ToString(), _progress, cancellationToken: cancellationToken);
List<CheerEmote> twitchBits = await TwitchHelper.GetBits(chatRoot.comments, _cacheDir, chatRoot.streamer.id.ToString(), _progress, cancellationToken: cancellationToken);
_progress.ReportProgress(100);

_progress.SetTemplateStatus("Embedding Images {0}%", 0);
Expand Down
16 changes: 8 additions & 8 deletions TwitchDownloaderCore/ChatRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using TwitchDownloaderCore.Extensions;
using TwitchDownloaderCore.Interfaces;
using TwitchDownloaderCore.Options;
using TwitchDownloaderCore.Services;
using TwitchDownloaderCore.Tools;
using TwitchDownloaderCore.TwitchObjects;

Expand All @@ -41,6 +42,7 @@ public sealed class ChatRenderer : IDisposable

private readonly ITaskProgress _progress;
private readonly ChatRenderOptions renderOptions;
private readonly string _cacheDir;
private List<ChatBadge> badgeList = new List<ChatBadge>();
private List<TwitchEmote> emoteList = new List<TwitchEmote>();
private List<TwitchEmote> emoteThirdList = new List<TwitchEmote>();
Expand All @@ -57,9 +59,7 @@ public sealed class ChatRenderer : IDisposable
public ChatRenderer(ChatRenderOptions chatRenderOptions, ITaskProgress progress)
{
renderOptions = chatRenderOptions;
renderOptions.TempFolder = Path.Combine(
string.IsNullOrWhiteSpace(renderOptions.TempFolder) ? Path.GetTempPath() : Path.GetFullPath(renderOptions.TempFolder),
"TwitchDownloader");
_cacheDir = CacheDirectoryService.GetCacheDirectory(renderOptions.TempFolder);
renderOptions.BlockArtPreWrapWidth = 29.166 * renderOptions.FontSize - renderOptions.SidePadding * 2;
renderOptions.BlockArtPreWrap = renderOptions.ChatWidth > renderOptions.BlockArtPreWrapWidth;
_progress = progress;
Expand Down Expand Up @@ -1691,7 +1691,7 @@ private async Task<List<ChatBadge>> GetScaledBadges(CancellationToken cancellati
return new List<ChatBadge>();
}

var badgeTask = await TwitchHelper.GetChatBadges(chatRoot.comments, chatRoot.streamer.id, renderOptions.TempFolder, _progress, chatRoot.embeddedData, renderOptions.Offline, cancellationToken);
var badgeTask = await TwitchHelper.GetChatBadges(chatRoot.comments, chatRoot.streamer.id, _cacheDir, _progress, chatRoot.embeddedData, renderOptions.Offline, cancellationToken);

foreach (var badge in badgeTask)
{
Expand All @@ -1710,7 +1710,7 @@ private async Task<List<ChatBadge>> GetScaledBadges(CancellationToken cancellati

private async Task<List<TwitchEmote>> GetScaledEmotes(CancellationToken cancellationToken)
{
var emoteTask = await TwitchHelper.GetEmotes(chatRoot.comments, renderOptions.TempFolder, _progress, chatRoot.embeddedData, renderOptions.Offline, cancellationToken);
var emoteTask = await TwitchHelper.GetEmotes(chatRoot.comments, _cacheDir, _progress, chatRoot.embeddedData, renderOptions.Offline, cancellationToken);

foreach (var emote in emoteTask)
{
Expand All @@ -1729,7 +1729,7 @@ private async Task<List<TwitchEmote>> GetScaledEmotes(CancellationToken cancella

private async Task<List<TwitchEmote>> GetScaledThirdEmotes(CancellationToken cancellationToken)
{
var emoteThirdTask = await TwitchHelper.GetThirdPartyEmotes(chatRoot.comments, chatRoot.streamer.id, renderOptions.TempFolder, _progress, chatRoot.embeddedData, renderOptions.BttvEmotes, renderOptions.FfzEmotes,
var emoteThirdTask = await TwitchHelper.GetThirdPartyEmotes(chatRoot.comments, chatRoot.streamer.id, _cacheDir, _progress, chatRoot.embeddedData, renderOptions.BttvEmotes, renderOptions.FfzEmotes,
renderOptions.StvEmotes, renderOptions.AllowUnlistedEmotes, renderOptions.Offline, cancellationToken);

foreach (var emote in emoteThirdTask)
Expand All @@ -1749,7 +1749,7 @@ private async Task<List<TwitchEmote>> GetScaledThirdEmotes(CancellationToken can

private async Task<List<CheerEmote>> GetScaledBits(CancellationToken cancellationToken)
{
var cheerTask = await TwitchHelper.GetBits(chatRoot.comments, renderOptions.TempFolder, chatRoot.streamer.id.ToString(), _progress, chatRoot.embeddedData, renderOptions.Offline, cancellationToken);
var cheerTask = await TwitchHelper.GetBits(chatRoot.comments, _cacheDir, chatRoot.streamer.id.ToString(), _progress, chatRoot.embeddedData, renderOptions.Offline, cancellationToken);

foreach (var cheer in cheerTask)
{
Expand All @@ -1768,7 +1768,7 @@ private async Task<List<CheerEmote>> GetScaledBits(CancellationToken cancellatio

private async Task<Dictionary<string, SKBitmap>> GetScaledEmojis(CancellationToken cancellationToken)
{
var emojis = await TwitchHelper.GetEmojis(renderOptions.TempFolder, renderOptions.EmojiVendor, _progress, cancellationToken);
var emojis = await TwitchHelper.GetEmojis(_cacheDir, renderOptions.EmojiVendor, _progress, cancellationToken);

//Assume emojis are 4x (they're 72x72)
double emojiScale = 0.5 * renderOptions.ReferenceScale * renderOptions.EmojiScale;
Expand Down
18 changes: 9 additions & 9 deletions TwitchDownloaderCore/ChatUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using TwitchDownloaderCore.Chat;
using TwitchDownloaderCore.Interfaces;
using TwitchDownloaderCore.Options;
using TwitchDownloaderCore.Services;
using TwitchDownloaderCore.Tools;
using TwitchDownloaderCore.TwitchObjects;
using TwitchDownloaderCore.TwitchObjects.Gql;
Expand All @@ -20,14 +21,13 @@ public sealed class ChatUpdater

private readonly ChatUpdateOptions _updateOptions;
private readonly ITaskProgress _progress;
private readonly string _cacheDir;

public ChatUpdater(ChatUpdateOptions updateOptions, ITaskProgress progress)
{
_updateOptions = updateOptions;
_progress = progress;
_updateOptions.TempFolder = Path.Combine(
string.IsNullOrWhiteSpace(_updateOptions.TempFolder) ? Path.GetTempPath() : Path.GetFullPath(_updateOptions.TempFolder),
"TwitchDownloader");
_cacheDir = CacheDirectoryService.GetCacheDirectory(_updateOptions.TempFolder);
}

public async Task UpdateAsync(CancellationToken cancellationToken)
Expand Down Expand Up @@ -273,7 +273,7 @@ private async Task UpdateEmbeds(int currentStep, int totalSteps, CancellationTok

private async Task FirstPartyEmoteTask(CancellationToken cancellationToken = default)
{
List<TwitchEmote> firstPartyEmoteList = await TwitchHelper.GetEmotes(chatRoot.comments, _updateOptions.TempFolder, _progress, _updateOptions.ReplaceEmbeds ? null : chatRoot.embeddedData, cancellationToken: cancellationToken);
List<TwitchEmote> firstPartyEmoteList = await TwitchHelper.GetEmotes(chatRoot.comments, _cacheDir, _progress, _updateOptions.ReplaceEmbeds ? null : chatRoot.embeddedData, cancellationToken: cancellationToken);

int inputCount = chatRoot.embeddedData.firstParty.Count;
chatRoot.embeddedData.firstParty = new List<EmbedEmoteData>();
Expand All @@ -292,7 +292,7 @@ private async Task FirstPartyEmoteTask(CancellationToken cancellationToken = def

private async Task ThirdPartyEmoteTask(CancellationToken cancellationToken = default)
{
List<TwitchEmote> thirdPartyEmoteList = await TwitchHelper.GetThirdPartyEmotes(chatRoot.comments, chatRoot.streamer.id, _updateOptions.TempFolder, _progress, _updateOptions.ReplaceEmbeds ? null : chatRoot.embeddedData, _updateOptions.BttvEmotes, _updateOptions.FfzEmotes, _updateOptions.StvEmotes, cancellationToken: cancellationToken);
List<TwitchEmote> thirdPartyEmoteList = await TwitchHelper.GetThirdPartyEmotes(chatRoot.comments, chatRoot.streamer.id, _cacheDir, _progress, _updateOptions.ReplaceEmbeds ? null : chatRoot.embeddedData, _updateOptions.BttvEmotes, _updateOptions.FfzEmotes, _updateOptions.StvEmotes, cancellationToken: cancellationToken);

int inputCount = chatRoot.embeddedData.thirdParty.Count;
chatRoot.embeddedData.thirdParty = new List<EmbedEmoteData>();
Expand All @@ -313,7 +313,7 @@ private async Task ThirdPartyEmoteTask(CancellationToken cancellationToken = def

private async Task ChatBadgeTask(CancellationToken cancellationToken = default)
{
List<ChatBadge> badgeList = await TwitchHelper.GetChatBadges(chatRoot.comments, chatRoot.streamer.id, _updateOptions.TempFolder, _progress, _updateOptions.ReplaceEmbeds ? null : chatRoot.embeddedData, cancellationToken: cancellationToken);
List<ChatBadge> badgeList = await TwitchHelper.GetChatBadges(chatRoot.comments, chatRoot.streamer.id, _cacheDir, _progress, _updateOptions.ReplaceEmbeds ? null : chatRoot.embeddedData, cancellationToken: cancellationToken);

int inputCount = chatRoot.embeddedData.twitchBadges.Count;
chatRoot.embeddedData.twitchBadges = new List<EmbedChatBadge>();
Expand All @@ -329,7 +329,7 @@ private async Task ChatBadgeTask(CancellationToken cancellationToken = default)

private async Task BitTask(CancellationToken cancellationToken = default)
{
List<CheerEmote> bitList = await TwitchHelper.GetBits(chatRoot.comments, _updateOptions.TempFolder, chatRoot.streamer.id.ToString(), _progress, _updateOptions.ReplaceEmbeds ? null : chatRoot.embeddedData, cancellationToken: cancellationToken);
List<CheerEmote> bitList = await TwitchHelper.GetBits(chatRoot.comments, _cacheDir, chatRoot.streamer.id.ToString(), _progress, _updateOptions.ReplaceEmbeds ? null : chatRoot.embeddedData, cancellationToken: cancellationToken);

int inputCount = chatRoot.embeddedData.twitchBits.Count;
chatRoot.embeddedData.twitchBits = new List<EmbedCheerEmote>();
Expand Down Expand Up @@ -363,7 +363,7 @@ private async Task ChatTrimBeginningTask(CancellationToken cancellationToken)
return;
}

string tempFile = Path.Combine(_updateOptions.TempFolder, Path.GetRandomFileName());
string tempFile = Path.Combine(_cacheDir, Path.GetRandomFileName());

try
{
Expand Down Expand Up @@ -400,7 +400,7 @@ private async Task ChatTrimEndingTask(CancellationToken cancellationToken)
return;
}

string tempFile = Path.Combine(_updateOptions.TempFolder, Path.GetRandomFileName());
string tempFile = Path.Combine(_cacheDir, Path.GetRandomFileName());

try
{
Expand Down
12 changes: 6 additions & 6 deletions TwitchDownloaderCore/ClipDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using TwitchDownloaderCore.Extensions;
using TwitchDownloaderCore.Interfaces;
using TwitchDownloaderCore.Options;
using TwitchDownloaderCore.Services;
using TwitchDownloaderCore.Tools;
using TwitchDownloaderCore.TwitchObjects.Gql;

Expand All @@ -19,14 +20,13 @@ public sealed class ClipDownloader
private readonly ClipDownloadOptions downloadOptions;
private readonly ITaskProgress _progress;
private static readonly HttpClient HttpClient = new();
private readonly string _cacheDir;

public ClipDownloader(ClipDownloadOptions clipDownloadOptions, ITaskProgress progress)
{
downloadOptions = clipDownloadOptions;
_progress = progress;
downloadOptions.TempFolder = Path.Combine(
string.IsNullOrWhiteSpace(downloadOptions.TempFolder) ? Path.GetTempPath() : Path.GetFullPath(downloadOptions.TempFolder),
"TwitchDownloader");
_cacheDir = CacheDirectoryService.GetCacheDirectory(downloadOptions.TempFolder);
}

public async Task DownloadAsync(CancellationToken cancellationToken)
Expand Down Expand Up @@ -68,12 +68,12 @@ private async Task DownloadAsyncImpl(FileInfo outputFileInfo, FileStream outputF
return;
}

if (!Directory.Exists(downloadOptions.TempFolder))
if (!Directory.Exists(_cacheDir))
{
TwitchHelper.CreateDirectory(downloadOptions.TempFolder);
TwitchHelper.CreateDirectory(_cacheDir);
}

var tempFile = Path.Combine(downloadOptions.TempFolder, $"{downloadOptions.Id}_{DateTimeOffset.UtcNow.Ticks}.mp4");
var tempFile = Path.Combine(_cacheDir, $"{downloadOptions.Id}_{DateTimeOffset.UtcNow.Ticks}.mp4");
try
{
await using (var tempFileStream = File.Open(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read))
Expand Down
48 changes: 48 additions & 0 deletions TwitchDownloaderCore/Services/CacheDirectoryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;

namespace TwitchDownloaderCore.Services
{
public static class CacheDirectoryService
{
private const string CACHE_DIRECTORY_SUFFIX = "TwitchDownloader";

public static string GetCacheDirectory([AllowNull] string baseDirectory)
{
if (string.IsNullOrWhiteSpace(baseDirectory))
baseDirectory = Path.GetTempPath();

baseDirectory = Path.GetFullPath(baseDirectory);

if (new DirectoryInfo(baseDirectory).Name == CACHE_DIRECTORY_SUFFIX)
{
return baseDirectory;
}

return Path.Combine(baseDirectory, CACHE_DIRECTORY_SUFFIX);
}

public static bool ClearCacheDirectory([AllowNull] string baseDirectory, out Exception exception)
{
var cacheDirectory = GetCacheDirectory(baseDirectory);
if (!Directory.Exists(cacheDirectory))
{
exception = null;
return true;
}

try
{
Directory.Delete(cacheDirectory, true);
exception = null;
return true;
}
catch (Exception ex)
{
exception = ex;
return false;
}
}
}
}
Loading
Loading