public FileAPIHandler() : base() { DocumentDomain documentDomain = new DocumentDomain(new DocumentRepository()); userlog log = new userlog(); Before += ctx => { // new userlogB().Insert(new userlog { }); log.ip = ctx.Request.UserHostAddress; log.loginfo = ctx.Request.Path; log.remark = DateTime.Now.ToString("yyyy-MM-dd HH:mm"); return(ctx.Response); }; Post["/document/checkauth"] = p => { // new DocumentRepository().SaveDocument("3b31d546f6f94a8029f966a8d71c7c66", "SWXX", "123123", ".png", "image/png", @"D:\FileCenter\SWXX\201601\3b31d546f6f94a8029f966a8d71c7c66.png", DateTime.Now); string code = this.Request.Form.code; string secret = this.Request.Form.secret; Qycode space = new QycodeB().GetEntities(d => d.Code == code && d.secret == secret).FirstOrDefault(); return(space == null ? "N" : "Y"); }; Post["/document/fileupload"] = p => { //借助上传组件工具上传 try { string size = this.Request.Form.size; string chunks = this.Request.Form.chunks; string strchunk = this.Request.Form.chunk; string fileMd5 = this.Request.Form.fileMd5; string spacecode = this.Request.Form.code; string upinfo = this.Request.Form.upinfo; if (!string.IsNullOrEmpty(spacecode)) { spacecode = spacecode.Trim('\''); int chunkcount = 0, chunk; if (!string.IsNullOrEmpty(chunks) && int.TryParse(chunks, out chunkcount) && chunkcount > 1 && int.TryParse(strchunk, out chunk)) { //属于分片文件 var file = this.Request.Files.FirstOrDefault(); if (file != null) { var md5 = documentDomain.PushChunk(fileMd5, chunk, file.Name, Path.GetExtension(file.Name), file.ContentType, file.Value); return(Response.AsJson(md5)); } } else { var file = this.Request.Files.FirstOrDefault(); if (file != null) { var md5 = documentDomain.Push(file.Name, System.IO.Path.GetExtension(file.Name), file.ContentType, file.Value, spacecode, upinfo); return(Response.AsJson(md5)); } } } return(Response.AsRedirect("/document")); } catch (Exception ex) { Logger.LogError(ex.Message.ToString()); return(Response.AsRedirect("/document")); } }; Post["/document/fileupload/{qycode}/{upinfo}"] = p => { //后台上传 try { string qycode = p.qycode; string upinfo = p.upinfo; var file = this.Request.Files.FirstOrDefault(); if (file != null) { var md5 = documentDomain.Push(file.Name, System.IO.Path.GetExtension(file.Name), file.ContentType, file.Value, qycode, upinfo); return(Response.AsText(md5)); } return(Response.AsRedirect("/document")); } catch (Exception ex) { Logger.LogError(ex.Message.ToString()); return(Response.AsRedirect("/document")); } }; Post["/document/fileupload/{qycode}"] = p => { //后台上传 try { string qycode = p.qycode; var file = this.Request.Files.FirstOrDefault(); if (file != null) { var md5 = documentDomain.Push(file.Name, System.IO.Path.GetExtension(file.Name), file.ContentType, file.Value, qycode, ""); return(Response.AsText(md5)); } return(Response.AsRedirect("/document")); } catch (Exception ex) { Logger.LogError(ex.Message.ToString()); return(Response.AsRedirect("/document")); } }; Post["/document/fileupload/checkwholefile"] = p => { var md5Client = this.Request.Form.md5; string spacecode = this.Request.Form.code; //MD5值验证 string md5 = md5Client.Value.ToLower(); var result = documentDomain.Exist(spacecode, md5); return(Response.AsJson(result)); }; Post["/document/fileupload/fileMerge"] = p => { string fileMd5 = this.Request.Form.fileMd5; string ext = this.Request.Form.ext; string name = this.Request.Form.name; string contentType = this.Request.Form.contentType; string spacecode = this.Request.Form.code; string upinfo = this.Request.Form.upinfo; if (!ext.StartsWith(".")) { ext = "." + ext; } string strID = "0"; var result = documentDomain.FileMerge(fileMd5, name, contentType, ext, spacecode, upinfo); return(Response.AsJson(result)); }; Get["/{qycode}/document/fileupload"] = p => { string strCode = p.qycode; Qycode space = new QycodeB().GetEntities(d => d.Code == strCode).FirstOrDefault(); return(View["FileUpload"]); }; Get["/document/info/{id}"] = p => { string idS = p.id; string[] ids = idS.Trim(',').Split(','); List <Document> spaces = new DocumentB().GetEntities(d => d.Qycode == "-1").ToList(); foreach (string id in ids) { Document space = new DocumentB().GetEntities(d => d.ID == id).FirstOrDefault(); if (space != null) { spaces.Add(space); } } return(Response.AsJson(spaces)); }; //下一版废弃 Get["/{qycode}/document/{id}"] = p => { log.useraction = "下载文件"; string id = p.id; string qycode = p.qycode; if (!string.IsNullOrEmpty(id) && id.Contains(",")) { var md5s = id.Split(','); var documents = documentDomain.Fetch("", md5s); if (documents != null && documents.Any()) { //压缩,并返回 var mimeType = "application/zip"; var extension = "zip"; var name = "打包下载的文件"; var file = documentDomain.Compress(documents); return(Response.AsFileV1(file, mimeType, extension, name)); } else { return("找不到该文件" + p.id); } } else { var document = documentDomain.Fetch("", p.id); if (document == null) { return("找不到该文件 " + p.id); } string mimeType = document.contenttype; string extension = document.extension; string file = document.file; string name = document.name; return(Response.AsFileV1(file, mimeType, extension, name)); } }; Get["/document/{id}"] = p => { log.useraction = "下载文件"; string id = p.id; if (!string.IsNullOrEmpty(id) && id.Contains(",")) { var md5s = id.Split(','); var documents = documentDomain.Fetch("", md5s); if (documents != null && documents.Any()) { //压缩,并返回 var mimeType = "application/zip"; var extension = "zip"; var name = "打包下载的文件"; var file = documentDomain.Compress(documents); return(Response.AsFileV1(file, mimeType, extension, name)); } else { return("找不到该文件" + p.id); } } else { var document = documentDomain.Fetch("", p.id); if (document == null) { return("找不到该文件 " + p.id); } string mimeType = document.contenttype; string extension = document.extension; string file = document.file; string name = document.name; return(Response.AsFileV1(file, mimeType, extension, name)); } }; Get["/document/zipfile/{md5}"] = p => { string md5 = p.md5; var filename = documentDomain.GetZipFile(md5); var mimeType = "application/zip"; var extension = "zip"; var name = "打包下载的文件"; return(Response.AsFileV1(filename, mimeType, extension, name)); }; Get["/{qycode}/document/image/{id}"] = p => { log.useraction = "预览图片"; string id = p.id; string qycode = p.qycode; var document = documentDomain.Fetch(qycode, p.id); if (document == null) { return("找不到该文件" + p.id); } string mimeType = document.contenttype; string extension = document.extension; string file = document.file; string name = document.name; return(Response.AsPreviewFile(file, mimeType, extension, name)); }; //修改缩略图接口 Get["/{qycode}/document/image/{id}/{width?0}/{height?0}"] = p => { log.useraction = "预览压缩图片"; string id = p.id; string qycode = p.qycode; var document = documentDomain.Fetch(qycode, p.id); if (document == null) { return("找不到该文件 " + p.id); } int width = p.width; int height = p.height; string mimeType = document.contenttype; string extension = document.extension; string file = document.file; string name = document.name; var smallImg = ImageCLass.GenSmallImg(file, width, height); return(Response.AsPreviewFile(smallImg, mimeType, extension, name)); }; Get["/{qycode}/document/unzip/{id}"] = p => { string qycode = p.qycode; var document = documentDomain.Fetch(qycode, p.id); if (document == null) { return("找不到该文档 " + p.id); } string extension = document.extension; if (extension == "zip") { SubFolderModel model = documentDomain.UnCompress(document, p.qycode); return(Response.AsJson(model)); } return(Response.AsJson("")); }; Post["/{qycode}/document/zipfolder"] = p => { var data = Request.Form.data; string qycode = p.qycode; string md5 = ""; if (data) { SubFolderModel subFolderModel = Newtonsoft.Json.JsonConvert.DeserializeObject <SubFolderModel>(data); var zipItems = subFolderModel.GetZipEntryItems().ToArray(); var md5s = zipItems.Select(o => o.Item1).ToArray(); var documents = documentDomain.Fetch(qycode, md5s); if (documents != null && documents.Any()) { md5 = documentDomain.CompressNestedFolder(documents, zipItems); } } return(md5); }; Get["/{qycode}/document/zipfile/{md5}"] = p => { string md5 = p.md5; var filename = documentDomain.GetZipFile(md5); var mimeType = "application/zip"; var extension = "zip"; var name = "打包下载的文件"; return(Response.AsFileV1(filename, mimeType, extension, name)); }; Post["/{qycode}/document/nestedfolder"] = p => { var data = Request.Form.data; string qycode = p.qycode; if (data) { SubFolderModel subFolderModel = Newtonsoft.Json.JsonConvert.DeserializeObject <SubFolderModel>(data); var zipItems = subFolderModel.GetZipEntryItems().ToArray(); var md5s = zipItems.Select(o => o.Item1).ToArray(); var documents = documentDomain.Fetch(qycode, md5s); if (documents != null && documents.Any()) { //压缩,并返回 var mimeType = "application/zip"; var extension = "zip"; var name = subFolderModel.Name; string file = documentDomain.CompressNestedFolder(documents, zipItems); return(Response.AsFileV1(file, mimeType, extension, name)); } else { return("找不到该文档 " + subFolderModel.Name); } } return(""); }; #region 多媒体相关接口 Get["/document/officecov/{id}"] = p => { string strReturn = "0,0"; string id = p.id; Document document = documentDomain.Fetch(p.id); if (document == null) { return("找不到该文件" + p.id); } else { strReturn = document.isyl + "," + document.ylinfo; if (document.isyl == "0") { string extension = document.Extension.TrimStart('.'); string file = document.FullPath; //OfficeConverter Converter = new OfficeConverter(); //if (new List<string>() { "pdf" }.Contains(extension.ToLower())) //{ // Converter.PdfToImage(file, file.Substring(0, file.LastIndexOf('.')), 0, 0, 400); //} //if (new List<string>() { "doc", "docx" }.Contains(extension.ToLower())) //{ // Converter.WordToImage(document, file.Substring(0, file.LastIndexOf('.')), 0, 0, null, 400); //} //if (new List<string>() { "ppt", "pptx" }.Contains(extension.ToLower())) //{ // Converter.PPTToImage(file, file.Substring(0, file.LastIndexOf('.')), 0, 0, 400); //} } //if (new List<string>() { "xls", "xlsx" }.Contains(extension.ToLower())) //{ // strReturn = Converter.YLExcel(file); // return Response.AsRedirect(strReturn); //} return(strReturn); } }; //返回office预览文件 Get["/document/YL/{id}"] = p => { log.useraction = "预览OFFICE文件"; Document document = documentDomain.Fetch(p.id); if (document == null) { return("找不到可预览的文件 " + p.id); } else if (document.isyl != "2") { return("预览文件正在准备中 " + p.id); } else { string strFileName = document.FileName.Substring(0, document.FileName.LastIndexOf('.')); string strReturn = "/Web/Html/doc.html?ID=" + p.id + "&size=" + document.ylinfo + "&title=" + HttpUtility.UrlEncode(strFileName); return(Response.AsRedirect(strReturn)); } }; //获取可用的视频播放路径 Get["/document/viedo/{id}"] = p => { Document document = documentDomain.Fetch(p.id); string tempfile = document.Directory.TrimEnd('\\') + "\\" + document.Md5 + "\\" + document.Md5; if (document == null) { return("找不到文件 " + p.id); } if (document.isyl != "2") { return("正在生成可预览文件 "); } else { string mimeType = "video/mp4"; string extension = ".mp4"; string file = tempfile + "_low.mp4"; string name = file; string[] range_info = (string[])Request.Headers["Range"]; if (range_info.Length == 0) { return(Response.AsFileV1(file, mimeType, extension, document.ID.ToString() + "_low.mp4")); } else { string strRange = range_info[0].ToString(); return(Response.AsVideoFile(document.FullPath, mimeType, extension, name, strRange)); } } }; //获取视频封面 Get["/document/viedo/img/{id}"] = p => { Document document = documentDomain.Fetch(p.id); string tempfile = document.Directory.TrimEnd('\\') + "\\" + document.Md5 + "\\" + document.Md5; if (document == null) { return("找不到文件 " + p.id); } if (document.isyl != "2") { return("正在生成可预览文件 "); } else { string mimeType = "image/jpeg"; string extension = ".jpg"; string file = tempfile + ".jpg"; string name = file; return(Response.AsPreviewFile(file, mimeType, extension, name)); } }; //获取office转换后的图片 Get["/document/YL/{id}/{index}"] = p => { log.useraction = "预览OFFICE图片"; string strReturn = "无法预览"; string id = p.id; string index = p.index; Document document = documentDomain.Fetch(p.id); if (document == null) { return("找不到可预览的文件 " + p.id); } else { if (document.isyl == "1") { strReturn = "预览准备中"; } if (document.isyl == "2") { string ylfile = document.FullPath; ylfile = ylfile.Substring(0, ylfile.LastIndexOf('.')) + "/" + document.Md5 + "_" + index + ".png"; string strName = document.Md5 + "_" + index + ".png"; if (new List <string>() { ".ppt", ".pptx", ".doc", ".docx", ".pdf" }.Contains(document.Extension.ToLower())) { if (File.Exists(ylfile)) { return(Response.AsPreviewFile(ylfile, "image/png", ".png", strName)); } } } } return(strReturn); }; After += ctx => { //添加日志 new userlogB().Insert(log); }; #endregion }
public APIHandler() : base() { Msg_Result msg = new Msg_Result() { Action = "", ErrorMsg = "" }; JH_Auth_UserB.UserInfo UserInfo = new JH_Auth_UserB.UserInfo(); Before += ctx => { try { //Logger.LogInfo("请求地址" + ctx.Request.Path); if (ctx.Request.Path == "/adminapi/login") { return(ctx.Response); } else if (ctx.Request.Path == "/adminapi/getuser") { return(ctx.Response); } else if (ctx.Request.Path == "/") { return(Response.AsRedirect("/Web/Login.html")); } else if (ctx.Request.Path.StartsWith("/adminapi/dfile")) { return(ctx.Response); } else if (ctx.Request.Path.StartsWith("/adminapi/ExeActionPub")) { return(ctx.Response); } else { string strUser = ""; string strpasd = ""; string strDay = ""; string filecode = ""; List <user> users = new List <user>(); if (ctx.Request.Cookies.ContainsKey("user")) { strUser = ctx.Request.Cookies["user"]; } if (ctx.Request.Cookies.ContainsKey("filecode")) { filecode = ctx.Request.Cookies["filecode"]; } if (string.IsNullOrEmpty(filecode)) { filecode = ctx.Request.Query["filecode"]; } if (string.IsNullOrEmpty(filecode)) { filecode = ctx.Request.Form["filecode"]; } if (!string.IsNullOrEmpty(filecode)) { strUser = new CacheHelp().Get(filecode); users = new JH_Auth_UserB().GetEntities(d => d.username == strUser).ToList(); new CacheHelp().Set(filecode, strUser); } if (users.Count() != 1) { msg.ErrorMsg = "NOSESSIONCODE"; return(Response.AsText(JsonConvert.SerializeObject(msg), "text/html; charset=utf-8")); } else { UserInfo.User = users[0]; } return(ctx.Response); } } catch (Exception ex) { msg.ErrorMsg = ex.Message.ToString(); return(Response.AsText(JsonConvert.SerializeObject(msg), "text/html; charset=utf-8")); } }; Get["/"] = p => { return(Response.AsRedirect("/Web/Login.html")); //return View["login"]; }; ///获取文件 Get["/adminapi/dfile/{fileid}"] = p => { int fileId = 0; int.TryParse(p.fileid.Value.Split(',')[0], out fileId); FT_File file = new FT_FileB().GetEntity(d => d.ID == fileId); string strzyid = p.fileid.Value; if (file != null) { strzyid = file.zyid; } string width = Context.Request.Query["width"].Value ?? ""; string height = Context.Request.Query["height"].Value ?? ""; Qycode qy = new QycodeB().GetALLEntities().FirstOrDefault(); string filename = Context.Request.Url.SiteBase + "/" + qy.Code + "/document/" + strzyid; if (width + height != "") { filename = Context.Request.Url.SiteBase + "/" + qy.Code + "/document/image/" + strzyid + (width + height != "" ? ("/" + width + "/" + height) : ""); } return(Response.AsRedirect(filename)); }; //登录接口 Post["/adminapi/login"] = p => { string strUser = Context.Request.Form["user"]; string strpasd = Context.Request.Form["pasd"]; var users = new JH_Auth_UserB().GetEntities(d => d.username == strUser && d.pasd == CommonHelp.GetMD5(strpasd)); if (users.Count() == 1) { msg.Result = "Y"; string strToken = CommonHelp.GenerateToken(strUser); msg.Result2 = strToken; new CacheHelp().Set(strToken, strUser); } else { msg.Result = "N"; } //JsonConvert.SerializeObject(Model).Replace("null", "\"\""); return(Response.AsJson(msg)); }; Post["/adminapi/getuser"] = p => { string strUser = Context.Request.Form["user"]; string Secret = Context.Request.Form["Secret"]; string strlotusSecret = CommonHelp.GetConfig("lotusSecret", "www.qijiekeji.com"); Logger.LogError("用户" + strUser + strlotusSecret + "获取Code" + Secret); if (Secret == strlotusSecret) { var users = new JH_Auth_UserB().GetEntities(d => d.username == strUser); if (users.Count() == 1) { string strToken = CommonHelp.GenerateToken(strUser); msg.Result = strToken; new CacheHelp().Set(strToken, strUser); } else { msg.ErrorMsg = "找不到用户信息"; return(Response.AsText(JsonConvert.SerializeObject(msg), "text/html; charset=utf-8")); } } else { msg.ErrorMsg = "Secret不匹配"; return(Response.AsText(JsonConvert.SerializeObject(msg), "text/html; charset=utf-8")); } //JsonConvert.SerializeObject(Model).Replace("null", "\"\""); return(Response.AsJson(msg)); }; //普通用户接口 Post["/adminapi/ExeAction/{action}"] = p => { JObject JsonData = new JObject(); foreach (string item in Context.Request.Form.Keys) { JsonData.Add(item, Context.Request.Form[item].Value); } string PostData = ""; string P1 = JsonData["P1"] == null ? "" : JsonData["P1"].ToString(); string P2 = JsonData["P2"] == null ? "" : JsonData["P2"].ToString(); string strAction = p.action; Qycode qy = new QycodeB().GetALLEntities().FirstOrDefault(); JH_Auth_QY QYinfo = new JH_Auth_QY(); QYinfo.FileServerUrl = Context.Request.Url.SiteBase; QYinfo.QYCode = qy.Code; UserInfo.QYinfo = QYinfo; //Dictionary<string, string> results3 = JsonConvert.DeserializeObject<Dictionary<string, string>>(PostData.ToString()); var function = Activator.CreateInstance(typeof(QYWDManage)) as QYWDManage; var method = function.GetType().GetMethod(strAction.ToUpper()); method.Invoke(function, new object[] { JsonData, msg, P1, P2, UserInfo }); IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; string Result = JsonConvert.SerializeObject(msg, Formatting.Indented, timeConverter).Replace("null", "\"\""); return(Response.AsText(Result, "text/html; charset=utf-8")); }; //管理用户接口 Post["/adminapi/ExeActionAuth/{action}"] = p => { JObject JsonData = new JObject(); foreach (string item in Context.Request.Form.Keys) { JsonData.Add(item, Context.Request.Form[item].Value); } string PostData = ""; string P1 = JsonData["P1"] == null ? "" : JsonData["P1"].ToString(); string P2 = JsonData["P2"] == null ? "" : JsonData["P2"].ToString(); string strAction = p.action; Qycode qy = new QycodeB().GetALLEntities().FirstOrDefault(); JH_Auth_QY QYinfo = new JH_Auth_QY(); QYinfo.FileServerUrl = Context.Request.Url.SiteBase; QYinfo.QYCode = qy.Code; UserInfo.QYinfo = QYinfo; //Dictionary<string, string> results3 = JsonConvert.DeserializeObject<Dictionary<string, string>>(PostData.ToString()); var function = Activator.CreateInstance(typeof(QYWDManage)) as QYWDManage; var method = function.GetType().GetMethod(strAction.ToUpper()); method.Invoke(function, new object[] { JsonData, msg, P1, P2, UserInfo }); IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; string Result = JsonConvert.SerializeObject(msg, Formatting.Indented, timeConverter).Replace("null", "\"\""); return(Response.AsText(Result, "text/html; charset=utf-8")); }; //外部接口 Post["/adminapi/ExeActionPub/{action}"] = p => { JObject JsonData = new JObject(); foreach (string item in Context.Request.Form.Keys) { JsonData.Add(item, Context.Request.Form[item].Value); } string PostData = ""; string P1 = JsonData["P1"] == null ? "" : JsonData["P1"].ToString(); string P2 = JsonData["P2"] == null ? "" : JsonData["P2"].ToString(); string strAction = p.action; Qycode qy = new QycodeB().GetALLEntities().FirstOrDefault(); JH_Auth_QY QYinfo = new JH_Auth_QY(); QYinfo.FileServerUrl = Context.Request.Url.SiteBase; QYinfo.QYCode = qy.Code; UserInfo.QYinfo = QYinfo; //Dictionary<string, string> results3 = JsonConvert.DeserializeObject<Dictionary<string, string>>(PostData.ToString()); var function = Activator.CreateInstance(typeof(PubManage)) as PubManage; var method = function.GetType().GetMethod(strAction.ToUpper()); method.Invoke(function, new object[] { JsonData, msg, P1, P2, UserInfo }); IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; string Result = JsonConvert.SerializeObject(msg, Formatting.Indented, timeConverter).Replace("null", "\"\""); return(Response.AsText(Result, "text/html; charset=utf-8")); }; After += ctx => { msg.Action = Context.Request.Path; //添加日志 }; }