Skip to content
Draft
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
3 changes: 3 additions & 0 deletions server/RdtClient.Data/Enums/DownloadClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ public enum DownloadClient

[Description("Synology DownloadStation")]
DownloadStation,

[Description("Strm Downloader")]
Strm
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using Moq;
using RdtClient.Service.Services.Downloaders;

namespace RdtClient.Service.Test.Services.Downloaders;

public class StrmDownloaderTest
{
[Fact]
public async Task Download_WhenFileCreatedSuccesfully_RaisesDownloadCompleteEvent()
{
var mockFileSystem = new MockFileSystem();
var strmDownloader = new StrmDownloader("http://example.com/video.mp4", "video.mp4", mockFileSystem);

await Assert.RaisesAsync<DownloadCompleteEventArgs>(a => strmDownloader.DownloadComplete += a,
a => strmDownloader.DownloadComplete -= a,
strmDownloader.Download);

Assert.True(mockFileSystem.FileExists("video.mp4.strm"));
Assert.Equal(await mockFileSystem.File.ReadAllTextAsync("video.mp4.strm"), "http://example.com/video.mp4");

Check warning on line 21 in server/RdtClient.Service.Test/Services/Downloaders/StrmDownloaderTest.cs

View workflow job for this annotation

GitHub Actions / build

The literal or constant value "http://example.com/video.mp4" should be passed as the 'expected' argument in the call to 'Assert.Equal(expected, actual)' in method 'Download_WhenFileCreatedSuccesfully_RaisesDownloadCompleteEvent' on type 'StrmDownloaderTest'. Swap the parameter values. (https://xunit.net/xunit.analyzers/rules/xUnit2000)

Check warning on line 21 in server/RdtClient.Service.Test/Services/Downloaders/StrmDownloaderTest.cs

View workflow job for this annotation

GitHub Actions / build

The literal or constant value "http://example.com/video.mp4" should be passed as the 'expected' argument in the call to 'Assert.Equal(expected, actual)' in method 'Download_WhenFileCreatedSuccesfully_RaisesDownloadCompleteEvent' on type 'StrmDownloaderTest'. Swap the parameter values. (https://xunit.net/xunit.analyzers/rules/xUnit2000)
}

[Fact]
public async Task Download_WhenErrorCreatingFile_RaisesDownloadCompleteEventWithError()
{
// Arrange
var mockFileSystem = new Mock<IFileSystem>();
mockFileSystem.Setup(fs => fs.File.WriteAllTextAsync(It.IsAny<String>(), It.IsAny<String>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new IOException("File write error"));

var strmDownloader = new StrmDownloader("http://example.com/video.mp4", "video.mp4", mockFileSystem.Object);

// Act & Assert
var raisedEvent = await Assert.RaisesAsync<DownloadCompleteEventArgs>(
a => strmDownloader.DownloadComplete += a,
a => strmDownloader.DownloadComplete -= a,
strmDownloader.Download);

Assert.NotNull(raisedEvent);
Assert.False(String.IsNullOrWhiteSpace(raisedEvent.Arguments.Error));
}
}
6 changes: 4 additions & 2 deletions server/RdtClient.Service/Services/DownloadClient.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using RdtClient.Data.Enums;
using System.IO.Abstractions;
using RdtClient.Data.Enums;
using RdtClient.Data.Models.Data;
using RdtClient.Service.Helpers;
using RdtClient.Service.Services.Downloaders;
using RdtClient.Service.Services.TorrentClients;

namespace RdtClient.Service.Services;

public class DownloadClient(Download download, Torrent torrent, String destinationPath, String? category)
public class DownloadClient(Download download, Torrent torrent, String destinationPath, String? category, IFileSystem fileSystem)
{
private static Int64 _totalBytesDownloadedThisSession;
private static readonly Lock TotalBytesDownloadedLock = new();
Expand Down Expand Up @@ -70,6 +71,7 @@ public async Task<String> Start()
Data.Enums.DownloadClient.Aria2c => new Aria2cDownloader(download.RemoteId, download.Link, filePath, downloadPath, category),
Data.Enums.DownloadClient.Symlink => new SymlinkDownloader(download.Link, filePath, downloadPath, torrent.ClientKind),
Data.Enums.DownloadClient.DownloadStation => await DownloadStationDownloader.Init(download.RemoteId, download.Link, filePath, downloadPath, category),
Data.Enums.DownloadClient.Strm => new StrmDownloader(download.Link, filePath, fileSystem),
_ => throw new($"Unknown download client {Type}")
};

Expand Down
44 changes: 44 additions & 0 deletions server/RdtClient.Service/Services/Downloaders/StrmDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.IO.Abstractions;

namespace RdtClient.Service.Services.Downloaders;

public class StrmDownloader(String downloadLink, String filePath, IFileSystem fileSystem) : IDownloader
{
public event EventHandler<DownloadCompleteEventArgs>? DownloadComplete;
public event EventHandler<DownloadProgressEventArgs>? DownloadProgress;

Check warning on line 8 in server/RdtClient.Service/Services/Downloaders/StrmDownloader.cs

View workflow job for this annotation

GitHub Actions / build

The event 'StrmDownloader.DownloadProgress' is never used

Check warning on line 8 in server/RdtClient.Service/Services/Downloaders/StrmDownloader.cs

View workflow job for this annotation

GitHub Actions / build

The event 'StrmDownloader.DownloadProgress' is never used

public async Task<String> Download()
{
try
{
await fileSystem.File.WriteAllTextAsync(filePath + ".strm", downloadLink);

DownloadComplete?.Invoke(this, new());
}
catch (Exception ex)
{
DownloadComplete?.Invoke(this,
new()
{
Error = ex.Message
});
}

return Guid.NewGuid().ToString();
}

public Task Cancel()
{
return Task.CompletedTask;
}

public Task Pause()
{
return Task.CompletedTask;
}

public Task Resume()
{
return Task.CompletedTask;
}
}
5 changes: 3 additions & 2 deletions server/RdtClient.Service/Services/TorrentRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
using RdtClient.Service.Services.Downloaders;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO.Abstractions;
using System.Text.Json;

namespace RdtClient.Service.Services;

public class TorrentRunner(ILogger<TorrentRunner> logger, Torrents torrents, Downloads downloads)
public class TorrentRunner(ILogger<TorrentRunner> logger, Torrents torrents, Downloads downloads, IFileSystem fileSystem)
{
public static readonly ConcurrentDictionary<Guid, DownloadClient> ActiveDownloadClients = new();
public static readonly ConcurrentDictionary<Guid, UnpackClient> ActiveUnpackClients = new();
Expand Down Expand Up @@ -428,7 +429,7 @@ public async Task Tick()
Log($"Setting download path to {downloadPath}", download, torrent);

// Start the download process
var downloadClient = new DownloadClient(download, torrent, downloadPath, torrent.Category);
var downloadClient = new DownloadClient(download, torrent, downloadPath, torrent.Category, fileSystem);

if (ActiveDownloadClients.TryAdd(download.DownloadId, downloadClient))
{
Expand Down
5 changes: 3 additions & 2 deletions server/RdtClient.Web/Controllers/SettingsController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.IO.Abstractions;
using System.Reflection;
using Aria2NET;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -14,7 +15,7 @@ namespace RdtClient.Web.Controllers;

[Authorize(Policy = "AuthSetting")]
[Route("Api/Settings")]
public class SettingsController(Settings settings, Torrents torrents) : Controller
public class SettingsController(Settings settings, Torrents torrents, IFileSystem fileSystem) : Controller
{
[HttpGet]
[Route("")]
Expand Down Expand Up @@ -108,7 +109,7 @@ public async Task<ActionResult> TestDownloadSpeed(CancellationToken cancellation
}
};

var downloadClient = new DownloadClient(download, download.Torrent, downloadPath, null);
var downloadClient = new DownloadClient(download, download.Torrent, downloadPath, null, fileSystem);

await downloadClient.Start();

Expand Down