using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace EasyDevCore.Remote.HttpAccess
{
///
/// Represents HTTP content based on a local file. Typically used with PostMultipartAsync for uploading files.
///
public class FileContent : HttpContent
{
///
/// The local file path.
///
public string Path { get; }
private readonly int _BufferSize;
///
/// Initializes a new instance of the class.
///
/// The local file path.
/// The buffer size of the stream upload in bytes. Defaults to 4096.
public FileContent(string path, int bufferSize = 4096)
{
Path = path;
_BufferSize = bufferSize;
}
///
/// Serializes to stream asynchronous.
///
/// The stream.
/// The context.
///
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
using(var fs = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read, _BufferSize, useAsync: true))
{
await fs.CopyToAsync(stream, _BufferSize).ConfigureAwait(false);
}
}
///
/// Tries the length of the compute.
///
/// The length.
///
protected override bool TryComputeLength(out long length)
{
length = -1;
return false;
}
}
}