-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
154 additions
and
14 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
ResourceMonitor/.idea/.idea.ResourceMonitor/.idea/contentModel.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
ResourceMonitor/ResourceMonitor.Test/TransmissionDownloaderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using ResourceMonitor.Services.Implementation; | ||
|
||
namespace ResourceMonitor.Test | ||
{ | ||
[TestClass] | ||
public class TransmissionDownloaderTests | ||
{ | ||
IConfigurationRoot configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(new Dictionary<string, string> | ||
{ | ||
{"Transmission:Url", "http://localhost:9091/transmission/rpc"}, | ||
{"Transmission:Login", "admin"}, | ||
{"Transmission:Password", "admin"} | ||
}) | ||
.Build(); | ||
|
||
[TestMethod] | ||
public async Task TryConnect() | ||
{ | ||
var downloader = new TransmissionDownloader(configuration, new NullLogger<TransmissionDownloader>()); | ||
Assert.IsTrue(await downloader.TryConnect()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetAllTasks() | ||
{ | ||
var downloader = new TransmissionDownloader(configuration, new NullLogger<TransmissionDownloader>()); | ||
var tasks = await downloader.GetAllTasks(); | ||
foreach (var task in tasks) | ||
{ | ||
Debug.WriteLine(task.ToString()); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public async Task AddNewTorrentTask() | ||
{ | ||
var downloader = new TransmissionDownloader(configuration, new NullLogger<TransmissionDownloader>()); | ||
var torrentBytes = File.ReadAllBytes("valid.torrent"); | ||
await downloader.AddNewTorrentTask(torrentBytes); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
ResourceMonitor/ResourceMonitor/Services/Implementation/TransmissionDownloader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using ResourceMonitor.Models.Downloader; | ||
using ResourceMonitor.Services.Declaration; | ||
using Transmission.API.RPC; | ||
using Transmission.API.RPC.Entity; | ||
|
||
namespace ResourceMonitor.Services.Implementation | ||
{ | ||
public class TransmissionDownloader : IDownloader | ||
{ | ||
private readonly ILogger<TransmissionDownloader> _logger; | ||
private readonly Client _client; | ||
|
||
|
||
public TransmissionDownloader(IConfiguration configuration, ILogger<TransmissionDownloader> logger) | ||
{ | ||
_logger = logger; | ||
_client = new Client( | ||
configuration["Transmission:Url"], | ||
Guid.NewGuid().ToString("N"), | ||
configuration["Transmission:Login"], | ||
configuration["Transmission:Password"]); | ||
} | ||
|
||
public async Task<bool> TryConnect() | ||
{ | ||
_logger.LogInformation("尝试连接到 Transmission"); | ||
try | ||
{ | ||
var info = await _client.GetSessionInformationAsync(); | ||
_logger.LogInformation($"连接到 Transmission 成功。版本 {info.Version}。RPC版本 {info.RpcVersion}。"); | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogWarning(ex, "连接到 Transmission 失败"); | ||
return false; | ||
} | ||
} | ||
|
||
public async Task<List<DownloaderTask>> GetAllTasks() | ||
{ | ||
_logger.LogDebug("获取 Transmission 所有任务的状态"); | ||
var tasks = await _client.TorrentGetAsync(TorrentFields.ALL_FIELDS); | ||
return tasks?.Torrents?.Select(t => new DownloaderTask | ||
{ | ||
Id = t.HashString, | ||
Magnet = t.MagnetLink, | ||
Name = t.Name | ||
}).ToList() ?? new List<DownloaderTask>(0); | ||
} | ||
|
||
public async Task<bool> IfTaskExists(string id, string url) | ||
{ | ||
var allTasks = await GetAllTasks(); | ||
return allTasks.Any(t => t.Id.Equals(id, StringComparison.InvariantCultureIgnoreCase)); | ||
} | ||
|
||
public async Task AddNewTorrentTask(byte[] torrentBytes) | ||
{ | ||
var newTorrentInfo = await _client.TorrentAddAsync(new NewTorrent | ||
{ | ||
Metainfo = Convert.ToBase64String(torrentBytes), | ||
Paused = false | ||
}); | ||
if (newTorrentInfo == null || newTorrentInfo.ID == 0) | ||
{ | ||
throw new Exception("在 Transmission 上建立任务失败"); | ||
} | ||
|
||
_logger.LogInformation($"在 Transmission 上建立任务成功。ID={newTorrentInfo.ID}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters