/// <summary> /// Add attachment files. /// </summary> /// <param name="files">The http file collection.</param> /// <param name="service">The INetDriveService object.</param> public void AttachFiles(HttpFileCollectionBase files, INetDriveService service) { if (files.Count == 0) { return; } if (service == null) { throw new ArgumentNullException("service"); } var attchUri = Parent.AttachmentsPath; if (!service.Exists(attchUri)) { service.CreatePath(attchUri); } var itemUri = new Uri(attchUri.ToString() + "/" + this.ID.ToString()); if (!service.Exists(itemUri)) { service.CreatePath(itemUri); } var itemPath = service.MapPath(itemUri); for (int i = 0; i < files.Count; i++) { var file = files[i]; if (file.ContentLength > 0) { var fileName = System.IO.Path.GetFileName(file.FileName); file.SaveAs(itemPath + (!itemPath.EndsWith("\\") ? "\\" : "") + fileName); var webFile = new WebResourceInfo(itemUri.ToString() + (!itemPath.EndsWith("\\") ? "\\" : "") + fileName); var attach = new ContentAttachment() { ContentType = webFile.ContentType, Extension = webFile.Extension, ItemID = this.ID, Size = file.ContentLength, Name = fileName, Uri = webFile.Url.ToString() }; Context.Add(attach); } } Context.SaveChanges(); Model.TotalAttachments = Context.Count <ContentAttachment>(c => c.ItemID.Equals(this.ID)); this.TotalAttachments = Model.TotalAttachments; Context.SaveChanges(); }
public ActionResult GetPath(string path, int?w, int?h, string match = "*.*", int resize = 0, int index = 0, int size = 0, bool ratio = true, bool enlage = true, string format = "") { var urlString = Request.Url.ToString(); if (!string.IsNullOrEmpty(Request.Url.Query)) { urlString = urlString.Replace(Request.Url.Query, ""); } var url = new Uri(urlString); if (!service.Exists(url)) { return(HttpNotFound()); } var webres = new WebResourceInfo(Request.Url); if (match.Equals("self")) { string result = JsonConvert.SerializeObject(ToJson(new WebResourceInfo(url)), new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat }); return(Content(result, "application/json", Encoding.UTF8)); } //return Json(ToJson(new WebResourceInfo(url)), JsonRequestBehavior.AllowGet); if (webres.IsFile) { //if (IsRequestFromExternalDomain(HttpContext.Request)) //{ // if (!_context.Web.IsTrusted(HttpContext.Request.Url, HttpContext.Request.UrlReferrer)) // { // if (!string.IsNullOrEmpty(_context.Web.MasterTools.UrlForUntrustLink)) // { // var ulink = Url.Content(_context.Web.MasterTools.UrlForUntrustLink); // return File(ulink, FileUtility.GetContentType(ulink)); // } // return HttpNotFound(); // } //} var serverPath = service.MapPath(url); if (((w.HasValue && h.HasValue) || resize > 0) && webres.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase)) { //return File(GetThumbnailImage(path, webres.ContentType, w.Value, h.Value, ratio, !enlage), webres.ContentType); return(Thumb(serverPath, webres, w, h, resize, ratio, enlage)); } if (!string.IsNullOrEmpty(format) && format == "code") { return(Content(Text.TextEngine.CodeFile(serverPath).ToHtmlString(), "text/html")); } return(File(serverPath, FileUtility.GetContentType(serverPath))); } else { var paging = index > 0 && size > 0; IQueryable <Uri> results = null; var decodeType = Server.UrlDecode(string.IsNullOrEmpty(match) ? "*" : match); //var url = Request.Url; if (resize > 0 || (w.HasValue && h.HasValue)) // Get folder cover { var serverPath = service.MapPath(url); var files = Directory.GetFiles(serverPath); FileInfo lastestFileInfo = null; var attrs = service.GetAttributes(url); var coverUrl = ""; Uri coverUri = null; int width = resize; int height = resize; if (w.HasValue && h.HasValue) { width = w.Value; height = h.Value; } if (attrs != null && attrs.Count > 0 && attrs.ContainsKey("cover") && !string.IsNullOrEmpty((string)attrs["cover"])) { coverUrl = url.ToString() + (url.ToString().EndsWith("/") ? "" : "/") + (string)attrs["cover"]; Uri.TryCreate(coverUrl, UriKind.Absolute, out coverUri); if (coverUri != null) { if (service.Exists(coverUri)) { var coverPath = service.MapPath(coverUri); var coverRes = new WebResourceInfo(coverUri); if (coverRes.ContentType.StartsWith("image")) { return(File(GetThumbnailImage(coverPath, coverRes.ContentType, width, height, ratio, !enlage), coverRes.ContentType)); } } } } //If the cover is set in metax foreach (var _f in files) { var fi = new FileInfo(_f); if (fi.Extension.Equals(".metax")) { continue; } if (lastestFileInfo != null) { if (fi.CreationTime > lastestFileInfo.CreationTime) { lastestFileInfo = fi; } } else { lastestFileInfo = fi; } } if (lastestFileInfo != null) { var lastWebResUrl = url.ToString() + (url.ToString().EndsWith("/") ? "" : "/") + lastestFileInfo.Name; var lastWebRes = new WebResourceInfo(lastWebResUrl); if (lastWebRes.ContentType.StartsWith("image")) { return(File(GetThumbnailImage(lastestFileInfo.FullName, lastWebRes.ContentType, width, height, ratio, !enlage), lastWebRes.ContentType)); } } return(HttpNotFound()); } if (decodeType == "*.*") //Paths and files { results = service.GetPaths(url).AsQueryable(); results = results.Union(service.GetFiles(url)); } else { if (decodeType == "*") //Only paths { results = service.GetPaths(url).AsQueryable(); } if (decodeType[0] == '.' || decodeType.StartsWith("*.")) //only files { results = service.GetFiles(url).AsQueryable(); if (decodeType != ".*" && decodeType.Length > 1) // extension filter { results = results.Where(r => r.ToString().ToLower().EndsWith(decodeType.StartsWith("*") ? decodeType.Substring(1) : decodeType)); } } } if (results == null) { return(Json(new List <WebResourceInfo>(), JsonRequestBehavior.AllowGet)); } if (!paging) { if (results.Count() > 0) { return(Json(results.ToList().Select(f => ToJson(new WebResourceInfo(f))), JsonRequestBehavior.AllowGet)); } else { return(Json(new List <WebResourceInfo>(), JsonRequestBehavior.AllowGet)); } } else { var jsonResults = results.Skip((index - 1) * size).Take(size).ToList(); return(Json(new { Total = results.Count(), Model = jsonResults.Count > 0 ? jsonResults.Select(f => ToJson(new WebResourceInfo(f))) : jsonResults }, JsonRequestBehavior.AllowGet)); } } }