383 lines
15 KiB
C#
383 lines
15 KiB
C#
using System.Net.Mime;
|
|
using DotBased.Monads;
|
|
using Manager.App.Constants;
|
|
using Manager.App.Models.Library;
|
|
using Manager.App.Models.Settings;
|
|
using Manager.App.Models.System;
|
|
using Manager.App.Services.System;
|
|
using Manager.Data.Contexts;
|
|
using Manager.Data.Entities.LibraryContext;
|
|
using Manager.YouTube.Models.Innertube;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Manager.App.Services;
|
|
|
|
public class LibraryService : ILibraryService
|
|
{
|
|
private readonly ILogger<LibraryService> _logger;
|
|
private readonly IDbContextFactory<LibraryDbContext> _dbContextFactory;
|
|
private readonly DirectoryInfo _libraryDirectory;
|
|
private readonly CacheService _cacheService;
|
|
|
|
public LibraryService(ILogger<LibraryService> logger, IOptions<LibrarySettings> librarySettings, IDbContextFactory<LibraryDbContext> contextFactory, CacheService cacheService)
|
|
{
|
|
_logger = logger;
|
|
var librarySettings1 = librarySettings.Value;
|
|
_dbContextFactory = contextFactory;
|
|
_cacheService = cacheService;
|
|
_libraryDirectory = Directory.CreateDirectory(librarySettings1.Path);
|
|
logger.LogDebug("Library directory: {LibraryWorkingDir}", _libraryDirectory.FullName);
|
|
Directory.CreateDirectory(Path.Combine(librarySettings1.Path, LibraryConstants.Directories.SubDirMedia));
|
|
Directory.CreateDirectory(Path.Combine(librarySettings1.Path, LibraryConstants.Directories.SubDirChannels));
|
|
}
|
|
|
|
private async Task AddWebImagesAsync(LibraryDbContext context, List<WebImage> images, string foreignKey, string libSubDir, string fileType, string subDir)
|
|
{
|
|
foreach (var image in images)
|
|
{
|
|
if (context.Files.Any(f => image.Url.Equals(f.OriginalUrl)))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var cacheResult = await _cacheService.CacheFromUrl(image.Url);
|
|
if (!cacheResult.IsSuccess)
|
|
{
|
|
_logger.LogWarning("Failed to get image {ImageUrl}", image.Url);
|
|
continue;
|
|
}
|
|
|
|
var cachedFile = cacheResult.Value;
|
|
|
|
var fileId = Guid.NewGuid();
|
|
var fileName = cachedFile.OriginalFileName ?? $"{fileId}.{cachedFile.ContentType?.Split('/').Last() ?? "unknown"}";
|
|
var relativePath = Path.Combine(foreignKey, libSubDir, $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}_{fileName}");
|
|
var savePath = Path.Combine(_libraryDirectory.FullName, subDir, relativePath);
|
|
Directory.CreateDirectory(Path.GetDirectoryName(savePath) ?? savePath);
|
|
await using var fileStream = File.Create(savePath);
|
|
await fileStream.WriteAsync(cachedFile.Data.AsMemory(0, cachedFile.Data.Length));
|
|
|
|
var file = new FileEntity
|
|
{
|
|
Id = fileId,
|
|
OriginalUrl = image.Url,
|
|
OriginalFileName = cachedFile.OriginalFileName,
|
|
ForeignKey = foreignKey,
|
|
FileType = fileType,
|
|
RelativePath = relativePath.Replace('\\', '/'),
|
|
MimeType = cachedFile.ContentType,
|
|
SizeBytes = cachedFile.Data.Length,
|
|
Height = image.Height,
|
|
Width = image.Width
|
|
};
|
|
|
|
await context.Files.AddAsync(file);
|
|
}
|
|
}
|
|
|
|
public async Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
var updateEntity = false;
|
|
var dbClient = context.ClientAccounts.Include(ca => ca.HttpCookies).FirstOrDefault(c => c.Id == client.Id);
|
|
if (dbClient == null)
|
|
{
|
|
dbClient = client;
|
|
}
|
|
else
|
|
{
|
|
updateEntity = true;
|
|
dbClient.HttpCookies = client.HttpCookies;
|
|
dbClient.UserAgent = client.UserAgent;
|
|
}
|
|
|
|
if (updateEntity)
|
|
{
|
|
context.ClientAccounts.Update(dbClient);
|
|
}
|
|
else
|
|
{
|
|
context.HttpCookies.RemoveRange(context.HttpCookies.Where(x => x.ClientId == client.Id));
|
|
context.ClientAccounts.Add(dbClient);
|
|
}
|
|
|
|
var savedResult= await context.SaveChangesAsync(cancellationToken);
|
|
return savedResult <= 0 ? ResultError.Fail("Could not save changes!") : Result.Success();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return HandleException(e);
|
|
}
|
|
}
|
|
|
|
public async Task<Result<LibraryFile>> GetFileByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
var file = context.Files.FirstOrDefault(f => f.Id == id);
|
|
if (file == null)
|
|
{
|
|
return ResultError.Fail($"File with id {id} not found.");
|
|
}
|
|
|
|
var fs = new FileStream(Path.Combine(_libraryDirectory.FullName, LibraryConstants.Directories.SubDirChannels, file.RelativePath), FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
return new LibraryFile { DataStream = fs, SizeBytes = file.SizeBytes, FileName = file.OriginalFileName ?? file.Id.ToString(), MimeType = file.MimeType ?? MediaTypeNames.Application.Octet };
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return HandleException(e);
|
|
}
|
|
}
|
|
|
|
public async Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(id))
|
|
{
|
|
return ResultError.Fail("Channel id cannot be null or empty!");
|
|
}
|
|
|
|
try
|
|
{
|
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
var channel = await context.Channels
|
|
.Include(c => c.ClientAccount)
|
|
.ThenInclude(p => p!.HttpCookies)
|
|
.Include(f => f.Files)
|
|
.FirstOrDefaultAsync(c => c.Id == id, cancellationToken);
|
|
|
|
if (channel == null)
|
|
{
|
|
return ResultError.Fail("Channel not found!");
|
|
}
|
|
|
|
return channel;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return HandleException(e);
|
|
}
|
|
}
|
|
|
|
public async Task<Result> SaveChannelAsync(InnertubeChannel innertubeChannel, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
var channelResult = await GetChannelByIdAsync(innertubeChannel.Id, cancellationToken);
|
|
|
|
ChannelEntity? channelEntity;
|
|
try
|
|
{
|
|
if (channelResult.IsSuccess)
|
|
{
|
|
channelEntity = channelResult.Value;
|
|
channelEntity.Name = innertubeChannel.ChannelName;
|
|
channelEntity.Handle = innertubeChannel.Handle;
|
|
channelEntity.Description = innertubeChannel.Description;
|
|
}
|
|
else
|
|
{
|
|
channelEntity = new ChannelEntity
|
|
{
|
|
Id = innertubeChannel.Id,
|
|
Name = innertubeChannel.ChannelName,
|
|
Handle = innertubeChannel.Handle,
|
|
Description = innertubeChannel.Description
|
|
};
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return ResultError.Error(e);
|
|
}
|
|
|
|
if (channelResult.IsSuccess)
|
|
{
|
|
context.Channels.Update(channelEntity);
|
|
}
|
|
else
|
|
{
|
|
context.Channels.Add(channelEntity);
|
|
}
|
|
|
|
await AddWebImagesAsync(context, innertubeChannel.AvatarImages, innertubeChannel.Id, "avatars", LibraryConstants.FileTypes.ChannelAvatar, LibraryConstants.Directories.SubDirChannels);
|
|
await AddWebImagesAsync(context, innertubeChannel.BannerImages, innertubeChannel.Id, "banners", LibraryConstants.FileTypes.ChannelBanner, LibraryConstants.Directories.SubDirChannels);
|
|
|
|
var changed = await context.SaveChangesAsync(cancellationToken);
|
|
return changed <= 0 ? ResultError.Fail("Failed to save channel!") : Result.Success();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return HandleException(e);
|
|
}
|
|
}
|
|
|
|
public async Task<Result<LibraryInformation>> GetLibraryInfoAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
var libraryDriveInfo = GetLibraryDriveInfo(_libraryDirectory);
|
|
var libInfo = new LibraryInformation
|
|
{
|
|
LibraryPath = _libraryDirectory.FullName,
|
|
CreatedAtUtc = _libraryDirectory.CreationTimeUtc,
|
|
LastModifiedUtc = _libraryDirectory.LastWriteTimeUtc,
|
|
TotalChannels = await context.Channels.CountAsync(cancellationToken: cancellationToken),
|
|
TotalMedia = await context.Media.CountAsync(cancellationToken: cancellationToken),
|
|
TotalSizeBytes = GetDirectorySize(_libraryDirectory),
|
|
DriveTotalSpaceBytes = libraryDriveInfo.totalSpace,
|
|
DriveFreeSpaceBytes = libraryDriveInfo.freeSpace,
|
|
DriveUsedSpaceBytes = libraryDriveInfo.usedSpace
|
|
};
|
|
return libInfo;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return HandleException(e);
|
|
}
|
|
}
|
|
|
|
public async Task<ListResult<AccountListView>> GetAccountsAsync(string? search, int offset = 0, int total = 20, CancellationToken cancellationToken = default)
|
|
{
|
|
if (total == 0)
|
|
{
|
|
total = 20;
|
|
}
|
|
|
|
try
|
|
{
|
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
var accountsQuery = context.ClientAccounts
|
|
.Include(ca => ca.Channel)
|
|
.Include(ca => ca.HttpCookies)
|
|
.OrderByDescending(ca => ca.Id).AsQueryable();
|
|
var totalAccounts = accountsQuery.Count();
|
|
|
|
if (!string.IsNullOrWhiteSpace(search) && totalAccounts != 0)
|
|
{
|
|
var normalizedSearch = $"%{search.ToLower()}%";
|
|
accountsQuery = accountsQuery
|
|
.Where(ca =>
|
|
EF.Functions.Like(
|
|
(
|
|
ca.Id.ToString() + " " +
|
|
(ca.Channel != null ? ca.Channel.Name : "") + " " +
|
|
(ca.Channel != null ? ca.Channel.Handle : "")
|
|
).ToLower(),
|
|
normalizedSearch
|
|
)
|
|
);
|
|
totalAccounts = accountsQuery.Count();
|
|
}
|
|
|
|
var accountViews = accountsQuery.Skip(offset).Take(total).Select(account => new AccountListView
|
|
{
|
|
Id = account.Id,
|
|
Name = account.Channel != null ? account.Channel.Name : "",
|
|
Handle = account.Channel != null ? account.Channel.Handle : "",
|
|
HasCookies = account.HttpCookies.Count != 0,
|
|
AvatarFileId = account.Files == null ? null
|
|
: account.Files.Where(f => f.FileType == LibraryConstants.FileTypes.ChannelAvatar).OrderBy(x => x.Id).Select(f => f.Id).FirstOrDefault()
|
|
});
|
|
|
|
return new ListResultReturn<AccountListView>(totalAccounts == 0 ? [] : accountViews.ToList(), totalAccounts);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return HandleException(e);
|
|
}
|
|
}
|
|
|
|
public async Task<ListResult<ChannelListView>> GetChannelsAsync(string? search, int offset = 0, int total = 20, CancellationToken cancellationToken = default)
|
|
{
|
|
if (total == 0)
|
|
{
|
|
total = 20;
|
|
}
|
|
|
|
try
|
|
{
|
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
var channelQuery = context.Channels.OrderByDescending(c => c.Id).AsQueryable();
|
|
|
|
var totalChannels = channelQuery.Count();
|
|
if (!string.IsNullOrWhiteSpace(search) && totalChannels != 0)
|
|
{
|
|
var normalizedSearch = $"%{search.ToLower()}%";
|
|
channelQuery = channelQuery
|
|
.Where(ca =>
|
|
EF.Functions.Like((
|
|
ca.Id.ToString() + " " +
|
|
ca.Name + " " +
|
|
ca.Handle
|
|
).ToLower(),
|
|
normalizedSearch
|
|
)
|
|
);
|
|
totalChannels = channelQuery.Count();
|
|
}
|
|
|
|
var channelViews = channelQuery.Skip(offset).Take(total).Select(channel => new ChannelListView
|
|
{
|
|
Id = channel.Id,
|
|
Name = channel.Name,
|
|
Handle = channel.Handle,
|
|
AvatarFileId = channel.Files == null ? null
|
|
: channel.Files.Where(f => f.FileType == LibraryConstants.FileTypes.ChannelAvatar).OrderBy(x => x.Id).Select(f => f.Id).FirstOrDefault()
|
|
});
|
|
|
|
return new ListResultReturn<ChannelListView>(totalChannels == 0 ? [] : channelViews.ToList(), totalChannels);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return HandleException(e);
|
|
}
|
|
}
|
|
|
|
private long GetDirectorySize(DirectoryInfo dir)
|
|
{
|
|
try
|
|
{
|
|
var size = dir.EnumerateFiles("*", SearchOption.AllDirectories).Select(f => f.Length).Sum();
|
|
return size;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "Error while getting directory size.");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private (long totalSpace, long freeSpace, long usedSpace) GetLibraryDriveInfo(DirectoryInfo dir)
|
|
{
|
|
try
|
|
{
|
|
var drive = new DriveInfo(dir.FullName);
|
|
return (drive.TotalSize, drive.AvailableFreeSpace, drive.TotalSize - drive.AvailableFreeSpace);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "Error while getting directory free space.");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private ResultError HandleException(Exception exception)
|
|
{
|
|
if (exception is OperationCanceledException)
|
|
{
|
|
return ResultError.Fail("Library service operation cancelled");
|
|
}
|
|
|
|
_logger.LogError(exception, "Service error");
|
|
return ResultError.Error(exception);
|
|
}
|
|
} |