public static bool HasAccessToFolder(string fullPath, IHostingEnvironment env) { DirectoryInfo[] directories = env.GetStorageDirectoriesInfo(); foreach (var dir in directories) { if (fullPath.Contains(dir.Path, StringComparison.OrdinalIgnoreCase) && !dir.CanOpen) { return(false); } } return(true); }
private static IEnumerable <FileManagerObject> GetFolders(string path, IHostingEnvironment env) { // Без проверки на существование директории path, т.к. этот метод работает в паре с GetFilesAndFolders return(Directory.GetDirectories(path) .Where(directory => { if (!directory[directory.Length - 1].Equals('/')) { directory = directory.Insert(directory.Length, "/"); } return HasAccessToFolder(directory, env); }) .Select(directory => { string shortPathToFolder = directory.Substring(env.GetStorageFolderFullPath().Length); if (shortPathToFolder[shortPathToFolder.Length - 1].Equals('/')) { shortPathToFolder = shortPathToFolder.Substring(0, shortPathToFolder.Length - 1); } string folderName = shortPathToFolder.Substring(shortPathToFolder.LastIndexOf('/') + 1); shortPathToFolder = shortPathToFolder.Replace('/', '>'); bool canDelete = true; if (!directory[directory.Length - 1].Equals('/')) { directory = directory.Insert(directory.Length, "/"); } foreach (var directoryInfo in env.GetStorageDirectoriesInfo()) { if (directory.Equals(directoryInfo.Path, StringComparison.OrdinalIgnoreCase)) { canDelete = false; break; } } return new FileManagerObject { Name = folderName, ShortPath = shortPathToFolder, Type = FileManagerObjectType.Folder, CanDelete = canDelete }; }).OrderBy(o => o.Name)); }
public static void DeleteFileOrFolder(CMSDatabase db, string path, HttpContext context, out string redirectPath) { IHostingEnvironment env = context.RequestServices.GetService <IHostingEnvironment>(); Regex regex = new Regex(@"^((\w|-|_)+)(>(\w|-|_)+)*(\.\w+)?$"); if (!regex.IsMatch(path)) { redirectPath = null; return; } string fileOrFolderFullName = path.Substring(path.LastIndexOf('>') + 1); redirectPath = path = path.Substring(0, path.Length - fileOrFolderFullName.Length); if (!string.IsNullOrEmpty(path)) { path = path.Replace('>', '/'); if (!path[path.Length - 1].Equals('/')) { path = path.Insert(path.Length, "/"); } if (redirectPath[redirectPath.Length - 1].Equals('>')) { redirectPath = redirectPath.Substring(0, redirectPath.Length - 1); } } path = $"{env.GetStorageFolderFullPath()}{path}"; if (!Directory.Exists(path) || !HasAccessToFolder(path, env)) { redirectPath = null; return; } FileManagerObjectType?type = null; int pointIndex = fileOrFolderFullName.LastIndexOf('.'); if (pointIndex == -1) { type = FileManagerObjectType.Folder; } else { string fileExtension = fileOrFolderFullName.Substring(pointIndex); foreach (var typeOfExtension in typesOfExtensions) { if (fileExtension.Equals(typeOfExtension.Key)) { type = typeOfExtension.Value; break; } } if (!type.HasValue) { redirectPath = null; return; } for (int i = 0; i < pointIndex; ++i) { bool correctSymbol = false; foreach (var symbol in availableSymbolsInName) { if (fileOrFolderFullName[i].Equals(symbol)) { correctSymbol = true; break; } } if (!correctSymbol) { redirectPath = null; return; } } } if (type != FileManagerObjectType.Folder) { string pathToFile = $"{path}{fileOrFolderFullName}"; if (File.Exists(pathToFile)) { if (type == FileManagerObjectType.Image) { ImagesManagementFunctions.DeleteImage(path, fileOrFolderFullName, db, env); } else { File.Delete(pathToFile); } } else { redirectPath = null; } LogManagementFunctions.AddAdminPanelLog( db: db, context: context, info: $"{pathToFile.Substring(env.GetStorageFolderFullPath().Length - 1)}: {(context.Items["LogLocalization"] as IAdminPanelLogLocalization)?.FileDeleted}" ); } else { string pathToFolder = $"{path}{fileOrFolderFullName}/"; if (!Directory.Exists(pathToFolder)) { redirectPath = null; return; } foreach (var dir in env.GetStorageDirectoriesInfo()) { if (pathToFolder.Equals(dir.Path, StringComparison.OrdinalIgnoreCase)) { redirectPath = null; return; } } Directory.Delete(pathToFolder, true); LogManagementFunctions.AddAdminPanelLog( db: db, context: context, info: $"{pathToFolder.Substring(env.GetStorageFolderFullPath().Length - 1)}: {(context.Items["LogLocalization"] as IAdminPanelLogLocalization)?.FolderDeleted}" ); } }