-
Notifications
You must be signed in to change notification settings - Fork 49
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
1 parent
7afa69d
commit e384b79
Showing
11 changed files
with
267 additions
and
29 deletions.
There are no files selected for viewing
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,84 @@ | ||
using Ipfs.Engine; | ||
using McMaster.Extensions.CommandLineUtils; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Ipfs.Cli | ||
{ | ||
[Command(Description = "Manage the IPFS repository")] | ||
[Subcommand("gc", typeof(RepoGCCommand))] | ||
[Subcommand("stat", typeof(RepoStatCommand))] | ||
[Subcommand("verify", typeof(RepoVerifyCommand))] | ||
[Subcommand("version", typeof(RepoVersionCommand))] | ||
class RepoCommand : CommandBase | ||
{ | ||
public Program Parent { get; set; } | ||
|
||
protected override Task<int> OnExecute(CommandLineApplication app) | ||
{ | ||
app.ShowHelp(); | ||
return Task.FromResult(0); | ||
} | ||
} | ||
|
||
[Command(Description = "Perform a garbage collection sweep on the repo")] | ||
class RepoGCCommand : CommandBase | ||
{ | ||
RepoCommand Parent { get; set; } | ||
|
||
protected override async Task<int> OnExecute(CommandLineApplication app) | ||
{ | ||
var Program = Parent.Parent; | ||
|
||
await Program.CoreApi.BlockRepository.RemoveGarbageAsync(); | ||
return 0; | ||
} | ||
} | ||
|
||
[Command(Description = "Verify all blocks in repo are not corrupted")] | ||
class RepoVerifyCommand : CommandBase | ||
{ | ||
RepoCommand Parent { get; set; } | ||
|
||
protected override async Task<int> OnExecute(CommandLineApplication app) | ||
{ | ||
var Program = Parent.Parent; | ||
|
||
await Program.CoreApi.BlockRepository.VerifyAsync(); | ||
return 0; | ||
} | ||
} | ||
|
||
[Command(Description = "Repository information")] | ||
class RepoStatCommand : CommandBase | ||
{ | ||
RepoCommand Parent { get; set; } | ||
|
||
protected override async Task<int> OnExecute(CommandLineApplication app) | ||
{ | ||
var Program = Parent.Parent; | ||
|
||
var stats = await Program.CoreApi.BlockRepository.StatisticsAsync(); | ||
return Program.Output(app, stats, null); | ||
} | ||
} | ||
|
||
[Command(Description = "Repository version")] | ||
class RepoVersionCommand : CommandBase | ||
{ | ||
RepoCommand Parent { get; set; } | ||
|
||
protected override async Task<int> OnExecute(CommandLineApplication app) | ||
{ | ||
var Program = Parent.Parent; | ||
|
||
var stats = await Program.CoreApi.BlockRepository.VersionAsync(); | ||
return Program.Output(app, stats, null); | ||
} | ||
} | ||
|
||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Ipfs.CoreApi; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Http; | ||
using System.IO; | ||
|
||
namespace Ipfs.Server.HttpApi.V0 | ||
{ | ||
/// <summary> | ||
/// A wrapped version number. | ||
/// </summary> | ||
public class VersionBlockRepositoryDto | ||
{ | ||
/// <summary> | ||
/// The version number. | ||
/// </summary> | ||
public string Version; | ||
} | ||
|
||
/// <summary> | ||
/// Manages all the blocks in teh repository. | ||
/// </summary> | ||
public class BlockRepositoryController : IpfsController | ||
{ | ||
/// <summary> | ||
/// Creates a new controller. | ||
/// </summary> | ||
public BlockRepositoryController(ICoreApi ipfs) : base(ipfs) { } | ||
|
||
/// <summary> | ||
/// Garbage collection. | ||
/// </summary> | ||
[HttpGet, HttpPost, Route("repo/gc")] | ||
public Task GarbageCollection() | ||
{ | ||
return IpfsCore.BlockRepository.RemoveGarbageAsync(Cancel); | ||
} | ||
|
||
/// <summary> | ||
/// Get repository information. | ||
/// </summary> | ||
[HttpGet, HttpPost, Route("repo/stat")] | ||
public Task<RepositoryData> Statistics() | ||
{ | ||
return IpfsCore.BlockRepository.StatisticsAsync(Cancel); | ||
} | ||
|
||
/// <summary> | ||
/// Verify that the blocks are not corrupt. | ||
/// </summary> | ||
[HttpGet, HttpPost, Route("repo/verify")] | ||
public Task Verify() | ||
{ | ||
return IpfsCore.BlockRepository.VerifyAsync(Cancel); | ||
} | ||
|
||
/// <summary> | ||
/// Get repository information. | ||
/// </summary> | ||
[HttpGet, HttpPost, Route("repo/version")] | ||
public async Task<VersionBlockRepositoryDto> Version() | ||
{ | ||
return new VersionBlockRepositoryDto | ||
{ | ||
Version = await IpfsCore.BlockRepository.VersionAsync(Cancel) | ||
}; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Ipfs.CoreApi; | ||
using Newtonsoft.Json.Linq; | ||
using System.Linq; | ||
using System.Collections.Concurrent; | ||
using PeerTalk; | ||
|
||
namespace Ipfs.Engine.CoreApi | ||
{ | ||
class BlockRepositoryApi : IBlockRepositoryApi | ||
{ | ||
IpfsEngine ipfs; | ||
|
||
public BlockRepositoryApi(IpfsEngine ipfs) | ||
{ | ||
this.ipfs = ipfs; | ||
} | ||
|
||
public Task RemoveGarbageAsync(CancellationToken cancel = default(CancellationToken)) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<RepositoryData> StatisticsAsync(CancellationToken cancel = default(CancellationToken)) | ||
{ | ||
var data = new RepositoryData | ||
{ | ||
RepoPath = Path.GetFullPath(ipfs.Options.Repository.Folder), | ||
Version = "1", | ||
StorageMax = 10000000000 // TODO: there is no storage max | ||
}; | ||
|
||
GetDirStats(data.RepoPath, data, cancel); | ||
|
||
return Task.FromResult(data); | ||
} | ||
|
||
public Task VerifyAsync(CancellationToken cancel = default(CancellationToken)) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public async Task<string> VersionAsync(CancellationToken cancel = default(CancellationToken)) | ||
{ | ||
var stats = await StatisticsAsync(cancel).ConfigureAwait(false); | ||
return stats.Version; | ||
} | ||
|
||
void GetDirStats(string path, RepositoryData data, CancellationToken cancel) | ||
{ | ||
foreach (var file in Directory.EnumerateFiles(path)) | ||
{ | ||
cancel.ThrowIfCancellationRequested(); | ||
++data.NumObjects; | ||
data.RepoSize += (ulong)(new FileInfo(file).Length); | ||
} | ||
|
||
foreach (var dir in Directory.EnumerateDirectories(path)) | ||
{ | ||
cancel.ThrowIfCancellationRequested(); | ||
GetDirStats(dir, data, cancel); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Ipfs.Engine | ||
{ | ||
|
||
[TestClass] | ||
public class BlockRepositoryApiTest | ||
{ | ||
IpfsEngine ipfs = TestFixture.Ipfs; | ||
|
||
[TestMethod] | ||
public void Exists() | ||
{ | ||
Assert.IsNotNull(ipfs.BlockRepository); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Stats() | ||
{ | ||
var stats = await ipfs.BlockRepository.StatisticsAsync(); | ||
var version = await ipfs.BlockRepository.VersionAsync(); | ||
Assert.AreEqual(stats.Version, version); | ||
} | ||
|
||
} | ||
} |