Files
duplicati/Duplicati/Server/WebServer/RESTMethods/ServerSetting.cs
T
Kenneth Hsu 0c1ac9c489 Replace chained LINQ calls with call to overload with predicate.
This simplifies the code by reducing the number of enumerators created
while also making the code slightly more readable.
2018-10-06 16:20:18 -07:00

112 lines
4.7 KiB
C#

// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using System.Linq;
namespace Duplicati.Server.WebServer.RESTMethods
{
public class ServerSetting : IRESTMethodGET, IRESTMethodPUT, IRESTMethodDocumented
{
public void GET(string key, RequestInfo info)
{
if (string.IsNullOrWhiteSpace(key))
{
info.OutputError(null, System.Net.HttpStatusCode.BadRequest, "Key is missing");
return;
}
if (key.Equals("server-ssl-certificate", StringComparison.OrdinalIgnoreCase) || key.Equals("ServerSSLCertificate", StringComparison.OrdinalIgnoreCase))
{
info.OutputOK(Program.DataConnection.ApplicationSettings.ServerSSLCertificate == null ? "False" : "True");
return;
}
if (key.StartsWith("--", StringComparison.Ordinal))
{
var prop = Program.DataConnection.Settings.FirstOrDefault(x => string.Equals(key, x.Name, StringComparison.OrdinalIgnoreCase));
info.OutputOK(prop == null ? null : prop.Value);
}
else
{
var prop = typeof(Database.ServerSettings).GetProperty(key);
if (prop == null)
info.OutputError(null, System.Net.HttpStatusCode.NotFound, "Not found");
else
info.OutputOK(prop.GetValue(Program.DataConnection.ApplicationSettings));
}
}
public void PUT(string key, RequestInfo info)
{
if (string.IsNullOrWhiteSpace(key))
{
info.OutputError(null, System.Net.HttpStatusCode.BadRequest, "Key is missing");
return;
}
if (key.Equals("server-ssl-certificate", StringComparison.OrdinalIgnoreCase) || key.Equals("ServerSSLCertificate", StringComparison.OrdinalIgnoreCase))
{
info.OutputError(null, System.Net.HttpStatusCode.BadRequest, "Can only update SSL certificate from commandline");
return;
}
if (key.StartsWith("--", StringComparison.Ordinal))
{
var settings = Program.DataConnection.Settings.ToList();
var prop = settings.FirstOrDefault(x => string.Equals(key, x.Name, StringComparison.OrdinalIgnoreCase));
if (prop == null)
settings.Add(prop = new Database.Setting() { Name = key, Value = info.Request.Form["data"].Value });
else
prop.Value = info.Request.Form["data"].Value;
Program.DataConnection.Settings = settings.ToArray();
info.OutputOK(prop == null ? null : prop.Value);
}
else
{
var prop = typeof(Database.ServerSettings).GetProperty(key);
if (prop == null)
info.OutputError(null, System.Net.HttpStatusCode.NotFound, "Not found");
else
{
var dict = new Dictionary<string, string>();
dict[key] = info.Request.Form["data"].Value;
Program.DataConnection.ApplicationSettings.UpdateSettings(dict, false);
info.OutputOK();
}
}
}
public string Description { get { return "Return a list of settings for the server"; } }
public IEnumerable<KeyValuePair<string, Type>> Types
{
get
{
return new KeyValuePair<string, Type>[] {
new KeyValuePair<string, Type>(HttpServer.Method.Get, typeof(string)),
new KeyValuePair<string, Type>(HttpServer.Method.Put, typeof(string))
};
}
}
}
}