Skip to content

Commit

Permalink
feat(storage): Make the RAStreamWithContentType a stream wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Oct 9, 2020
1 parent 1b363e3 commit 789ae3f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/Uno.UWP/Storage/Streams/RandomAccessStreamReference.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable

using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -25,17 +26,17 @@ public static RandomAccessStreamReference CreateFromUri(Uri uri)
public static RandomAccessStreamReference CreateFromStream(IRandomAccessStream stream)
=> new RandomAccessStreamReference(async ct =>
{
return new RandomAccessStreamWithContentType(stream.CloneStream());
return stream.TrySetContentType();
});

private readonly Func<IAsyncOperation<IRandomAccessStreamWithContentType>> _open;

private RandomAccessStreamReference(Func<IAsyncOperation<IRandomAccessStreamWithContentType>> open)
internal RandomAccessStreamReference(Func<IAsyncOperation<IRandomAccessStreamWithContentType>> open)
{
_open = open;
}

public RandomAccessStreamReference(Func<CancellationToken, Task<IRandomAccessStreamWithContentType>> open)
internal RandomAccessStreamReference(Func<CancellationToken, Task<IRandomAccessStreamWithContentType>> open)
: this(() => AsyncOperation.FromTask(open))
{
}
Expand Down
24 changes: 22 additions & 2 deletions src/Uno.UWP/Storage/Streams/RandomAccessStreamWithContentType.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
#nullable enable

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.Foundation;

namespace Windows.Storage.Streams
{
internal class RandomAccessStreamWithContentType : IRandomAccessStreamWithContentType
internal class RandomAccessStreamWithContentType : IRandomAccessStreamWithContentType, IStreamWrapper, IRandomStreamWrapper
{
public const string DefaultContentType = "application/octet-stream";

private readonly IRandomAccessStream _stream;

public RandomAccessStreamWithContentType(IRandomAccessStream stream, string contentType = "application/octet-stream")
/// <summary>
/// -- DO NOT USE -- Prefer to use the System.IO.WindowsRuntimeStreamExtensions.TrySetContentType
/// The only valid use-case is when you explicitly know that the provided stream does not implement IRandomAccessStreamWithContentType
/// </summary>
public RandomAccessStreamWithContentType(IRandomAccessStream stream, string contentType = DefaultContentType)
{
Debug.Assert(!(stream is IRandomAccessStreamWithContentType));

ContentType = contentType;
_stream = stream;
}

public RandomAccessStreamWithContentType(Stream stream, string contentType = DefaultContentType)
: this(new RandomAccessStreamOverStream(stream), contentType)
{
}

Stream? IStreamWrapper.FindStream() => (_stream as IStreamWrapper)?.FindStream();
IRandomAccessStream? IRandomStreamWrapper.FindStream() => (_stream as IRandomStreamWrapper)?.FindStream() ?? _stream;
IInputStream? IInputStreamWrapper.FindStream() => (_stream as IInputStreamWrapper)?.FindStream() ?? _stream;
IOutputStream? IOutputStreamWrapper.FindStream() => (_stream as IOutputStreamWrapper)?.FindStream() ?? _stream;

/// <inheritdoc />
public string ContentType { get; }

Expand Down

0 comments on commit 789ae3f

Please sign in to comment.