-
Notifications
You must be signed in to change notification settings - Fork 3
/
CustomReportStorageWebExtension.cs
84 lines (79 loc) · 4.08 KB
/
CustomReportStorageWebExtension.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
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Web.Extensions;
using Microsoft.AspNetCore.Hosting;
using PassParameterExample.PredefinedReports;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
namespace PassParameterExample.Services {
public class CustomReportStorageWebExtension : ReportStorageWebExtension {
readonly string ReportDirectory;
const string FileExtension = ".repx";
public CustomReportStorageWebExtension(IWebHostEnvironment env) {
ReportDirectory = Path.Combine(env.ContentRootPath, "Reports");
if (!Directory.Exists(ReportDirectory)) {
Directory.CreateDirectory(ReportDirectory);
}
}
private bool IsWithinReportsFolder(string url, string folder) {
var rootDirectory = new DirectoryInfo(folder);
var fileInfo = new FileInfo(Path.Combine(folder, url));
return fileInfo.Directory.FullName.ToLower().StartsWith(rootDirectory.FullName.ToLower());
}
public override bool CanSetData(string url) { return true; }
public override bool IsValidUrl(string url) { return Path.GetFileName(url) == url; }
public override byte[] GetData(string url) {
try {
string[] parts = url.Split("?");
string reportName = parts[0];
string parametersString = parts.Length > 1 ? parts[1] : String.Empty;
XtraReport report = null;
if (Directory.EnumerateFiles(ReportDirectory).
Select(Path.GetFileNameWithoutExtension).Contains(reportName)) {
byte[] reportBytes = File.ReadAllBytes(
Path.Combine(ReportDirectory, reportName + FileExtension));
using (MemoryStream ms = new MemoryStream(reportBytes))
report = XtraReport.FromStream(ms);
}
if (ReportsFactory.Reports.ContainsKey(reportName)) {
report = ReportsFactory.Reports[reportName]();
}
if (report != null) {
// Assign parameters here
var parameters = HttpUtility.ParseQueryString(parametersString);
foreach (string parameterName in parameters.AllKeys) {
report.Parameters[parameterName].Value = Convert.ChangeType(
parameters.Get(parameterName), report.Parameters[parameterName].Type);
}
report.RequestParameters = false;
using (MemoryStream ms = new MemoryStream()) {
report.SaveLayoutToXml(ms);
return ms.ToArray();
}
}
} catch (Exception ex) {
throw new DevExpress.XtraReports.Web.ClientControls.FaultException(
"Could not get report data.", ex);
}
throw new DevExpress.XtraReports.Web.ClientControls.FaultException(
string.Format("Could not find report '{0}'.", url));
}
public override Dictionary<string, string> GetUrls() {
return Directory.GetFiles(ReportDirectory, "*" + FileExtension)
.Select(Path.GetFileNameWithoutExtension)
.Union(ReportsFactory.Reports.Select(x => x.Key))
.ToDictionary<string, string>(x => x);
}
public override void SetData(XtraReport report, string url) {
if (!IsWithinReportsFolder(url, ReportDirectory))
throw new DevExpress.XtraReports.Web.ClientControls.FaultException("Invalid report name.");
report.SaveLayoutToXml(Path.Combine(ReportDirectory, url + FileExtension));
}
public override string SetNewData(XtraReport report, string defaultUrl) {
SetData(report, defaultUrl);
return defaultUrl;
}
}
}