Skip to content

Commit

Permalink
add simple data chunk production
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Jul 14, 2024
1 parent 8ab6b96 commit 01faeb6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
41 changes: 39 additions & 2 deletions src/BeeNet.Util/Services/CalculatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public async Task<UploadEvaluationResult> EvaluateFileUploadAsync(
postageStampIssuer);
}

public async Task<IReadOnlyDictionary<string, string>> GetResourceMetadataFromChunksAsync(
public async Task<IReadOnlyDictionary<string, string>> GetFileMetadataFromChunksAsync(
string chunkStoreDirectory,
SwarmAddress address)
{
Expand All @@ -213,7 +213,7 @@ public async Task<IReadOnlyDictionary<string, string>> GetResourceMetadataFromCh
return await rootManifest.GetResourceMetadataAsync(address).ConfigureAwait(false);
}

public async Task<Stream> GetResourceStreamFromChunksAsync(
public async Task<Stream> GetFileStreamFromChunksAsync(
string chunkStoreDirectory,
SwarmAddress address)
{
Expand All @@ -233,5 +233,42 @@ public async Task<Stream> GetResourceStreamFromChunksAsync(

return memoryStream;
}

public Task<SwarmHash> WriteDataChunksAsync(
byte[] data,
string outputDirectory,
bool createDirectory = true,
bool encrypt = false,
RedundancyLevel redundancyLevel = RedundancyLevel.None)
{
using var stream = new MemoryStream(data);
return WriteDataChunksAsync(
stream,
outputDirectory,
createDirectory,
encrypt,
redundancyLevel);
}

public async Task<SwarmHash> WriteDataChunksAsync(
Stream stream,
string outputDirectory,
bool createDirectory = true,
bool encrypt = false,
RedundancyLevel redundancyLevel = RedundancyLevel.None)
{
var chunkStore = new LocalDirectoryChunkStore(outputDirectory, createDirectory);

// Create chunks and get file hash.
using var fileHasherPipeline = HasherPipelineBuilder.BuildNewHasherPipeline(
chunkStore,
new FakePostageStamper(),
redundancyLevel,
encrypt);
var fileHash = await fileHasherPipeline.HashDataAsync(stream).ConfigureAwait(false);

// Return file hash.
return fileHash;
}
}
}
36 changes: 34 additions & 2 deletions src/BeeNet.Util/Services/ICalculatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Task<UploadEvaluationResult> EvaluateFileUploadAsync(
/// <param name="chunkStoreDirectory">The chunk directory</param>
/// <param name="address">Resource address</param>
/// <returns>Resource metadata</returns>
Task<IReadOnlyDictionary<string, string>> GetResourceMetadataFromChunksAsync(
Task<IReadOnlyDictionary<string, string>> GetFileMetadataFromChunksAsync(
string chunkStoreDirectory,
SwarmAddress address);

Expand All @@ -99,8 +99,40 @@ Task<IReadOnlyDictionary<string, string>> GetResourceMetadataFromChunksAsync(
/// <param name="chunkStoreDirectory">The chunk directory</param>
/// <param name="address">Resource address</param>
/// <returns>Resource stream</returns>
Task<Stream> GetResourceStreamFromChunksAsync(
Task<Stream> GetFileStreamFromChunksAsync(
string chunkStoreDirectory,
SwarmAddress address);

/// <summary>
/// Write data chunks on a local directory, without any manifest
/// </summary>
/// <param name="data">The data byte array input</param>
/// <param name="outputDirectory">The output directory path</param>
/// <param name="createDirectory">If true, create if directory doesn't exist</param>
/// <param name="encrypt">True to encrypt</param>
/// <param name="redundancyLevel">Choose the redundancy level</param>
/// <returns>The data root hash</returns>
Task<SwarmHash> WriteDataChunksAsync(
byte[] data,
string outputDirectory,
bool createDirectory = true,
bool encrypt = false,
RedundancyLevel redundancyLevel = RedundancyLevel.None);

/// <summary>
/// Write data chunks on a local directory, without any manifest
/// </summary>
/// <param name="stream">The data stream input</param>
/// <param name="outputDirectory">The output directory path</param>
/// <param name="createDirectory">If true, create if directory doesn't exist</param>
/// <param name="encrypt">True to encrypt</param>
/// <param name="redundancyLevel">Choose the redundancy level</param>
/// <returns>The data root hash</returns>
Task<SwarmHash> WriteDataChunksAsync(
Stream stream,
string outputDirectory,
bool createDirectory = true,
bool encrypt = false,
RedundancyLevel redundancyLevel = RedundancyLevel.None);
}
}

0 comments on commit 01faeb6

Please sign in to comment.