diff --git a/Sunfish/Sunfish/ApiMethod.cs b/Sunfish/Sunfish/ApiMethod.cs new file mode 100644 index 0000000..4c97583 --- /dev/null +++ b/Sunfish/Sunfish/ApiMethod.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2 +{ + [AttributeUsage(AttributeTargets.Method)] + public class ApiMethod : Attribute + { + public ApiMethod(string name) + { + Name = name; + } + + public string Name { get; } + } +} diff --git a/Sunfish/Sunfish/DefineConfigurator.cs b/Sunfish/Sunfish/DefineConfigurator.cs index 1966ae4..2a86774 100644 --- a/Sunfish/Sunfish/DefineConfigurator.cs +++ b/Sunfish/Sunfish/DefineConfigurator.cs @@ -4,7 +4,7 @@ namespace DolphinWebXplorer2 { [AttributeUsage(AttributeTargets.Class)] - class DefineConfigurator : Attribute + public class DefineConfigurator : Attribute { public DefineConfigurator(Type configuratorType) { diff --git a/Sunfish/Sunfish/Middleware/ApiRest.cs b/Sunfish/Sunfish/Middleware/ApiRest.cs new file mode 100644 index 0000000..82955f0 --- /dev/null +++ b/Sunfish/Sunfish/Middleware/ApiRest.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2.Middleware +{ + public class ApiRest + { + //JsonNet.Serialize + public static void WriteError(string text, HttpCall call) + { + call.Write("{\"error\":\"" + text.Replace("\"", "\\\"") + "\"}"); + } + } +} diff --git a/Sunfish/Sunfish/Services/RootService.cs b/Sunfish/Sunfish/Services/RootService.cs index 66640ab..913f8c8 100644 --- a/Sunfish/Sunfish/Services/RootService.cs +++ b/Sunfish/Sunfish/Services/RootService.cs @@ -11,11 +11,10 @@ { public const string DIR_COMMON = "/$sunfish/"; public const string DIR_API = "api/"; - private static WebUILink linkHome; static RootService() { - linkHome = new WebUILink() + LinkHome = new WebUILink() { Icon = "home", Link = "/", @@ -41,13 +40,7 @@ // Root page if (ShowMenu) { - WebUI.WriteHeader(new WebUILink[]{ - new WebUILink() - { - Icon = "home", - Link = "/", - } - }, null, call); + WebUI.WriteHeader(new WebUILink[] { LinkHome }, null, call); foreach (SunfishService s in Sunfish.Services) { if (!s.Enabled) @@ -71,7 +64,41 @@ if (path.StartsWith(DIR_API)) { // API - path = path.Substring(DIR_API.Length); + path = "/" + path.Substring(DIR_API.Length); + SunfishService servc = Sunfish.GetServiceForPath(ref path); + if (servc == null || servc is RootService) + { + ApiRest.WriteError("Service does not exists", call); + } + else + { + if (path.StartsWith("/")) + path = path.Substring(1); + if (path == "") + { + string meta; + if (!call.Parameters.TryGetValue("format", out meta)) + meta = "json"; + switch (meta) + { + case "js": + ApiRest.WriteError("Not implemented", call); + break; + case "json": + ApiRest.WriteError("Not implemented", call); + break; + default: + ApiRest.WriteError("Format only supports 'js' and 'json'.", call); + break; + } + + } + else + { + //call method (path is method name) + } + } + } else if (path == "Sunfish.exe") { @@ -97,7 +124,7 @@ { } - public static WebUILink LinkHome => linkHome; + public static WebUILink LinkHome { get; private set; } public override string Description => "Root page service and API"; public bool ShowMenu { get; set; } = true; diff --git a/Sunfish/Sunfish/Services/WebService.cs b/Sunfish/Sunfish/Services/WebService.cs index 987b161..b7bb026 100644 --- a/Sunfish/Sunfish/Services/WebService.cs +++ b/Sunfish/Sunfish/Services/WebService.cs @@ -12,6 +12,7 @@ [DefineConfigurator(typeof(WebServiceConfigurator))] class WebService : SunfishService { + private VFS vfs = new VFS(); private string index; private bool allowNavigation; private bool allowSubfolderNavigation; @@ -25,6 +26,8 @@ allowSubfolderNavigation = ssc.GetConf(WebServiceConfigurator.CFG_NAVIGATION); } + #region WebServer + private void ErrorPage(int code, HttpCall call, string text) { call.Response.StatusCode = code; @@ -242,6 +245,8 @@ return lst.ToArray(); } + #endregion + protected override void Start() { } @@ -250,6 +255,16 @@ { } + #region API + + [ApiMethod("rename")] + public bool ApiMove(string from, string to) + { + return false; + } + + #endregion + public override string Description => "For Webpages or file sharing"; } } diff --git a/Sunfish/Sunfish/Sunfish.csproj b/Sunfish/Sunfish/Sunfish.csproj index 58a309a..9e38780 100644 --- a/Sunfish/Sunfish/Sunfish.csproj +++ b/Sunfish/Sunfish/Sunfish.csproj @@ -79,6 +79,7 @@ + @@ -104,6 +105,7 @@ + diff --git a/Sunfish/Sunfish/SunfishService.cs b/Sunfish/Sunfish/SunfishService.cs index c257ce5..8449b05 100644 --- a/Sunfish/Sunfish/SunfishService.cs +++ b/Sunfish/Sunfish/SunfishService.cs @@ -72,7 +72,6 @@ #endregion private SunfishServiceConfigurator configurator; - protected VFS vfs = new VFS(); public SunfishService(SunfishServiceConfiguration ssc) {