/
ServerSettingsLogsController.cs
118 lines (108 loc) · 4.07 KB
/
ServerSettingsLogsController.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace Dnn.PersonaBar.Servers.Services
{
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Dnn.PersonaBar.Library;
using Dnn.PersonaBar.Library.Attributes;
using Dnn.PersonaBar.Servers.Components.Log;
using DotNetNuke.Common;
using DotNetNuke.Data;
using DotNetNuke.Instrumentation;
[MenuPermission(Scope = ServiceScope.Host)]
public class ServerSettingsLogsController : PersonaBarApiController
{
private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(ServerSettingsLogsController));
private readonly LogController _logController = new LogController();
[HttpGet]
public HttpResponseMessage GetLogs()
{
try
{
var response = new
{
Success = true,
Results = new
{
LogList = this._logController.GetLogFilesList(),
UpgradeLogList = this._logController.GetUpgradeLogList(),
},
TotalResults = 1,
};
return this.Request.CreateResponse(HttpStatusCode.OK, response);
}
catch (Exception exc)
{
Logger.Error(exc);
return this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc);
}
}
[HttpGet]
public HttpResponseMessage GetLogFile(string fileName)
{
try
{
var cleanedFileName = Path.GetFileName(fileName);
var logFilePath = Path.Combine(Globals.ApplicationMapPath, @"portals\_default\logs", cleanedFileName);
return this.CreateLogFileResponse(logFilePath);
}
catch (ArgumentException exc)
{
return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, exc.Message);
}
catch (Exception exc)
{
Logger.Error(exc);
return this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc);
}
}
[HttpGet]
public HttpResponseMessage GetUpgradeLogFile(string logName)
{
try
{
var providerPath = DataProvider.Instance().GetProviderPath();
var cleanedLogName = Path.GetFileName(logName);
var logFilePath = Path.Combine(providerPath, cleanedLogName);
return this.CreateLogFileResponse(logFilePath);
}
catch (ArgumentException exc)
{
return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, exc.Message);
}
catch (Exception exc)
{
Logger.Error(exc);
return this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc);
}
}
[NonAction]
private static void ValidateFilePath(string physicalPath)
{
var fileInfo = new FileInfo(physicalPath);
if (!fileInfo.DirectoryName.StartsWith(Globals.ApplicationMapPath, StringComparison.InvariantCultureIgnoreCase))
{
throw new ArgumentException("Invalid File Path");
}
}
[NonAction]
private HttpResponseMessage CreateLogFileResponse(string logFilePath)
{
ValidateFilePath(logFilePath);
if (!File.Exists(logFilePath))
{
return this.Request.CreateResponse(HttpStatusCode.NotFound);
}
using (var reader = File.OpenText(logFilePath))
{
var logText = reader.ReadToEnd();
return this.Request.CreateResponse(HttpStatusCode.OK, logText);
}
}
}
}