Skip to content

Commit be2671c

Browse files
committed
Refactor MfsStream class to use Path property instead of private field
1 parent 8bca0a5 commit be2671c

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/MfsStream.cs

+11-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace OwlCore.Kubo;
1111
/// </summary>
1212
public class MfsStream : Stream
1313
{
14-
private readonly string _path;
1514
private long _length;
1615

1716
/// <summary>
@@ -22,7 +21,7 @@ public class MfsStream : Stream
2221
/// <param name="client">The client to use for interacting with IPFS.</param>
2322
public MfsStream(string path, long length, ICoreApi client)
2423
{
25-
_path = path;
24+
Path = path;
2625
_length = length;
2726
Client = client;
2827
}
@@ -31,6 +30,11 @@ public MfsStream(string path, long length, ICoreApi client)
3130
/// The IPFS Client to use for retrieving the content.
3231
/// </summary>
3332
public ICoreApi Client { get; }
33+
34+
/// <summary>
35+
/// The MFS path to the file. Relative to the root of MFS.
36+
/// </summary>
37+
public string Path { get; }
3438

3539
/// <inheritdoc/>
3640
public override bool CanRead => true;
@@ -53,13 +57,13 @@ public MfsStream(string path, long length, ICoreApi client)
5357
/// <inheritdoc/>
5458
public override void Flush()
5559
{
56-
_ = Client.Mfs.FlushAsync(_path).Result;
60+
_ = Client.Mfs.FlushAsync(Path).Result;
5761
}
5862

5963
/// <inheritdoc/>
6064
public override Task FlushAsync(CancellationToken cancellationToken)
6165
{
62-
return Client.Mfs.FlushAsync(_path, cancellationToken);
66+
return Client.Mfs.FlushAsync(Path, cancellationToken);
6367
}
6468

6569
/// <inheritdoc/>
@@ -71,10 +75,9 @@ public override int Read(byte[] buffer, int offset, int count)
7175
/// <inheritdoc/>
7276
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
7377
{
74-
Guard.IsLessThanOrEqualTo(offset + count, Length);
7578
Guard.IsGreaterThanOrEqualTo(offset, 0);
7679

77-
var result = await Client.Mfs.ReadFileStreamAsync(_path, offset: Position + offset, count: count, cancellationToken);
80+
var result = await Client.Mfs.ReadFileStreamAsync(Path, offset: Position, count: count, cancellationToken);
7881
var bytes = await result.ToBytesAsync(cancellationToken);
7982

8083
for (var i = 0; i < bytes.Length; i++)
@@ -150,13 +153,13 @@ public override async Task WriteAsync(byte[] buffer, int offset, int count, Canc
150153
SetLength(Position + count);
151154
}
152155

153-
await Client.Mfs.WriteAsync(_path, buffer, new() { Offset = Position, Count = count, Create = true }, cancellationToken);
156+
await Client.Mfs.WriteAsync(Path, buffer, new() { Offset = Position, Count = count, Create = true }, cancellationToken);
154157
Position += count;
155158
}
156159

157160
static string GetFileName(string path)
158161
{
159-
var dirName = Path.GetDirectoryName(path);
162+
var dirName = System.IO.Path.GetDirectoryName(path);
160163
return path.Replace('/', '\\').Replace(dirName ?? string.Empty, string.Empty).Trim('/').Trim('\\');
161164
}
162165
}

0 commit comments

Comments
 (0)