private ResultSet ResizeImage(ManagerEngine man, Hashtable input) { ResultSet rs = new ResultSet(new string[] { "status", "file", "message" }); IFile file, targetFile; ManagerConfig config; string tempName; int w, h; file = man.GetFile(man.DecryptPath((string)input["path"])); config = file.Config; if (!man.IsToolEnabled("edit", config)) { throw new ManagerException("{#error.no_access}"); } if (!file.Exists) { rs.Add("FAILED", man.EncryptPath(file.AbsolutePath), "{#error.file_not_exists}"); return(rs); } if (file.Name.IndexOf("mcic_") != 0 && !man.VerifyFile(file, "edit")) { rs.Add("FATAL", man.EncryptPath(file.AbsolutePath), man.InvalidFileMsg); return(rs); } if (input["temp"] != null && (bool)input["temp"]) { tempName = "mcic_" + HttpContext.Current.Session.SessionID + "." + PathUtils.GetExtension(file.Name); if (input["target"] != null) { targetFile = man.GetFile(man.DecryptPath((string)input["target"]), tempName); } else { targetFile = man.GetFile(file.Parent, tempName); } } else { targetFile = file; } w = Convert.ToInt32(input["width"]); h = Convert.ToInt32(input["height"]); try { ImageUtils.ResizeImage(file.AbsolutePath, targetFile.AbsolutePath, w, h, config.GetInt("edit.jpeg_quality", 90)); rs.Add("OK", man.EncryptPath(targetFile.AbsolutePath), "{#message.resize_success}"); } catch (Exception ex) { man.Logger.Error(ex.ToString()); rs.Add("FAILED", man.EncryptPath(targetFile.AbsolutePath), "{#error.resize_failed}"); } return(rs); }
private Hashtable LoadContent(ManagerEngine man, Hashtable input) { Hashtable result = new Hashtable(); IFile file; ManagerConfig config; StreamReader reader = null; Stream stream = null; file = man.GetFile(man.DecryptPath((string)input["path"])); config = file.Config; if (!man.IsToolEnabled("edit", config)) { throw new ManagerException("{#error.no_access}"); } if (config.GetBool("general.demo", false)) { throw new ManagerException("{#error.demo}"); } if (!man.VerifyFile(file, "edit")) { throw new ManagerException(man.InvalidFileMsg); } if (!file.CanWrite) { throw new ManagerException("{#error.no_write_access}"); } if (!config.GetBool("filesystem.writable", true)) { throw new ManagerException("{#error.no_write_access}"); } // Load content try { stream = file.Open(FileMode.Open); if (stream != null) { reader = new StreamReader(stream); result["content"] = reader.ReadToEnd(); } else { throw new ManagerException("{#error.no_access}"); } } finally { if (stream != null) { stream.Close(); } } return(result); }
private ResultSet CreateZip(ManagerEngine man, Hashtable input) { ResultSet rs = new ResultSet(new string[] { "status", "fromfile", "tofile", "message" }); if (man.GetFile(man.DecryptPath((string)input["topath"]), (string)input["toname"] + ".zip").Exists) { throw new ManagerException("{#error.tofile_exists}"); } for (int i = 0; input["frompath" + i] != null; i++) { IFile fromFile = man.GetFile(man.DecryptPath((string)input["frompath" + i])); IFile toFile = man.GetFile("zip://" + PathUtils.AddTrailingSlash(man.DecryptPath((string)input["topath"])) + input["toname"] + ".zip", fromFile.Name); if (!man.IsToolEnabled("zip", toFile.Config)) { throw new ManagerException("{#error.no_access}"); } if (!fromFile.Exists) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#error.no_from_file}"); continue; } // Zip check if (!man.VerifyFile(fromFile, "zip")) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), man.InvalidFileMsg); continue; } if (fromFile.CopyTo(toFile)) { rs.Add("OK", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#message.zip_success}"); } } return(rs); }
private bool StreamThumb(ManagerEngine man, NameValueCollection input) { HttpContext context = HttpContext.Current; string path, contentType; ManagerConfig config; IFile file; // Get input path = man.DecryptPath((string)input["path"]); // Find mime type contentType = man.MimeTypes[PathUtils.GetExtension(path).ToLower()]; if (contentType != null) { context.Response.ContentType = contentType; } // Get and stream file file = man.GetFile(path); config = file.Config; try { path = MakeThumb(man, file).AbsolutePath; } catch (Exception) { // Ignore } // Stream thumbnail if (file is LocalFile) { context.Response.WriteFile(path); } else { IOUtils.StreamFromTo(file.Open(FileMode.Open), context.Response.OutputStream, 1024); } return(false); }
private ResultSet SaveImage(ManagerEngine man, Hashtable input) { ResultSet rs = new ResultSet(new string[] { "status", "file", "message" }); IFile file, targetFile; ManagerConfig config; file = man.GetFile(man.DecryptPath((string)input["path"])); config = file.Config; if (!man.IsToolEnabled("edit", config)) { throw new ManagerException("{#error.no_access}"); } if (config.GetBool("general.demo", false)) { rs.Add("FAILED", man.EncryptPath(file.AbsolutePath), "{#error.demo}"); this.Cleanup(man, file.ParentFile); return(rs); } if (file.Name.IndexOf("mcic_") != 0 && !man.VerifyFile(file, "edit")) { rs.Add("FAILED", man.EncryptPath(file.AbsolutePath), man.InvalidFileMsg); this.Cleanup(man, file.ParentFile); return(rs); } targetFile = man.GetFile(file.Parent, (string)input["target"]); if (file.AbsolutePath != targetFile.AbsolutePath) { string fileExt = PathUtils.GetExtension(file.Name).ToLower(); string targetExt = PathUtils.GetExtension(targetFile.Name).ToLower(); if (fileExt != targetExt) { rs.Add("FAILED", man.EncryptPath(targetFile.AbsolutePath), "{#error.invalid_filename}"); this.Cleanup(man, file.ParentFile); return(rs); } if (config.GetBool("filesystem.delete_format_images", false)) { ImageUtils.DeleteFormatImages(targetFile.AbsolutePath, config.Get("edit.format")); } if (targetFile.Exists) { targetFile.Delete(); } file.RenameTo(targetFile); if (config.Get("edit.format") != null) { ImageUtils.FormatImage(targetFile.AbsolutePath, config.Get("edit.format"), config.GetInt("edit.jpeg_quality", 90)); } this.Cleanup(man, file.ParentFile); } rs.Add("OK", man.EncryptPath(file.AbsolutePath), "{#message.save_success}"); return(rs); }
private ResultSet GetMediaInfo(ManagerEngine man, Hashtable input) { ResultSet rs = new ResultSet(new string[] { "name", "path", "url", "size", "type", "created", "modified", "width", "height", "attribs", "next", "prev", "custom" }); BasicFileFilter fileFilter; IFile file, parent; IFile[] files; string prev, next, attribs, url, ext; int width, height; bool match; Hashtable customInfo = new Hashtable(); ManagerConfig config; if (input["url"] != null) { input["path"] = man.ConvertURIToPath(new Uri((string)input["url"]).AbsolutePath); } file = man.GetFile(man.DecryptPath((string)input["path"])); config = file.Config; parent = file.ParentFile; if (parent.IsDirectory) { // Setup file filter fileFilter = new BasicFileFilter(); //fileFilter->setDebugMode(true); fileFilter.IncludeDirectoryPattern = config["filesystem.include_directory_pattern"]; fileFilter.ExcludeDirectoryPattern = config["filesystem.exclude_directory_pattern"]; fileFilter.IncludeFilePattern = config["filesystem.include_file_pattern"]; fileFilter.ExcludeFilePattern = config["filesystem.exclude_file_pattern"]; fileFilter.IncludeExtensions = config["filesystem.extensions"]; fileFilter.OnlyFiles = true; // List files files = parent.ListFilesFiltered(fileFilter); } else { throw new ManagerException("{#error.file_not_exists}"); } match = false; prev = ""; next = ""; // Find next and prev foreach (IFile curFile in files) { if (curFile.AbsolutePath == file.AbsolutePath) { match = true; continue; } else if (!match) { prev = curFile.AbsolutePath; } if (match) { next = curFile.AbsolutePath; break; } } ext = PathUtils.GetExtension(file.Name).ToLower(); // Input default size? MediaInfo size = new MediaInfo(file.AbsolutePath); width = size.Width != -1 ? size.Width : 425; height = size.Height != -1 ? size.Height : 350; // Get custom info man.DispatchEvent(EventType.CustomInfo, file, "info", customInfo); attribs = (file.CanRead && config.GetBool("filesystem.readable", true) ? "R" : "-") + (file.CanWrite && config.GetBool("filesystem.writable", true) ? "W" : "-"); url = PathUtils.RemoveTrailingSlash(config["preview.urlprefix"]) + man.ConvertPathToURI(file.AbsolutePath); rs.Add(file.Name, man.EncryptPath(file.AbsolutePath), url, file.Length, ext, StringUtils.GetDate(file.CreationDate, config.Get("filesystem.datefmt", "yyyy-MM-dd HH:mm")), StringUtils.GetDate(file.LastModified, config.Get("filesystem.datefmt", "yyyy-MM-dd HH:mm")), width, height, attribs, man.EncryptPath(next), man.EncryptPath(prev), customInfo); return(rs); }
private ResultSet CreateDocs(ManagerEngine man, Hashtable input) { ResultSet rs = new ResultSet(new string[] { "status", "fromfile", "tofile", "message" }); IFile fromFile, toFile; ManagerConfig toConfig; string ext; Hashtable fields; for (int i = 0; input["frompath" + i] != null && input["toname" + i] != null; i++) { fromFile = man.GetFile(man.DecryptPath((string)input["frompath" + i])); ext = PathUtils.GetExtension(fromFile.Name); toFile = man.GetFile(man.DecryptPath((string)input["topath" + i]), (string)input["toname" + i] + "." + ext, FileType.File); toConfig = toFile.Config; if (!man.IsToolEnabled("createdoc", toConfig)) { throw new ManagerException("{#error.no_access}"); } if (toConfig.GetBool("general.demo", false)) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#error.demo}"); continue; } if (!fromFile.Exists) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#error.template_missing}"); continue; } if (!man.VerifyFile(toFile, "createdoc")) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), man.InvalidFileMsg); continue; } if (!toFile.CanWrite) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#error.no_write_access}"); continue; } if (!toConfig.GetBool("filesystem.writable", true)) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#error.no_write_access}"); continue; } if (fromFile.CopyTo(toFile)) { // Replace title fields = (Hashtable)input["fields"]; // Replace all fields if (fields != null) { // Read all data StreamReader reader = new StreamReader(toFile.Open(FileMode.Open)); string fileData = reader.ReadToEnd(); reader.Close(); // Replace fields foreach (string name in fields.Keys) { fileData = fileData.Replace("${" + name + "}", (string)fields[name]); } // Write file data StreamWriter writer = new StreamWriter(toFile.Open(FileMode.Create)); writer.Write(fileData); writer.Close(); } rs.Add("OK", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#message.createdoc_success}"); } else { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#error.createdoc_failed}"); } } return(rs); }
private ResultSet MoveFiles(ManagerEngine man, Hashtable input) { ResultSet rs = new ResultSet(new string[] { "status", "fromfile", "tofile", "message" }); IFile fromFile, toFile; ManagerConfig toConfig; FileType fromType; for (int i = 0; input["frompath" + i] != null; i++) { fromFile = man.GetFile(man.DecryptPath((string)input["frompath" + i])); fromType = fromFile.IsFile ? FileType.File : FileType.Directory; if (input["topath" + i] != null) { toFile = man.GetFile(man.DecryptPath((string)input["topath" + i]), "", fromType); } else { if (input["toname" + i] != null) { toFile = man.GetFile(fromFile.Parent, (string)input["toname" + i], fromType); } else { toFile = man.GetFile(man.DecryptPath((string)input["topath"]), fromFile.Name, fromType); } } toConfig = toFile.Config; if (!man.IsToolEnabled("cut", toConfig) && !man.IsToolEnabled("rename", toConfig)) { throw new ManagerException("{#error.no_access}"); } // From file missing if (!fromFile.Exists) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#error.no_from_file}"); continue; } // User tried to change extension if (fromFile.IsFile && PathUtils.GetExtension(fromFile.Name) != PathUtils.GetExtension(toFile.Name)) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#error.move_failed}"); continue; } if (toConfig.GetBool("general.demo", false)) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#error.demo}"); continue; } if (!man.VerifyFile(toFile, "rename")) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), man.InvalidFileMsg); continue; } if (!toFile.CanWrite) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#error.no_write_access}"); continue; } if (!toConfig.GetBool("filesystem.writable", true)) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#error.no_write_access}"); continue; } // To file there if (toFile.Exists) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#error.tofile_exists}"); continue; } // Zip check if (toFile is ZipFileImpl) { if (!man.VerifyFile(fromFile, "zip")) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), man.InvalidFileMsg); continue; } } // Unzip check if (fromFile is ZipFileImpl) { if (!man.VerifyFile(toFile, "unzip")) { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), man.InvalidFileMsg); continue; } } string fromPath = fromFile.AbsolutePath; if (fromFile.RenameTo(toFile)) { rs.Add("OK", man.EncryptPath(fromPath), man.EncryptPath(toFile.AbsolutePath), "{#message.move_success}"); } else { rs.Add("FAILED", man.EncryptPath(fromFile.AbsolutePath), man.EncryptPath(toFile.AbsolutePath), "{#error.move_failed}"); } } return(rs); }