Skip to content

Commit

Permalink
Create VariableValue from file
Browse files Browse the repository at this point in the history
  • Loading branch information
jlucansky committed Feb 28, 2019
1 parent 452608f commit 62c9bf7
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Camunda.Api.Client/VariableValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;

namespace Camunda.Api.Client
Expand Down Expand Up @@ -226,6 +227,55 @@ public static VariableValue FromObject(object value)
return val;
}

/// <summary>
/// Value instantiation. Creates file variable from <paramref name="content"/>
/// </summary>
/// <param name="mimeType">Use <see cref="MediaTypes"/></param>
/// <returns></returns>
public static VariableValue FromFile(string path, string mimeType, string charset = null)
{
return FromFile(File.ReadAllBytes(path), Path.GetFileName(path), mimeType, charset);
}

/// <summary>
/// Value instantiation. Creates file variable from <paramref name="content"/>
/// </summary>
/// <param name="mimeType">Use <see cref="MediaTypes"/></param>
/// <returns></returns>
public static VariableValue FromFile(byte[] content, string fileName, string mimeType, string charset = null)
{
var val = new VariableValue()
{
Type = VariableType.File,
Value = Convert.ToBase64String(content),
ValueInfo = new Dictionary<string, object>()
{
[ValueInfoFileName] = fileName,
[ValueInfoFileMimeType] = mimeType,
[ValueInfoFileEncoding] = charset ?? "",
}
};

return val;
}

/// <summary>
/// Value instantiation. Creates text file variable.
/// </summary>
public static VariableValue FromTextFile(string path, string charset = "utf-8")
{
return FromFile(File.ReadAllBytes(path), Path.GetFileName(path), MediaTypes.Text.Plain, charset);
}

/// <summary>
/// Value instantiation. Creates binary file variable.
/// </summary>
/// <param name="mimeType">Use <see cref="MediaTypes"/></param>
public static VariableValue FromBinaryFile(string path, string mimeType = MediaTypes.Application.OctetStream)
{
return FromFile(File.ReadAllBytes(path), Path.GetFileName(path), mimeType, null);
}

/// <summary>
/// Convert <see cref="Value"/> to desired type.
/// </summary>
Expand Down

0 comments on commit 62c9bf7

Please sign in to comment.