cloudconvert / cloudconvert-dotnet

CloudConvert .NET SDK
Other
20 stars 15 forks source link

Added Stream to UploadAsync #20

Closed GerardSmit closed 1 year ago

GerardSmit commented 1 year ago

This PR adds the following method to ICloudConvertAPI:

interface ICloudConvertAPI
{
    Task<string> UploadAsync(string url, Stream file, string fileName, object parameters);
}

Currently it's only possible to send a byte-array. Because of this, you have to load the file into memory before sending it to CloudConvert. This is fine for small files but loading large files could be a memory problem.

It's noteworthy that the stream doesn't get disposed by the API, so for example:

var stream = File.Open("file.png", FileMode.Open, FileAccess.Read, FileShare.Read);

await cloudConvertApi.UploadAsync(url, stream, "file.png", parameters);

In this example the stream is still open. You'll have to Dispose the stream yourself (or use using var stream = ...).

Tests I've added an internal constructor in RestHelper and made the internals visible to the Tests-project. This is so I can mock the HTTP-client and check if the stream doesn't get disposed by CloudConvertAPI.

GerardSmit commented 1 year ago

Should I remove the integration test for stream? It seems like having 3 target frameworks and 2 separate tests for byte-array and stream is too much (6 API requests in total). The unit test fails with:

{"message":"You have started too many jobs at once","code":"PARALLEL_JOB_LIMIT_EXCEEDED"}
josiasmontag commented 1 year ago

I just cleaned up that queue and tests went through now.

Looks great, thanks a lot! Could you also update the README here to use a stream instead of a byte array? (including Dispose)

GerardSmit commented 1 year ago

Done. The preview can be viewed at https://github.com/GerardSmit/cloudconvert-dotnet/tree/feature/upload-stream#uploading-files.