This commit is contained in:
Tyrrrz 2023-07-03 18:35:28 +03:00
parent 768fb88c8c
commit c69211797f
3 changed files with 65 additions and 77 deletions

View file

@ -48,9 +48,9 @@ public static class ExportWrapper
var fileName = channelId.ToString() + '.' + format.GetFileExtension(); var fileName = channelId.ToString() + '.' + format.GetFileExtension();
var filePath = Path.Combine(DirPath, fileName); var filePath = Path.Combine(DirPath, fileName);
// Lock separately for each channel and format using var _ = await Locker.LockAsync(filePath);
using (await Locker.LockAsync(filePath)) using var console = new FakeConsole();
{
// Perform the export only if it hasn't been done before // Perform the export only if it hasn't been done before
if (!File.Exists(filePath)) if (!File.Exists(filePath))
{ {
@ -60,12 +60,11 @@ public static class ExportWrapper
ChannelIds = new[] { channelId }, ChannelIds = new[] { channelId },
ExportFormat = format, ExportFormat = format,
OutputPath = filePath OutputPath = filePath
}.ExecuteAsync(new FakeConsole()); }.ExecuteAsync(console);
} }
return await File.ReadAllTextAsync(filePath); return await File.ReadAllTextAsync(filePath);
} }
}
public static async ValueTask<IHtmlDocument> ExportAsHtmlAsync(Snowflake channelId) => Html.Parse( public static async ValueTask<IHtmlDocument> ExportAsHtmlAsync(Snowflake channelId) => Html.Parse(
await ExportAsync(channelId, ExportFormat.HtmlDark) await ExportAsync(channelId, ExportFormat.HtmlDark)

View file

@ -38,8 +38,8 @@ internal partial class ExportAssetDownloader
var fileName = GetFileNameFromUrl(url); var fileName = GetFileNameFromUrl(url);
var filePath = Path.Combine(_workingDirPath, fileName); var filePath = Path.Combine(_workingDirPath, fileName);
using (await Locker.LockAsync(filePath, cancellationToken)) using var _ = await Locker.LockAsync(filePath, cancellationToken);
{
if (_pathCache.TryGetValue(url, out var cachedFilePath)) if (_pathCache.TryGetValue(url, out var cachedFilePath))
return cachedFilePath; return cachedFilePath;
@ -83,7 +83,6 @@ internal partial class ExportAssetDownloader
return _pathCache[url] = filePath; return _pathCache[url] = filePath;
} }
} }
}
internal partial class ExportAssetDownloader internal partial class ExportAssetDownloader
{ {

View file

@ -89,27 +89,17 @@ internal class ExportContext
public Role? TryGetRole(Snowflake id) => _roles.GetValueOrDefault(id); public Role? TryGetRole(Snowflake id) => _roles.GetValueOrDefault(id);
public Color? TryGetUserColor(Snowflake id) public IReadOnlyList<Role> GetUserRoles(Snowflake id) => TryGetMember(id)?
{
var memberRoles = GetUserRoles(id);
return memberRoles?
.Where(r => r.Color is not null)
.Select(r => r.Color)
.FirstOrDefault();
}
public IReadOnlyList<Role> GetUserRoles(Snowflake id)
{
var member = TryGetMember(id);
return member?
.RoleIds .RoleIds
.Select(TryGetRole) .Select(TryGetRole)
.WhereNotNull() .WhereNotNull()
.OrderByDescending(r => r.Position) .OrderByDescending(r => r.Position)
.ToArray() ?? Array.Empty<Role>(); .ToArray() ?? Array.Empty<Role>();
}
public Color? TryGetUserColor(Snowflake id) => GetUserRoles(id)
.Where(r => r.Color is not null)
.Select(r => r.Color)
.FirstOrDefault();
public async ValueTask<string> ResolveAssetUrlAsync(string url, CancellationToken cancellationToken = default) public async ValueTask<string> ResolveAssetUrlAsync(string url, CancellationToken cancellationToken = default)
{ {
@ -122,7 +112,7 @@ internal class ExportContext
var relativeFilePath = Path.GetRelativePath(Request.OutputDirPath, filePath); var relativeFilePath = Path.GetRelativePath(Request.OutputDirPath, filePath);
// Prefer relative paths so that the output files can be copied around without breaking references. // Prefer relative paths so that the output files can be copied around without breaking references.
// If the assets path is outside of the export directory, use an absolute path instead. // If the asset directory is outside of the export directory, use an absolute path instead.
var optimalFilePath = var optimalFilePath =
relativeFilePath.StartsWith(".." + Path.DirectorySeparatorChar, StringComparison.Ordinal) || relativeFilePath.StartsWith(".." + Path.DirectorySeparatorChar, StringComparison.Ordinal) ||
relativeFilePath.StartsWith(".." + Path.AltDirectorySeparatorChar, StringComparison.Ordinal) relativeFilePath.StartsWith(".." + Path.AltDirectorySeparatorChar, StringComparison.Ordinal)