diff --git a/Sunfish/Sunfish/Middleware/VFS.cs b/Sunfish/Sunfish/Middleware/VFS.cs index be135ea..5ad96a9 100644 --- a/Sunfish/Sunfish/Middleware/VFS.cs +++ b/Sunfish/Sunfish/Middleware/VFS.cs @@ -61,6 +61,8 @@ public VFSItem GetItem(string path) { + while (path.EndsWith("/")) + path = path.Substring(0, path.Length - 1); VFSFolder folder = LocateFolder(ref path); if (folder == null) return null; diff --git a/Sunfish/Sunfish/Middleware/VFSFolderFileSystem.cs b/Sunfish/Sunfish/Middleware/VFSFolderFileSystem.cs index 0769988..505893c 100644 --- a/Sunfish/Sunfish/Middleware/VFSFolderFileSystem.cs +++ b/Sunfish/Sunfish/Middleware/VFSFolderFileSystem.cs @@ -42,17 +42,27 @@ DirectoryInfo di = new DirectoryInfo(fpath); if (!fi.Exists && !di.Exists) return null; - return new VFSItem(this,fpath,di.Exists); + return new VFSItem(this,path,di.Exists); } public override string[] ListDirectories(string path) { - throw new NotImplementedException(); + string fpath = Path.Combine(basePath, path); + List lst = new List(); + foreach (string d in Directory.GetDirectories(fpath)) + lst.Add(d.Substring(fpath.Length+1)); + lst.Sort(); + return lst.ToArray(); } public override string[] ListFiles(string path) { - throw new NotImplementedException(); + string fpath = Path.Combine(basePath, path); + List lst = new List(); + foreach (string d in Directory.GetFiles(fpath)) + lst.Add(d.Substring(fpath.Length + 1)); + lst.Sort(); + return lst.ToArray(); } } diff --git a/Sunfish/Sunfish/Middleware/VFSItem.cs b/Sunfish/Sunfish/Middleware/VFSItem.cs index 972382c..b95cb86 100644 --- a/Sunfish/Sunfish/Middleware/VFSItem.cs +++ b/Sunfish/Sunfish/Middleware/VFSItem.cs @@ -29,6 +29,16 @@ return vfolder.OpenWrite(Path); } + public string[] ListDirectories() + { + return vfolder.ListDirectories(Path); + } + + public string[] ListFiles() + { + return vfolder.ListFiles(Path); + } + public string Path { get; } public string Name { get; } public bool Folder { get; } diff --git a/Sunfish/Sunfish/Middleware/WebUI.cs b/Sunfish/Sunfish/Middleware/WebUI.cs index cabe9d5..41858fa 100644 --- a/Sunfish/Sunfish/Middleware/WebUI.cs +++ b/Sunfish/Sunfish/Middleware/WebUI.cs @@ -11,7 +11,7 @@ { static WebUI() { - // InitResources(); + InitResources(); } #region Frontend Resources @@ -75,6 +75,17 @@ call.Write(File.ReadAllBytes(Path.Combine(rpath, path))); } + public static string FBytes(double lng) + { + string[] tail = { " bytes", "Kb", "Mb", "Gb", "Tb", "Pb", "Yb" }; + int taili = 0; + while (lng > 1024) + { + lng /= 1024; + taili++; + } + return lng.ToString("#0.00") + (taili >= tail.Length ? "^b" : tail[taili]); + } } diff --git a/Sunfish/Sunfish/Services/WebService.cs b/Sunfish/Sunfish/Services/WebService.cs index 92608fa..8502c31 100644 --- a/Sunfish/Sunfish/Services/WebService.cs +++ b/Sunfish/Sunfish/Services/WebService.cs @@ -22,15 +22,6 @@ allowSubfolderNavigation = ssc.GetConf(WebServiceConfigurator.CFG_NAVIGATION); } - #region ChildClasses - class WebServiceData - { - public string[] Files { get; set; } - public string ServicePath { get; set; } - public string AppTitle => "Sunfish " + Program.VERSION; - } - #endregion - private void ErrorPage(int code, HttpCall call, string text) { call.Response.StatusCode = code; @@ -68,43 +59,9 @@ } } - private object GetWebServiceData(string path) - { - WebServiceData wsd = new WebServiceData(); - if (path != null) - { - List fileList = new List(); - VFSItem itm = vfs.GetItem(path); - if (itm != null) - { - foreach (string d in vfs.ListDirectories(path)) - fileList.Add(d + "/"); - fileList.Sort(); - List lfls = new List(vfs.ListFiles(path)); - lfls.Sort(); - fileList.AddRange(lfls); - wsd.Files = fileList.ToArray(); - } - } - wsd.ServicePath = Configuration.Location; - if (!wsd.ServicePath.EndsWith("/")) - wsd.ServicePath = wsd.ServicePath + "/"; - return wsd; - } - public override void Process(string path, HttpCall call) { - if (allowNavigation && path.Contains("/|")) - { - path = path.Substring(path.IndexOf("/|") + 2); - switch (path) - { - case "data": - call.Write(JsonNet.Serialize(GetWebServiceData(call.Parameters.GetValue("path", null)))); - break; - } - } - else if (path.EndsWith("/")) + if (path.EndsWith("/")) { VFSItem idx = vfs.GetItem(path + index); //Directory entry, go for index file or navigation @@ -119,8 +76,8 @@ if (allowNavigation) { idx = vfs.GetItem(path); - if (idx != null && idx.Folder)// WHY? - DownloadAt("/$sunfish/index.html", call); + if (idx != null && idx.Folder) + WriteIndex(idx, call); else call.NotFound(); } @@ -139,10 +96,45 @@ } else call.NotFound(); - //DownloadAt("/$sunfish/404.html", call); } } + private void WriteIndex(VFSItem dir, HttpCall call) + { + WebUI.InitResources(); + WebUI.WriteHeader(call); + List fileList = new List(); + if (allowSubfolderNavigation) + { + foreach (string d in dir.ListDirectories()) + fileList.Add(d); + fileList.Sort(); + foreach (string d in fileList) + { + WebUI.WriteItem(new WebUIListItem() + { + Name = d, + Description = "Directory", + Link = d + "/" + }, call); + } + fileList.Clear(); + } + foreach (string d in dir.ListFiles()) + fileList.Add(d); + fileList.Sort(); + foreach (string d in fileList) + { + WebUI.WriteItem(new WebUIListItem() + { + Name = d, + Description = "File", + Link = d + }, call); + } + WebUI.WriteFooter(call); + } + protected override void Start() { }