public Crawler Fetch() { if (!IsExternalIPAddress(this.SourceUrl)) { State = "INVALID_URL"; return(this); } var request = HttpWebRequest.Create(this.SourceUrl) as HttpWebRequest; using (var response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) { State = "Url returns " + response.StatusCode + ", " + response.StatusDescription; return(this); } if (response.ContentType.IndexOf("image") == -1) { State = "Url is not an image"; return(this); } ServerUrl = PathFormatter.Format(Path.GetFileName(this.SourceUrl), UeditorConfig.GetString("catcherPathFormat")); var savePath = Server.MapPath(ServerUrl); if (!Directory.Exists(Path.GetDirectoryName(savePath))) { Directory.CreateDirectory(Path.GetDirectoryName(savePath)); } try { var stream = response.GetResponseStream(); var reader = new BinaryReader(stream); byte[] bytes; using (var ms = new MemoryStream()) { byte[] buffer = new byte[4096]; int count; while ((count = reader.Read(buffer, 0, buffer.Length)) != 0) { ms.Write(buffer, 0, count); } bytes = ms.ToArray(); } File.WriteAllBytes(savePath, bytes); State = "SUCCESS"; } catch (Exception e) { State = "抓取错误:" + e.Message; } return(this); } }
public Crawler Fetch() { if (!(SourceUrl.IsExternalAddress())) { State = "INVALID_URL"; return(this); } var request = WebRequest.Create(SourceUrl) as HttpWebRequest; using (var response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) { State = "Url returns " + response.StatusCode + ", " + response.StatusDescription; return(this); } if (response.ContentType.IndexOf("image", StringComparison.Ordinal) == -1) { State = "Url is not an image"; return(this); } ServerUrl = PathFormatter.Format(Path.GetFileName(SourceUrl), UeditorConfig.GetString("catcherPathFormat")); var savePath = Server.MapPath(ServerUrl); if (!Directory.Exists(Path.GetDirectoryName(savePath))) { Directory.CreateDirectory(Path.GetDirectoryName(savePath)); } try { using (var stream = response.GetResponseStream()) { using (var ms = new MemoryStream()) { stream.CopyTo(ms); File.WriteAllBytes(savePath, ms.GetBuffer()); } //var (url, success) = CommonHelper.UploadImage(savePath); //if (success) //{ // ServerUrl = url; // BackgroundJob.Enqueue(() => File.Delete(savePath)); //} } State = "SUCCESS"; } catch (Exception e) { State = "抓取错误:" + e.Message; } return(this); } }
public ActionResult Upload(string destination) { List <object> list = new List <object>(); if (Request.Form.Files.Count > 0) { foreach (var t in Request.Form.Files) { string path = Path.Combine(_hostingEnvironment.ContentRootPath, UeditorConfig.GetString("PathRoot").TrimStart('\\', '/'), destination.TrimStart('\\', '/'), t.FileName); using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { t.CopyTo(fs); } } } return(Json(new { result = list })); }
public ActionResult Handle(string path, string[] items, string toFilename) { path = path?.TrimStart('\\', '/') ?? ""; var root = UeditorConfig.GetString("PathRoot").TrimStart('\\', '/'); var file = Path.Combine(_hostingEnvironment.ContentRootPath, root, path); switch (Request.Query["action"]) { case "download": if (System.IO.File.Exists(file)) { return(this.ResumePhysicalFile(file, Path.GetFileName(file))); } break; case "downloadMultiple": byte[] buffer = _sevenZipCompressor.ZipStream(items.Select(s => Path.Combine(_hostingEnvironment.ContentRootPath, root, s.TrimStart('\\', '/'))).ToList()).ToArray(); return(this.ResumeFile(buffer, Path.GetFileName(toFilename))); } return(Content("null")); }
/// <summary> /// 百度编辑器的后端接口 /// </summary> /// <param name="action">执行动作</param> /// <returns></returns> public ActionResult UEditor() { string action = Request["action"]; switch (action) { case "config": return(Content(UeditorConfig.Items.ToJson())); case "uploadimage": return(UEditorUpload(new UeditorUploadConfig() { AllowExtensions = UeditorConfig.GetStringList("imageAllowFiles"), PathFormat = UeditorConfig.GetString("imagePathFormat"), SizeLimit = UeditorConfig.GetInt("imageMaxSize"), UploadFieldName = UeditorConfig.GetString("imageFieldName") })); case "uploadscrawl": return(UEditorUpload(new UeditorUploadConfig() { AllowExtensions = new string[] { ".png" }, PathFormat = UeditorConfig.GetString("scrawlPathFormat"), SizeLimit = UeditorConfig.GetInt("scrawlMaxSize"), UploadFieldName = UeditorConfig.GetString("scrawlFieldName"), Base64 = true, Base64Filename = "scrawl.png" })); case "uploadvideo": return(UEditorUpload(new UeditorUploadConfig() { AllowExtensions = UeditorConfig.GetStringList("videoAllowFiles"), PathFormat = UeditorConfig.GetString("videoPathFormat"), SizeLimit = UeditorConfig.GetInt("videoMaxSize"), UploadFieldName = UeditorConfig.GetString("videoFieldName") })); case "uploadfile": return(UEditorUpload(new UeditorUploadConfig() { AllowExtensions = UeditorConfig.GetStringList("fileAllowFiles"), PathFormat = UeditorConfig.GetString("filePathFormat"), SizeLimit = UeditorConfig.GetInt("fileMaxSize"), UploadFieldName = UeditorConfig.GetString("fileFieldName") })); case "listimage": return(ListFileManager(UeditorConfig.GetString("imageManagerListPath"), UeditorConfig.GetStringList("imageManagerAllowFiles"))); case "listfile": return(ListFileManager(UeditorConfig.GetString("fileManagerListPath"), UeditorConfig.GetStringList("fileManagerAllowFiles"))); case "catchimage": return(CrawlerHandler()); default: return(Content(new { state = "action 参数为空或者 action 不被支持。" }.ToJson())); } }
public ActionResult FileUploader() { HttpContext context = System.Web.HttpContext.Current; Handler action = new NotSupportedHandler(context); switch (Request["action"])//管理员用 { case "config": action = new ConfigHandler(context); break; case "uploadimage": action = new UploadHandler(context, new UploadConfig() { AllowExtensions = UeditorConfig.GetStringList("imageAllowFiles"), PathFormat = UeditorConfig.GetString("imagePathFormat"), SizeLimit = UeditorConfig.GetInt("imageMaxSize"), UploadFieldName = UeditorConfig.GetString("imageFieldName") }); break; case "uploadscrawl": action = new UploadHandler(context, new UploadConfig() { AllowExtensions = new[] { ".png" }, PathFormat = UeditorConfig.GetString("scrawlPathFormat"), SizeLimit = UeditorConfig.GetInt("scrawlMaxSize"), UploadFieldName = UeditorConfig.GetString("scrawlFieldName"), Base64 = true, Base64Filename = "scrawl.png" }); break; case "catchimage": action = new CrawlerHandler(context); break; case "uploadvideo": action = new UploadHandler(context, new UploadConfig() { AllowExtensions = UeditorConfig.GetStringList("videoAllowFiles"), PathFormat = UeditorConfig.GetString("videoPathFormat"), SizeLimit = UeditorConfig.GetInt("videoMaxSize"), UploadFieldName = UeditorConfig.GetString("videoFieldName") }); break; case "uploadfile": action = new UploadHandler(context, new UploadConfig() { AllowExtensions = UeditorConfig.GetStringList("fileAllowFiles"), PathFormat = UeditorConfig.GetString("filePathFormat"), SizeLimit = UeditorConfig.GetInt("fileMaxSize"), UploadFieldName = UeditorConfig.GetString("fileFieldName") }); break; case "listimage": action = new ListFileManager(context, UeditorConfig.GetString("imageManagerListPath"), UeditorConfig.GetStringList("imageManagerAllowFiles")); break; case "listfile": action = new ListFileManager(context, UeditorConfig.GetString("fileManagerListPath"), UeditorConfig.GetStringList("fileManagerAllowFiles")); break; } #region 用户通用方法 //switch (Request["action"])//通用 //{ // case "config": // action = new ConfigHandler(context); // break; // case "uploadimage": // action = new UploadHandler(context, new UploadConfig() // { // AllowExtensions = UeditorConfig.GetStringList("imageAllowFiles"), // PathFormat = UeditorConfig.GetString("imagePathFormat"), // SizeLimit = UeditorConfig.GetInt("imageMaxSize"), // UploadFieldName = UeditorConfig.GetString("imageFieldName") // }); // break; // case "uploadscrawl": // action = new UploadHandler(context, new UploadConfig() // { // AllowExtensions = new[] { ".png" }, // PathFormat = UeditorConfig.GetString("scrawlPathFormat"), // SizeLimit = UeditorConfig.GetInt("scrawlMaxSize"), // UploadFieldName = UeditorConfig.GetString("scrawlFieldName"), // Base64 = true, // Base64Filename = "scrawl.png" // }); // break; // case "catchimage": // action = new CrawlerHandler(context); // break; //} #endregion #region 管理员权限使用方法 //UserInfoOutputDto user = HttpContext.Session.GetByRedis<UserInfoOutputDto>(SessionKey.UserInfo) ?? new UserInfoOutputDto(); //if (user.IsAdmin) //{ // switch (Request["action"])//管理员用 // { // case "uploadvideo": // action = new UploadHandler(context, new UploadConfig() // { // AllowExtensions = UeditorConfig.GetStringList("videoAllowFiles"), // PathFormat = UeditorConfig.GetString("videoPathFormat"), // SizeLimit = UeditorConfig.GetInt("videoMaxSize"), // UploadFieldName = UeditorConfig.GetString("videoFieldName") // }); // break; // case "uploadfile": // action = new UploadHandler(context, new UploadConfig() // { // AllowExtensions = UeditorConfig.GetStringList("fileAllowFiles"), // PathFormat = UeditorConfig.GetString("filePathFormat"), // SizeLimit = UeditorConfig.GetInt("fileMaxSize"), // UploadFieldName = UeditorConfig.GetString("fileFieldName") // }); // break; // case "listimage": // action = new ListFileManager(context, UeditorConfig.GetString("imageManagerListPath"), UeditorConfig.GetStringList("imageManagerAllowFiles")); // break; // case "listfile": // action = new ListFileManager(context, UeditorConfig.GetString("fileManagerListPath"), UeditorConfig.GetStringList("fileManagerAllowFiles")); // break; // } //} #endregion string result = action.Process(); return(Content(result, ContentType.Json, Encoding.UTF8)); }
public async Task <ActionResult> UeditorFileUploader() { UserInfoDto user = HttpContext.Session.Get <UserInfoDto>(SessionKey.UserInfo) ?? new UserInfoDto(); var action = Request.Query["action"].ToString() switch //通用 { "config" => (Handler) new ConfigHandler(HttpContext), "uploadimage" => new UploadHandler(HttpContext, new UploadConfig() { AllowExtensions = UeditorConfig.GetStringList("imageAllowFiles"), PathFormat = "/" + CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload").Trim('/', '\\') + UeditorConfig.GetString("imagePathFormat"), SizeLimit = UeditorConfig.GetInt("imageMaxSize"), UploadFieldName = UeditorConfig.GetString("imageFieldName") }), "uploadscrawl" => new UploadHandler(HttpContext, new UploadConfig() { AllowExtensions = new[] { ".png" }, PathFormat = "/" + CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload").Trim('/', '\\') + UeditorConfig.GetString("scrawlPathFormat"), SizeLimit = UeditorConfig.GetInt("scrawlMaxSize"), UploadFieldName = UeditorConfig.GetString("scrawlFieldName"), Base64 = true, Base64Filename = "scrawl.png" }), "catchimage" => new CrawlerHandler(HttpContext), _ => new NotSupportedHandler(HttpContext) }; if (user.IsAdmin) { switch (Request.Query["action"])//管理员用 { //case "uploadvideo": // action = new UploadHandler(context, new UploadConfig() // { // AllowExtensions = UeditorConfig.GetStringList("videoAllowFiles"), // PathFormat = "/" + CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload") + UeditorConfig.GetString("videoPathFormat"), // SizeLimit = UeditorConfig.GetInt("videoMaxSize"), // UploadFieldName = UeditorConfig.GetString("videoFieldName") // }); // break; case "uploadfile": action = new UploadHandler(HttpContext, new UploadConfig() { AllowExtensions = UeditorConfig.GetStringList("fileAllowFiles"), PathFormat = "/" + CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload").Trim('/', '\\') + UeditorConfig.GetString("filePathFormat"), SizeLimit = UeditorConfig.GetInt("fileMaxSize"), UploadFieldName = UeditorConfig.GetString("fileFieldName") }); break; //case "listimage": // action = new ListFileManager(context, CommonHelper.SystemSettings.GetOrAdd("UploadPath", "/upload") + UeditorConfig.GetString("imageManagerListPath"), UeditorConfig.GetStringList("imageManagerAllowFiles")); // break; //case "listfile": // action = new ListFileManager(context, CommonHelper.SystemSettings.GetOrAdd("UploadPath", "/upload") + UeditorConfig.GetString("fileManagerListPath"), UeditorConfig.GetStringList("fileManagerAllowFiles")); // break; } } string result = await action.Process(); return(Content(result, ContentType.Json)); }
public ActionResult UeditorFileUploader() { //UserInfoOutputDto user = HttpContext.Session.GetByRedis<UserInfoOutputDto>(SessionKey.UserInfo) ?? new UserInfoOutputDto(); bool IsAdmin = false; Handler action = new NotSupportedHandler(HttpContext); switch (Request.Query["action"])//通用 { case "config": action = new ConfigHandler(HttpContext); break; case "uploadimage": action = new UploadHandler(HttpContext, new UploadConfig() { AllowExtensions = UeditorConfig.GetStringList("imageAllowFiles"), PathFormat = UeditorConfig.GetString("imagePathFormat"), SizeLimit = UeditorConfig.GetInt("imageMaxSize"), UploadFieldName = UeditorConfig.GetString("imageFieldName") }); break; case "uploadscrawl": action = new UploadHandler(HttpContext, new UploadConfig() { AllowExtensions = new[] { ".png" }, PathFormat = UeditorConfig.GetString("scrawlPathFormat"), SizeLimit = UeditorConfig.GetInt("scrawlMaxSize"), UploadFieldName = UeditorConfig.GetString("scrawlFieldName"), Base64 = true, Base64Filename = "scrawl.png" }); break; case "catchimage": action = new CrawlerHandler(HttpContext); break; } if (IsAdmin) { switch (Request.Query["action"])//管理员用 { //case "uploadvideo": // action = new UploadHandler(context, new UploadConfig() // { // AllowExtensions = UeditorConfig.GetStringList("videoAllowFiles"), // PathFormat = UeditorConfig.GetString("videoPathFormat"), // SizeLimit = UeditorConfig.GetInt("videoMaxSize"), // UploadFieldName = UeditorConfig.GetString("videoFieldName") // }); // break; case "uploadfile": action = new UploadHandler(HttpContext, new UploadConfig() { AllowExtensions = UeditorConfig.GetStringList("fileAllowFiles"), PathFormat = UeditorConfig.GetString("filePathFormat"), SizeLimit = UeditorConfig.GetInt("fileMaxSize"), UploadFieldName = UeditorConfig.GetString("fileFieldName") }); break; //case "listimage": // action = new ListFileManager(context, UeditorConfig.GetString("imageManagerListPath"), UeditorConfig.GetStringList("imageManagerAllowFiles")); // break; //case "listfile": // action = new ListFileManager(context, UeditorConfig.GetString("fileManagerListPath"), UeditorConfig.GetStringList("fileManagerAllowFiles")); // break; } } string result = action.Process(); return(Content(result, ContentType.Json)); }
public ActionResult Handle([FromBody] FileRequest req) { List <object> list = new List <object>(); var root = Path.Combine(_hostingEnvironment.ContentRootPath, UeditorConfig.GetString("PathRoot").TrimStart('\\', '/')); switch (req.Action) { case "list": string path = Path.Combine(root, req.Path.TrimStart('\\', '/')); string[] dirs = Directory.GetDirectories(path); string[] files = Directory.GetFiles(path); dirs.ForEach(s => { DirectoryInfo dirinfo = new DirectoryInfo(s); list.Add(new FileList() { date = dirinfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"), name = dirinfo.Name, //rights = "drwxrwxrwx", size = 0, type = "dir" }); }); files.ForEach(s => { FileInfo info = new FileInfo(s); list.Add(new FileList() { name = info.Name, //rights = "-rw-rw-rw-", size = info.Length, date = info.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"), type = "file" }); }); break; case "remove": req.Items.ForEach(s => { s = Path.Combine(root, s.TrimStart('\\', '/')); try { System.IO.File.Delete(s); } catch { Directory.Delete(s, true); } }); list.Add(new { success = "true" }); break; case "rename": case "move": path = Path.Combine(root, req.Item.TrimStart('\\', '/')); var newpath = Path.Combine(root, req.NewItemPath); if (!string.IsNullOrEmpty(req.Item)) { try { System.IO.File.Move(path, newpath); } catch { Directory.Move(path, newpath); } } else { newpath = Path.Combine(root, req.NewPath.TrimStart('\\', '/')); req.Items.ForEach(s => { try { System.IO.File.Move(Path.Combine(root, s.TrimStart('\\', '/')), Path.Combine(newpath, Path.GetFileName(s))); } catch { Directory.Move(Path.Combine(root, s.TrimStart('\\', '/')), Path.Combine(newpath, Path.GetFileName(s))); } }); } list.Add(new { success = "true" }); break; case "copy": path = Path.Combine(root, req.Item.TrimStart('\\', '/')); newpath = Path.Combine(root, req.NewItemPath.TrimStart('\\', '/')); if (!string.IsNullOrEmpty(req.Item)) { System.IO.File.Copy(path, newpath); } else { newpath = Path.Combine(root, req.NewPath.TrimStart('\\', '/')); req.Items.ForEach(s => System.IO.File.Copy(Path.Combine(root, s.TrimStart('\\', '/')), !string.IsNullOrEmpty(req.SingleFilename) ? Path.Combine(newpath, req.SingleFilename) : Path.Combine(newpath, Path.GetFileName(s)))); } list.Add(new { success = "true" }); break; case "edit": path = Path.Combine(root, req.Item.TrimStart('\\', '/')); string content = req.Content; System.IO.File.WriteAllText(path, content, Encoding.UTF8); list.Add(new { success = "true" }); break; case "getContent": path = Path.Combine(root, req.Item.TrimStart('\\', '/')); content = System.IO.File.ReadAllText(path, Encoding.UTF8); return(Json(new { result = content })); case "createFolder": string dir = Path.Combine(root, req.NewPath.TrimStart('\\', '/')); var directoryInfo = Directory.CreateDirectory(dir); list.Add(new { success = directoryInfo.Exists.ToString() }); break; case "changePermissions": break; case "compress": string filename = Path.Combine(Path.Combine(root, req.Destination.TrimStart('\\', '/')), Path.GetFileNameWithoutExtension(req.CompressedFilename) + ".zip"); _sevenZipCompressor.Zip(req.Items.Select(s => Path.Combine(root, s.TrimStart('\\', '/'))).ToList(), filename); list.Add(new { success = "true" }); break; case "extract": string folder = Path.Combine(Path.Combine(root, req.Destination.TrimStart('\\', '/')), req.FolderName.Trim('/', '\\')); string zip = Path.Combine(root, req.Item.TrimStart('\\', '/')); _sevenZipCompressor.Extract(zip, folder); list.Add(new { success = "true" }); break; } return(Json(new { result = list })); }
// GET: UEditor public void UeIndex() { UeHandler action = null; var context = System.Web.HttpContext.Current; switch (context.Request["action"]) { case "config": action = new ConfigHandler(context); break; case "uploadimage": action = new UploadHandler(context, new UploadConfig() { AllowExtensions = UeditorConfig.GetStringList("imageAllowFiles"), PathFormat = UeditorConfig.GetString("imagePathFormat"), SizeLimit = UeditorConfig.GetInt("imageMaxSize"), UploadFieldName = UeditorConfig.GetString("imageFieldName") }); break; case "uploadscrawl": action = new UploadHandler(context, new UploadConfig() { AllowExtensions = new string[] { ".png" }, PathFormat = UeditorConfig.GetString("scrawlPathFormat"), SizeLimit = UeditorConfig.GetInt("scrawlMaxSize"), UploadFieldName = UeditorConfig.GetString("scrawlFieldName"), Base64 = true, Base64Filename = "scrawl.png" }); break; case "uploadvideo": action = new UploadHandler(context, new UploadConfig() { AllowExtensions = UeditorConfig.GetStringList("videoAllowFiles"), PathFormat = UeditorConfig.GetString("videoPathFormat"), SizeLimit = UeditorConfig.GetInt("videoMaxSize"), UploadFieldName = UeditorConfig.GetString("videoFieldName") }); break; case "uploadfile": action = new UploadHandler(context, new UploadConfig() { AllowExtensions = UeditorConfig.GetStringList("fileAllowFiles"), PathFormat = UeditorConfig.GetString("filePathFormat"), SizeLimit = UeditorConfig.GetInt("fileMaxSize"), UploadFieldName = UeditorConfig.GetString("fileFieldName") }); break; case "listimage": action = new ListFileManager(context, UeditorConfig.GetString("imageManagerListPath"), UeditorConfig.GetStringList("imageManagerAllowFiles")); break; case "listfile": action = new ListFileManager(context, UeditorConfig.GetString("fileManagerListPath"), UeditorConfig.GetStringList("fileManagerAllowFiles")); break; case "catchimage": action = new CrawlerHandler(context); break; default: action = new NotSupportedHandler(context); break; } action.Process(); }