-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExcelResource.cs
135 lines (121 loc) · 4.75 KB
/
ExcelResource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.IO;
namespace DynamicPDF.Api
{
/// <summary>
/// Represents a excel resource.
/// </summary>
public class ExcelResource : Resource
{
/// <summary>
/// Initializes a new instance of the <see cref="ExcelResource"/> class.
/// </summary>
/// <param name="filePath">The excel file path.</param>
/// <param name="resourceName">The resource name with file extension.</param>
public ExcelResource(string filePath, string resourceName = null) : base(filePath, resourceName)
{
if (string.IsNullOrWhiteSpace(resourceName) == false)
{
SetMimeType();
}
else if (resourceName != null)
{
throw new EndpointException("Unsupported file type or invalid file extension.");
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ExcelResource"/> class.
/// </summary>
/// <param name="value">The byte array of the excel file.</param>
/// <param name="resourceName">The resource name with file extension.</param>
public ExcelResource(byte[] value, string resourceName) : base(value, resourceName)
{
if (string.IsNullOrWhiteSpace(resourceName) == false)
{
SetMimeType();
}
else if (resourceName != null)
{
throw new EndpointException("Unsupported file type or invalid file extension.");
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ExcelResource"/> class.
/// </summary>
/// <param name="data">The stream of the excel file.</param>
/// <param name="resourceName">The resource name with file extension.</param>
public ExcelResource(Stream data, string resourceName) : base(data, resourceName)
{
if (string.IsNullOrWhiteSpace(resourceName) == false)
{
SetMimeType();
}
else if (resourceName != null)
{
throw new EndpointException("Unsupported file type or invalid file extension.");
}
}
/// <summary>
/// Sets the resource name.
/// </summary>
/// <remarks>The resource name should be specified with a file extension.</remarks>
public override string ResourceName
{
set
{
if (string.IsNullOrWhiteSpace(value) == true || Path.HasExtension(value) == false)
throw new EndpointException("Invalid resource name.");
base.ResourceName = value;
SetMimeType();
}
}
[JsonProperty("type")]
[JsonConverter(typeof(StringEnumConverter), converterParameters: typeof(CamelCaseNamingStrategy))]
internal override ResourceType Type { get; } = ResourceType.Excel;
internal override string MimeType { get; set; }
internal override string FileExtension
{
get
{
string inputFileExtension = "";
if (string.IsNullOrWhiteSpace(ResourceName) == false)
{
if (Path.HasExtension(ResourceName.Trim()) == true)
inputFileExtension = Path.GetExtension(ResourceName).ToLower();
else
throw new EndpointException("Invalid resource name.");
}
else if (string.IsNullOrWhiteSpace(FilePath) == false)
{
if (Path.HasExtension(FilePath.Trim()) == true)
inputFileExtension = Path.GetExtension(FilePath).ToLower();
else
throw new EndpointException("Invalid file path specified.");
}
else
throw new EndpointException("Invalid file path or resource name.");
if (inputFileExtension == ".xls")
{
MimeType = "application/vnd.ms-excel";
return ".xls";
}
else if (inputFileExtension == ".xlsx" && Data[0] == 0x50 && Data[1] == 0x4b)
{
MimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
return ".xlsx";
}
else
{
throw new EndpointException("Unsupported file type or invalid file extension.");
}
}
}
private void SetMimeType()
{
_ = FileExtension;
}
}
}