using Nancy; using Nancy.ModelBinding; using Nancy.Swagger; using Nancy.Swagger.Annotations.Attributes; using Swagger.ObjectModel; using System; using System.Collections.Generic; using System.IO; using System.IO.Pipes; using System.Linq; using System.Reflection; using 电子展板.Base; using 电子展板.Models; using 电子展板.Utility; using 电子展板.Utility.Core; using 电子展板.Utility.Extension; using 电子展板.Utility.Logs; using 电子展板.Utility.Other; namespace 电子展板.WebModule { public class HomeModule : NancyModule { public HomeModule(ISwaggerModelCatalog modelCatalog) { //涉及到哪些Model modelCatalog.AddModel(); modelCatalog.AddModel>(); //默认页面 Get("", _ => Index()); Get("/", _ => Index()); Get("/index.html", _ => Index()); //全局静态资源 Get("/static/{name*}", dynamic => StaticResource(dynamic.name ?? "")); //获得配置 Get("/getConfig", _ => GetConfig(), null, "getConfig"); //获得上传的文件 Get("/upload/{id}", dynamic => GetUploadFiles(dynamic.id ?? "")); //上传图片 Post("/uploadImage", dynamic => { IEnumerable files = Request.Files; if (files == null || files.Count() == 0) return UploadImage(null); return UploadImage(files.FirstOrDefault()); }, null, "uploadImage"); //保存配置 Post("/save", dynamic => { MyConfig config = this.Bind(); return SaveConfig(config); }, null, "save"); } /// /// 默认页面 /// /// /// private object Index() { return Resouce("Views/Index.html"); } /// /// 全局静态资源 /// /// /// private object StaticResource(string name) { return Resouce(name); } /// /// 获得配置 /// /// /// [Route("getConfig")]//Name [Route(HttpMethod.Get, "/getConfig")]//Method And Path [Route(Summary = "获得配置数据")] [SwaggerResponse(HttpStatusCode.OK, typeof(MyConfig))] [Route(Tags = new[] { "获得配置数据" })] private object GetConfig() { return Response.AsJson(GlobalVariable.Config); } /// /// 获得上传的文件 /// /// /// private object GetUploadFiles(string id) { string contentType = MimeTypes.GetMimeType(id); //return Response.AsFile(MyEnvironment.Root("/Upload/" + id), contentType); UploadFiles uploadFiles = GlobalVariable.Config.FileUploadList.FirstOrDefault(x => x.Name == id); if (uploadFiles == null) return null; return Response.FromStream(new MemoryStream(uploadFiles.Bin), contentType); } /// /// 上传图片 /// /// /// [Route("uploadImage")]//Name [Route(HttpMethod.Post, "/uploadImage")]//Method And Path [Route(Summary = "上传图片")] [SwaggerResponse(HttpStatusCode.OK, typeof(RetValue))] [Route(Tags = new[] { "上传图片" })] private object UploadImage([RouteParam(ParamIn = ParameterIn.Form, Name = "file", Description = "图片文件", ParamType = typeof(SwaggerFile), Required = true)] HttpFile file) { string uploadPath = MyEnvironment.Root("/Upload/"); if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } string ext = Path.GetExtension(file.Name); string fileName = UUID.StrSnowId + ext; //var filePath = uploadPath + fileName; //using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) //{ // file.Value.CopyTo(fileStream); //} ByteArrayStream stream = new ByteArrayStream(); file.Value.CopyTo(stream); byte[] data = stream.ToByteArray(); GlobalVariable.Config.FileUploadList.Add(new UploadFiles { Name = fileName, Bin = data }); GlobalVariable.SaveConfig(); RetValue result = new RetValue { state = 1, message = "上传成功", data = $"/upload/{fileName}" }; return Response.AsJson(result); } [Route("save")]//Name [Route(HttpMethod.Post, "/save")]//Method And Path [Route(Summary = "保存配置")] [SwaggerResponse(HttpStatusCode.OK, typeof(RetValue))] [Route(Tags = new[] { "保存配置" })] private object SaveConfig([RouteParam(ParamIn = ParameterIn.Body, Name = "parms", Description = "保存参数", Required = true)] MyConfig config) { //保存 PropertyInfo[] propertyInfos = typeof(MyConfig).GetProperties(); foreach (PropertyInfo propertyInfo in propertyInfos) { if (propertyInfo.Name == "FileUploadList") { continue; } object value = propertyInfo.GetValue(config); propertyInfo.SetValue(GlobalVariable.Config, value); } GlobalVariable.SaveConfig(); //保存并修改界面 EventBus.Instance.Publish("save", ""); RetValue result = new RetValue { state = 1, message = "保存成功" }; return Response.AsJson(result); } private object Resouce(string resourcePath) { string packUri = $"pack://application:,,,/Assets/{resourcePath}"; string contentType = MimeTypes.GetMimeType(packUri); return Response.FromStream(System.Windows.Application.GetResourceStream(new Uri(packUri, UriKind.RelativeOrAbsolute)).Stream, contentType); } } }