public HttpResponseMessage ImagesLookupExt(string q, string folder) { try { var folderManager = FolderManager.Instance; string imageFolder = "OpenContent/Files/" + ActiveModule.ModuleID; if (!string.IsNullOrEmpty(folder)) { imageFolder = folder; } var dnnFolder = folderManager.GetFolder(PortalSettings.PortalId, imageFolder); if (dnnFolder == null) { dnnFolder = folderManager.AddFolder(PortalSettings.PortalId, imageFolder); } var files = folderManager.GetFiles(dnnFolder, true); files = files.Where(f => IsImageFile(f)); if (q != "*" && !string.IsNullOrEmpty(q)) { files = files.Where(f => f.FileName.ToLower().Contains(q.ToLower())); } int folderLength = imageFolder.Length; var res = files.Select(f => new { id = f.FileId.ToString(), thumbUrl = ImageHelper.GetImageUrl(f, new Ratio(40, 40)), //todo for install in application folder is dat niet voldoende ??? url = DnnFileUtils.RemoveCachbuster(FileManager.Instance.GetUrl(f)), text = f.Folder.Substring(folderLength).TrimStart('/') + f.FileName }).Take(1000); return(Request.CreateResponse(HttpStatusCode.OK, res)); } catch (Exception exc) { Logger.Error(exc); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc)); } }
/// <summary> /// Gets the image URL. /// If OpenFiles has been installed, some extra logic is applied. /// </summary> public static string GetImageUrl(IFileInfo file, Ratio requestedCropRatio) { if (file == null) { throw new NoNullAllowedException("FileInfo should not be null"); } if (ModuleDefinitionController.GetModuleDefinitionByFriendlyName("OpenFiles") == null) { return(DnnFileUtils.ToUrl(file)); } var url = file.ToLinkClickSafeUrl(); url = url.RemoveQueryParams(); //imageprocessor does not tolerate unknow querystrings (for security reasons). Remove them JObject content = GetContentAsJObject(file); if (content != null) { var crop = content["crop"]; if (crop is JObject && crop["croppers"] != null) { foreach (var cropperobj in crop["croppers"].Children()) { try { var cropper = cropperobj.Children().First(); int w = int.Parse(cropper["width"].ToString()); int h = int.Parse(cropper["height"].ToString()); var definedCropRatio = new Ratio(w, h); if (Math.Abs(definedCropRatio.AsFloat - requestedCropRatio.AsFloat) < 0.02) //allow 2% margin { if (cropper["x"] == null || cropper["x"].IsEmpty()) { cropper["x"] = 0; } if (cropper["y"] == null || cropper["y"].IsEmpty()) { cropper["y"] = 0; } int left = int.Parse(cropper["x"].ToString()); int top = int.Parse(cropper["y"].ToString()); // crop first then resize (order defined by the processors definition order in the config file) // don't specify new Height, otherwise you might end up with black lines under your image. The height will be automaticly calculated based on the width and the crop ratio. return(url.AppendQueryParams($"crop={left},{top},{w},{h}&width={requestedCropRatio.Width}")); } } catch (Exception ex) { App.Services.Logger.Warn($"Warning for page {HttpContext.Current.Request.RawUrl}. Error processing croppers for {url} in {content}. Error: {ex.Message}"); } } } else { //App.Services.Logger.Debug(string.Format("Warning for page {0}. Can't find croppers in {1}. ", HttpContext.Current.Request.RawUrl, contentItem.Content)); } } return(url.AppendQueryParams($"width={requestedCropRatio.Width}&height={requestedCropRatio.Height}&mode=crop")); }
// Upload entire file private void UploadWholeFile(HttpContextBase context, ICollection <FilesStatus> statuses) { for (var i = 0; i < context.Request.Files.Count; i++) { var file = context.Request.Files[i]; if (file == null) { continue; } var fileName = CleanUpFileName(Path.GetFileName(file.FileName)); if (IsAllowedExtension(fileName)) { string uploadfolder = "OpenContent/Files/" + ActiveModule.ModuleID; if (!string.IsNullOrEmpty(context.Request.Form["uploadfolder"])) { uploadfolder = context.Request.Form["uploadfolder"]; } var userFolder = _folderManager.GetFolder(PortalSettings.PortalId, uploadfolder); if (userFolder == null) { userFolder = _folderManager.AddFolder(PortalSettings.PortalId, uploadfolder); } int suffix = 0; string baseFileName = Path.GetFileNameWithoutExtension(fileName); string extension = Path.GetExtension(fileName); var fileInfo = _fileManager.GetFile(userFolder, fileName); while (fileInfo != null) { suffix++; fileName = baseFileName + "-" + suffix + extension; fileInfo = _fileManager.GetFile(userFolder, fileName); } fileInfo = _fileManager.AddFile(userFolder, fileName, file.InputStream, true); var fileIcon = IconController.IconURL("Ext" + fileInfo.Extension, "32x32"); if (!File.Exists(context.Server.MapPath(fileIcon))) { fileIcon = IconController.IconURL("File", "32x32"); } statuses.Add(new FilesStatus { success = true, name = fileName, extension = fileInfo.Extension, type = fileInfo.ContentType, size = file.ContentLength, progress = "1.0", url = DnnFileUtils.RemoveCachbuster(fileInfo.ToUrl()), thumbnail_url = fileIcon, message = "success", id = fileInfo.FileId, }); } else { statuses.Add(new FilesStatus { success = false, name = fileName, message = "File type not allowed." }); } } }