-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomFileStorage.cs
83 lines (76 loc) · 3.07 KB
/
CustomFileStorage.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
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using DevExpress.DashboardWeb;
using DevExpress.DashboardCommon;
namespace WebApplication18 {
public class CustomFileStorage : IEditableDashboardStorage {
public string WorkingDirectory { get; set; }
protected virtual DirectoryInfo Directory
{
get
{
string absolutePath = Path.IsPathRooted(WorkingDirectory) ? WorkingDirectory : HttpContext.Current.Server.MapPath(WorkingDirectory);
return new DirectoryInfo(absolutePath);
}
}
public CustomFileStorage(string workingDirectory) {
WorkingDirectory = workingDirectory;
}
Dashboard CreateIntance(XDocument dashboard) {
Dashboard instance = null;
try {
instance = new Dashboard();
instance.LoadFromXDocument(dashboard);
return instance;
} catch {
if (instance != null)
instance.Dispose();
return null;
}
}
protected virtual string ResolveFileName(string dashboardID) {
var dashboardFileName = string.Format("{0}.xml", dashboardID);
dashboardFileName = Path.GetInvalidFileNameChars().Aggregate(dashboardFileName, (current, c) => current.Replace(c.ToString(), string.Empty));
return Path.Combine(Directory.FullName, dashboardFileName);
}
public IEnumerable<DashboardInfo> GetAvailableDashboardsInfo() {
IEnumerable<string> dashboardsID = Directory.GetFiles().Select(file => Path.GetFileNameWithoutExtension(file.Name)).ToList();
return dashboardsID.Select(id => {
string name = null;
using (Dashboard instance = CreateIntance(LoadDashboard(id))) {
if (instance != null)
name = instance.Title.Text;
}
return new DashboardInfo {
ID = id,
Name = name
};
});
}
public XDocument LoadDashboard(string dashboardID) {
var fileName = ResolveFileName(dashboardID);
if (File.Exists(fileName))
return XDocument.Load(fileName);
return null;
}
public void SaveDashboard(string dashboardID, XDocument dashboard) {
var fileName = ResolveFileName(dashboardID);
dashboard.Save(fileName);
}
public string AddDashboard(XDocument dashboard, string dashboardName) {
using (Dashboard instance = CreateIntance(dashboard)) {
if (instance != null) {
instance.Title.Text = dashboardName;
dashboard = instance.SaveToXDocument();
var fileName = ResolveFileName(dashboardName);
dashboard.Save(fileName);
}
}
return dashboardName;
}
}
}