Skip to content

Commit

Permalink
add - doc - Added a way to get the blob stream
Browse files Browse the repository at this point in the history
---

We've added a method that allows you to get a stream from a blob. Doesn't support URLs or non-BASE64 blobs.

---

Type: add
Breaking: False
Doc Required: True
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Jul 1, 2024
1 parent 8586e8d commit bfd08a5
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions VisualCard.ShowContacts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Terminaux.Colors.Data;
using Terminaux.Writer.ConsoleWriters;
Expand Down
23 changes: 23 additions & 0 deletions VisualCard/Parsers/VcardParserTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,5 +351,28 @@ internal static bool IsEncodingBlob(string[] args, string keyEncoded)
encoding.Equals("BASE64", StringComparison.OrdinalIgnoreCase) ||
encoding.Equals("BLOB", StringComparison.OrdinalIgnoreCase);
}

internal static Stream GetBlobData(string[] args, string keyEncoded)
{
if (IsEncodingBlob(args, keyEncoded))
{
bool isValidUri = Uri.TryCreate(keyEncoded, UriKind.Absolute, out Uri uri);
string dataStr;
if (isValidUri)
{
if (uri.Scheme == "data")
dataStr = uri.AbsolutePath.Substring(uri.AbsolutePath.IndexOf(",") + 1);
else
throw new InvalidDataException("Contains a valid URL; you should fetch that URL manually and convert the response to the stream.");
}
else
dataStr = keyEncoded;
byte[] dataBytes = Convert.FromBase64String(dataStr);
Stream blobStream = new MemoryStream(dataBytes);
return blobStream;
}
else
throw new InvalidOperationException("Not a blob. You should somehow handle it.");
}
}
}
7 changes: 7 additions & 0 deletions VisualCard/Parts/Implementations/KeyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
return _key;
}

/// <summary>
/// Gets a stream representing the key data
/// </summary>
/// <returns>A stream that contains key data</returns>
public Stream GetStream() =>
VcardParserTools.GetBlobData(Arguments, KeyEncoded);

/// <inheritdoc/>
public override bool Equals(object obj) =>
Equals((KeyInfo)obj);
Expand Down
7 changes: 7 additions & 0 deletions VisualCard/Parts/Implementations/LogoInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
return _logo;
}

/// <summary>
/// Gets a stream representing the logo data
/// </summary>
/// <returns>A stream that contains logo data</returns>
public Stream GetStream() =>
VcardParserTools.GetBlobData(Arguments, LogoEncoded);

/// <inheritdoc/>
public override bool Equals(object obj) =>
Equals((LogoInfo)obj);
Expand Down
7 changes: 7 additions & 0 deletions VisualCard/Parts/Implementations/PhotoInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
return _photo;
}

/// <summary>
/// Gets a stream representing the image data
/// </summary>
/// <returns>A stream that contains image data</returns>
public Stream GetStream() =>
VcardParserTools.GetBlobData(Arguments, PhotoEncoded);

/// <inheritdoc/>
public override bool Equals(object obj) =>
Equals((PhotoInfo)obj);
Expand Down
7 changes: 7 additions & 0 deletions VisualCard/Parts/Implementations/SoundInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
return _sound;
}

/// <summary>
/// Gets a stream representing the sound data
/// </summary>
/// <returns>A stream that contains sound data</returns>
public Stream GetStream() =>
VcardParserTools.GetBlobData(Arguments, SoundEncoded);

/// <inheritdoc/>
public override bool Equals(object obj) =>
Equals((SoundInfo)obj);
Expand Down

0 comments on commit bfd08a5

Please sign in to comment.