//Получение списка файлов в директории 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}"); } }
public string Run(string sessionId, params string[] args) { if (args.Count() > 1) { FileServiceSession.SetUser(sessionId, args[1]); VirtualFileSystem.SetPath(sessionId, "C:"); return(FileServiceSession.GetUser(sessionId)); } else { throw new FileServiceCommandExeption("Missed user name"); } }
//Проверка файла на наличие блокировки 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 string RunCommand(string cmd) { string fsCommand = cmd.Split(' ')[0]; string[] args = cmd.Split(' ').Skip(1).ToArray(); try { IFileServiceCommand fsCommandObj = FileServiceCommands.NewCommand(fsCommand.ToLower()); string answer = fsCommandObj.Run(OperationContext.Current.SessionId, args); OperationContext.Current.GetCallbackChannel <IFileServiceCallback>().Notify(FileServiceSession.GetUser(OperationContext.Current.SessionId) + " " + fsCommandObj.Notify(args)); return(answer); } catch (FileServiceCommandExeption e) { return($"Error: {e.Message}"); } finally { if (Monitor.IsEntered(VirtualFileSystem.Lock)) { Monitor.Exit(VirtualFileSystem.Lock); } } }
public void Logout() { FileServiceSession.DeleteUser(OperationContext.Current.SessionId); }