Skip to content

Commit

Permalink
Merge pull request #51 from AssemblyAI/niels/add-upload-bytearray-memory
Browse files Browse the repository at this point in the history
Add overloads to upload files using byte array and memory
  • Loading branch information
Swimburger committed Sep 4, 2024
2 parents a140e1e + 2ced6d3 commit f96ba1d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/AssemblyAI.IntegrationTests/FilesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,36 @@ public async Task Should_Upload_File_Using_Stream_With_Dispose()
Assert.That(uploadedFile.UploadUrl, Is.Not.Null);
Assert.Throws<ObjectDisposedException>(() => fileStream.ReadByte());
}

[Test]
public async Task Should_Upload_File_Using_ByteArray()
{
// Assuming there's a method to create a configured RawClient instance
var client = Helpers.CreateClient();

// Adjust the path to where your test file is located
var testFilePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "nbc.mp3");
var fileMemory = File.ReadAllBytes(testFilePath);

var uploadedFile = await client.Files.UploadAsync(fileMemory).ConfigureAwait(false);

Assert.That(uploadedFile, Is.Not.Null);
Assert.That(uploadedFile.UploadUrl, Is.Not.Null);
}

[Test]
public async Task Should_Upload_File_Using_Memory()
{
// Assuming there's a method to create a configured RawClient instance
var client = Helpers.CreateClient();

// Adjust the path to where your test file is located
var testFilePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "nbc.mp3");
var fileMemory = new ReadOnlyMemory<byte>(File.ReadAllBytes(testFilePath));

var uploadedFile = await client.Files.UploadAsync(fileMemory).ConfigureAwait(false);

Assert.That(uploadedFile, Is.Not.Null);
Assert.That(uploadedFile.UploadUrl, Is.Not.Null);
}
}
48 changes: 48 additions & 0 deletions src/AssemblyAI/Files/ExtendedFilesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,54 @@ public async Task<UploadedFile> UploadAsync(
await using (stream)
#else
using (stream)
#endif
{
return await UploadAsync(stream, options, cancellationToken).ConfigureAwait(false);
}
}

/// <summary>
/// Upload a media file to AssemblyAI's servers.
/// </summary>
/// <param name="audioFile">The audio file to upload</param>
/// <param name="options">The HTTP request options</param>
/// <param name="cancellationToken"></param>
/// <returns>File uploaded to AssemblyAI</returns>
public async Task<UploadedFile> UploadAsync(
ReadOnlyMemory<byte> audioFile,
RequestOptions? options = null,
CancellationToken cancellationToken = default
)
{
var stream = new MemoryStream(audioFile.ToArray());
#if NET6_0_OR_GREATER
await using (stream)
#else
using (stream)
#endif
{
return await UploadAsync(stream, options, cancellationToken).ConfigureAwait(false);
}
}

/// <summary>
/// Upload a media file to AssemblyAI's servers.
/// </summary>
/// <param name="audioFile">The audio file to upload</param>
/// <param name="options">The HTTP request options</param>
/// <param name="cancellationToken"></param>
/// <returns>File uploaded to AssemblyAI</returns>
public async Task<UploadedFile> UploadAsync(
byte[] audioFile,
RequestOptions? options = null,
CancellationToken cancellationToken = default
)
{
var stream = new MemoryStream(audioFile);
#if NET6_0_OR_GREATER
await using (stream)
#else
using (stream)
#endif
{
return await UploadAsync(stream, options, cancellationToken).ConfigureAwait(false);
Expand Down

0 comments on commit f96ba1d

Please sign in to comment.