Skip to content

Commit

Permalink
Merge pull request #109 from richardschneider/108-raw-leaves
Browse files Browse the repository at this point in the history
fix: listing raw and cms files
  • Loading branch information
richardschneider authored Jun 1, 2019
2 parents 73231de + f3c5591 commit c8c1842
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ string text = await ipfs.FileSystem.ReadAllTextAsync(filename);

- [IPFS Core](https://github.com/richardschneider/net-ipfs-core)
- [IPFS HTTP Client](https://github.com/richardschneider/net-ipfs-http-client)
- [IPFS HTTP Gateway](https://github.com/richardschneider/net-ipfs-http-gateway)
- [Peer Talk](https://github.com/richardschneider/peer-talk)

## Sponsors
Expand Down
41 changes: 36 additions & 5 deletions src/CoreApi/FileSystemApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,35 @@ async Task<FileSystemNode> CreateDirectoryAsync (IEnumerable<IFileSystemLink> li
{
var cid = await ipfs.ResolveIpfsPathToCidAsync(path, cancel).ConfigureAwait(false);
var block = await ipfs.Block.GetAsync(cid, cancel).ConfigureAwait(false);

// TODO: A content-type registry should be used.
if (cid.ContentType == "dag-pb")
{
// fall thru
}
else if (cid.ContentType == "raw")
{
return new FileSystemNode
{
Id = cid,
Size = block.Size
};
}
else if (cid.ContentType == "cms")
{
return new FileSystemNode
{
Id = cid,
Size = block.Size
};
}
else
{
throw new NotSupportedException($"Cannot read content type '{cid.ContentType}'.");
}

var dag = new DagNode(block.DataStream);
var dm = Serializer.Deserialize<DataMessage>(dag.DataStream);

var fsn = new FileSystemNode
{
Id = cid,
Expand All @@ -189,7 +215,7 @@ async Task<FileSystemNode> CreateDirectoryAsync (IEnumerable<IFileSystemLink> li
})
.ToArray(),
IsDirectory = dm.Type == DataType.Directory,
Size = (long) (dm.FileSize ?? 0)
Size = (long)(dm.FileSize ?? 0)
};

// Cannot determine if a link points to a directory. The link's block must be
Expand Down Expand Up @@ -247,9 +273,14 @@ async Task<FileSystemNode> CreateDirectoryAsync (IEnumerable<IFileSystemLink> li
async Task AddTarNodeAsync(Cid cid, string name, TarOutputStream tar, CancellationToken cancel)
{
var block = await ipfs.Block.GetAsync(cid, cancel).ConfigureAwait(false);
var dag = new DagNode(block.DataStream);
var dm = Serializer.Deserialize<DataMessage>(dag.DataStream);
var dm = new DataMessage { Type = DataType.Raw };
DagNode dag = null;

if (cid.ContentType == "dag-pb")
{
dag = new DagNode(block.DataStream);
dm = Serializer.Deserialize<DataMessage>(dag.DataStream);
}
var entry = new TarEntry(new TarHeader());
var header = entry.TarHeader;
header.Mode = 0x1ff; // 777 in octal
Expand Down Expand Up @@ -281,7 +312,7 @@ async Task AddTarNodeAsync(Cid cid, string name, TarOutputStream tar, Cancellati
tar.CloseEntry();
}

// TODO: recurse over files and subdirectories
// Recurse over files and subdirectories
if (dm.Type == DataType.Directory)
{
foreach (var link in dag.Links)
Expand Down
53 changes: 53 additions & 0 deletions test/CoreApi/FileSystemApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,43 @@ public async Task GetTar()
}
}

[TestMethod]
public async Task GetTar_RawLeaves()
{
var ipfs = TestFixture.Ipfs;
var temp = MakeTemp();
try
{
var options = new AddFileOptions
{
RawLeaves = true
};
var dir = ipfs.FileSystem.AddDirectoryAsync(temp, true, options).Result;
var dirid = dir.Id.Encode();

var tar = await ipfs.FileSystem.GetAsync(dir.Id);
var archive = TarArchive.CreateInputTarArchive(tar);
var files = new List<string>();
archive.ProgressMessageEvent += (a, e, m) =>
{
files.Add(e.Name);
};
archive.ListContents();

Assert.AreEqual($"{dirid}", files[0]);
Assert.AreEqual($"{dirid}/alpha.txt", files[1]);
Assert.AreEqual($"{dirid}/beta.txt", files[2]);
Assert.AreEqual($"{dirid}/x", files[3]);
Assert.AreEqual($"{dirid}/x/x.txt", files[4]);
Assert.AreEqual($"{dirid}/x/y", files[5]);
Assert.AreEqual($"{dirid}/x/y/y.txt", files[6]);
}
finally
{
Directory.Delete(temp, true);
}
}

[TestMethod]
public async Task GetTar_EmptyDirectory()
{
Expand All @@ -792,6 +829,22 @@ public async Task GetTar_EmptyDirectory()
}
#endif

[TestMethod]
public async Task Isssue108()
{
var ipfs = TestFixture.Ipfs;
var options = new AddFileOptions
{
Hash = "blake2b-256",
RawLeaves = true
};
var node = await ipfs.FileSystem.AddTextAsync("hello world", options);
var other = await ipfs.FileSystem.ListFileAsync(node.Id);
Assert.AreEqual(node.Id, other.Id);
Assert.AreEqual(node.IsDirectory, other.IsDirectory);
Assert.AreEqual(node.Size, other.Size);
}

public static string MakeTemp()
{
var temp = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Expand Down

0 comments on commit c8c1842

Please sign in to comment.