//удаление директории и всех директорий, входящих в неё public static string DeleteTreeDirectory(string sessionId, string path) { string localPath = formatPath(path.ToLower(), sessionId); IsContainCurDirectory(sessionId, localPath); FileSystemContext ctx = new FileSystemContext(); if (ctx.Directories.Where(d => d.Path == localPath).Count() == 0) { throw new FileServiceCommandExeption($"{localPath} not exist"); } else { MdDirectory localDir = ctx.Directories.Where(d => d.Path == localPath).FirstOrDefault(); ctx.Directories.Where(d => d.Path.Contains(localPath)).ToList().ForEach(d => { ctx.Files.Where(f => f.IdDirectory == d.Id).ToList().ForEach(f => { CheckLock(sessionId, ctx, f); ctx.Files.Remove(f); }); ctx.Directories.Remove(d); }); ctx.Files.Where(f => f.IdDirectory == localDir.Id).ToList().ForEach(f => { CheckLock(sessionId, ctx, f); ctx.Files.Remove(f); }); ctx.Directories.Remove(ctx.Directories.Where(d => d.Path == localPath).FirstOrDefault()); ctx.SaveChanges(); } return(localPath.Trim('\\')); }
//Получение списка файлов в директории public static List <string> PrintFiles(string path, string sessionId) { FileSystemContext ctx = new FileSystemContext(); List <string> result = new List <string>(); MdDirectory dir = PathExist(path); ctx.Files.Where(f => f.IdDirectory == dir.Id).ToList().ForEach(f => { string lockedBy = ""; if (ctx.Locks.Where(l => l.IdFile == f.Id).Count() > 0) { lockedBy = "locked by "; ctx.Locks.Where(l => l.IdFile == f.Id).ToList().ForEach(l => { if (l.User == FileServiceSession.GetUser(sessionId)) { lockedBy += "Me,"; } else { lockedBy += $"{l.User},"; } }); lockedBy = $"[{lockedBy.Trim(',')}]"; } result.Add($"{dir.Path}{f.FileName}{lockedBy}"); }); return(result); }
//Разблокировка файла public static string UnlockFile(string sessionId, string path, string filename) { string user = FileServiceSession.GetUser(sessionId); string localPath = path; FileSystemContext ctx = new FileSystemContext(); if (path == "") { localPath = GetPath(sessionId); } string fullFilename = $"{localPath}{filename}"; MdDirectory localDir = PathExist(localPath); if (ctx.Files.Where(f => f.IdDirectory == localDir.Id && f.FileName == filename).Count() > 0) { MdFile localFile = ctx.Files.Where(f => f.IdDirectory == localDir.Id && f.FileName == filename).FirstOrDefault(); if (ctx.Locks.Where(l => l.IdFile == localFile.Id && l.User == user).Count() > 0) { ctx.Locks.Remove(ctx.Locks.Where(l => l.IdFile == localFile.Id && l.User == user).FirstOrDefault()); ctx.SaveChanges(); } return($"{localPath}{filename}"); } else { throw new FileServiceCommandExeption($"{filename} not exist in {localPath}"); } }
//Переместить директорию private static void MoveMatches(string sessionId, FileSystemContext ctx, string oldPath, string newPath) { List <MdDirectory> oldList = ctx.Directories.Where(d => d.Path.Contains(oldPath)).ToList(); oldList.ForEach(d => { List <string> fileSet = new List <string>(); ctx.Files.Where(f => f.IdDirectory == d.Id).ToList().ForEach(f => { CheckLock(sessionId, ctx, f); fileSet.Add(f.FileName); }); string path = d.Path.Replace(oldPath, newPath); MdDirectory newDir = ctx.Directories.Where(nd => nd.Path == path).FirstOrDefault(); if (newDir != null) { List <MdFile> newDirFiles = ctx.Files.Where(f => f.IdDirectory == newDir.Id).ToList(); newDirFiles.ForEach(f => { if (fileSet.IndexOf(f.FileName) >= 0) { CheckLock(sessionId, ctx, f); ctx.Files.Remove(f); } else { f.IdDirectory = d.Id; } }); ctx.Directories.Remove(newDir); } d.Path = path; }); ctx.SaveChanges(); }
//Копирование файла public static string CopyFile(string sessionId, string filename, string source, string dest) { FileSystemContext ctx = new FileSystemContext(); string localSource = ""; if (source == "") { localSource = GetPath(sessionId); } else { localSource = formatPath(source.ToLower(), sessionId); } if (ctx.Directories.Where(d => d.Path == localSource).Count() == 0) { throw new FileServiceCommandExeption($" source {localSource} not exist"); } string localDest = formatPath(dest.ToLower(), sessionId); if (ctx.Directories.Where(d => d.Path == localDest).Count() == 0) { throw new FileServiceCommandExeption($" destinationw {localDest} not exist"); } MdDirectory copyDir = ctx.Directories.Where(d => d.Path == localSource).FirstOrDefault(); MdDirectory newDir = ctx.Directories.Where(d => d.Path == localDest).FirstOrDefault(); MdFile currentFile = ctx.Files.Where(f => f.IdDirectory == copyDir.Id && f.FileName == filename).FirstOrDefault(); if (currentFile == null) { throw new FileServiceCommandExeption($"{filename} not found"); } else { MdFile dubFile = ctx.Files.Where(f => f.IdDirectory == newDir.Id && f.FileName == filename).FirstOrDefault(); if (dubFile != null) { CheckLock(sessionId, ctx, dubFile); dubFile.Content = currentFile.Content.ToArray(); } else { ctx.Files.Add(new MdFile() { FileName = currentFile.FileName, IdDirectory = newDir.Id, Content = currentFile.Content.ToArray() }); } ctx.SaveChanges(); return($"{filename} copied from {localSource} to {localDest}"); } }
//Проверка файла на наличие блокировки private static void CheckLock(string sessionId, FileSystemContext ctx, MdFile filename) { if (ctx.Locks.Where(l => l.IdFile == filename.Id).Count() > 0) { string user = FileServiceSession.GetUser(sessionId); MdDirectory dir = ctx.Directories.Where(d => d.Id == filename.IdDirectory).FirstOrDefault(); string error = $"{dir.Path}{filename.FileName} locked by "; ctx.Locks.Where(l => l.IdFile == filename.Id ).ToList().ForEach(l => { if (user == l.User) { error += "Me,"; } else { error += $"{l.User},"; } }); throw new FileServiceCommandExeption(error.Trim(',')); } }
//Создание файла public static string CreateFile(string sessionId, string path, string filename) { FileSystemContext ctx = new FileSystemContext(); string localPath = path; if (path == "") { localPath = GetPath(sessionId); } else { localPath = formatPath(path.ToLower(), sessionId); } MdDirectory localDir = PathExist(localPath); if (!CheckFilenameFormat(filename)) { throw new FileServiceCommandExeption("Bad filename format"); } string fullFilename = $"{localPath}{filename}"; if (ctx.Files.Where(f => f.IdDirectory == localDir.Id && f.FileName == filename).Count() > 0) { throw new FileServiceCommandExeption($"{filename} already created in {localPath}"); } else { FileStream fs = File.Create(Path.GetTempFileName()); int length = Convert.ToInt32(fs.Length); byte[] data = new byte[length]; fs.Read(data, 0, length); fs.Close(); ctx.Files.Add(new MdFile() { IdDirectory = localDir.Id, FileName = filename, Content = data }); ctx.SaveChanges(); return($"{localPath}{filename}"); } }
//Удаление файла public static string DeleteFile(string sessionId, string path, string filename) { string localPath = path; FileSystemContext ctx = new FileSystemContext(); if (path == "") { localPath = GetPath(sessionId); } MdDirectory dir = PathExist(localPath); string fullFilename = $"{localPath}{filename}"; if (ctx.Files.Where(f => f.IdDirectory == dir.Id && f.FileName == filename).Count() > 0) { CheckLock(sessionId, ctx, ctx.Files.Where(f => f.IdDirectory == dir.Id && f.FileName == filename).FirstOrDefault()); ctx.Files.Remove(ctx.Files.Where(f => f.IdDirectory == dir.Id && f.FileName == filename).FirstOrDefault()); ctx.SaveChanges(); return($"{localPath}{filename}"); } else { throw new FileServiceCommandExeption($"{filename} not exist in {localPath}"); } }
//Копирование директории private static void CopyMatches(string sessionId, FileSystemContext ctx, string copyPath, string newPath) { List <MdDirectory> copyList = ctx.Directories.Where(d => d.Path.Contains(copyPath)).ToList(); copyList.ForEach(d => { List <string> fileSet = new List <string>(); List <MdFile> fileList = ctx.Files.Where(f => f.IdDirectory == d.Id).ToList(); fileList.ForEach(f => { fileSet.Add(f.FileName); }); string path = d.Path.Replace(copyPath, newPath); MdDirectory newDir = ctx.Directories.Where(nd => nd.Path == path).FirstOrDefault(); if (newDir != null) { List <MdFile> newDirFiles = ctx.Files.Where(f => f.IdDirectory == newDir.Id).ToList(); newDirFiles.ForEach(f => { if (fileSet.IndexOf(f.FileName) >= 0) { CheckLock(sessionId, ctx, f); } }); } }); copyList.ForEach(d => { List <string> fileSet = new List <string>(); List <MdFile> fileList = ctx.Files.Where(f => f.IdDirectory == d.Id).ToList(); fileList.ForEach(f => { fileSet.Add(f.FileName); }); string path = d.Path.Replace(copyPath, newPath); MdDirectory newDir = ctx.Directories.Where(nd => nd.Path == path).FirstOrDefault(); if (newDir != null) { List <MdFile> newDirFiles = ctx.Files.Where(f => f.IdDirectory == newDir.Id).ToList(); newDirFiles.ForEach(f => { if (fileSet.IndexOf(f.FileName) >= 0) { MdFile copyFile = fileList.Where(cf => cf.FileName == f.FileName).FirstOrDefault(); f.Content = copyFile.Content.ToArray(); fileSet.Remove(f.FileName); } }); fileList.ForEach(f => { if (fileSet.IndexOf(f.FileName) >= 0) { ctx.Files.Add(new MdFile() { FileName = f.FileName, IdDirectory = newDir.Id, Content = f.Content.ToArray() }); } }); } else { MdDirectory copyDir = new MdDirectory() { Path = path }; ctx.Directories.Add(copyDir); ctx.SaveChanges(); fileList.ForEach(f => { ctx.Files.Add(new MdFile() { FileName = f.FileName, IdDirectory = copyDir.Id, Content = f.Content.ToArray() }); }); } }); ctx.SaveChanges(); }