/// <inheritdoc /> public override void Initialize(AnalysisContext context) { context.EnableConcurrentExecution(); context.RegisterCompilationStartAction(compilationStartContext => { var fileSystemContext = new FileSystemContext(compilationStartContext.Compilation); if (ShouldAnalyze(fileSystemContext)) { AnalyzeCompilation(compilationStartContext, fileSystemContext); } }); }
//Получение пути файла private static MdDirectory PathExist(string path) { string[] pathComponents = path.Trim('\\').Split('\\'); FileSystemContext ctx = new FileSystemContext(); string dbPath = $"{path.Trim('\\')}\\"; if (ctx.Directories.Where(d => d.Path == dbPath).Count() == 0) { throw new FileServiceCommandExeption($"{path} not exist"); } else { return(ctx.Directories.Where(d => d.Path == dbPath).FirstOrDefault()); } }
public void CanInsertFileIntoDatabase() { builder.UseInMemoryDatabase("CanInsertFile"); using (var context = new FileSystemContext(builder.Options)) { var file = new File() { DirectoryId = 1 }; context.Files.Add(file); //Check the state of the entity is 'added'. Assert.Equal(EntityState.Added, context.Entry(file).State); } }
public void CanRemoveFileFromDatabase() { builder.UseInMemoryDatabase("CanDeleteFile"); using (var context = new FileSystemContext(builder.Options)) { var file = new File() { Name = "DeleteFile", DirectoryId = 1 }; context.Files.Add(file); context.Files.Remove(file); Assert.Equal(EntityState.Detached, context.Entry(file).State); } }
public void CanInsertSingleDirectory() { //UsInMemoryDatabase() needs a string to be passed in to give a name to that instance //It's how EF Core keeps track of different instances of the provider in memory. builder.UseInMemoryDatabase("InsertSingleDirectory"); using (var context = new FileSystemContext(builder.Options)) { var bizlogic = new BusinessDataLogic(context); bizlogic.MkDir(new Directory()); } using (var context2 = new FileSystemContext(builder.Options)) { Assert.Equal(1, context2.Directories.Count()); } }
public void CanInsertDirectoryIntoDatabase() { using (var context = new FileSystemContext()) { //context.Database.EnsureDeleted(); context.Database.EnsureCreated(); var dir = new Directory(); context.Directories.Add(dir); Debug.WriteLine($"Before save: {dir.Id}"); context.SaveChanges(); Debug.WriteLine($"After save: {dir.Id}"); Assert.NotEqual(0, dir.Id); } }
public void CanRemoveDirectoryFromDatabase() { builder.UseInMemoryDatabase("CanDeleteDirectory"); using (var context = new FileSystemContext(builder.Options)) { var dir = new Directory() { Name = "DeleteDirectory" }; context.Directories.Add(dir); context.Directories.Remove(dir); Assert.Equal(EntityState.Detached, context.Entry(dir).State); } }
//Перемещение файла public static string MoveFile(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 oldDir = 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 == oldDir.Id && f.FileName == filename).FirstOrDefault(); if (currentFile == null) { throw new FileServiceCommandExeption($"{filename} not found"); } else { CheckLock(sessionId, ctx, currentFile); MdFile dubFile = ctx.Files.Where(f => f.IdDirectory == newDir.Id && f.FileName == filename).FirstOrDefault(); if (dubFile != null) { CheckLock(sessionId, ctx, dubFile); ctx.Files.Remove(dubFile); } currentFile.IdDirectory = newDir.Id; ctx.SaveChanges(); return($"{filename} moved from {localSource} to {localDest}"); } }
public void ThrowsExceptionForDuplicateDirectory() { builder.UseInMemoryDatabase("ThrowsException"); using (var context = new FileSystemContext(builder.Options)) { context.Database.EnsureCreated(); var bizlogic = new BusinessDataLogic(context); var dir = new Directory() { Name = "Exception Directory", DirectoryId = 1 }; context.Directories.Add(dir); context.SaveChanges(); Assert.Throws <Exception>(() => bizlogic.AlreadyExists("Exception Directory", 1)); } }
//Создание новой директории public static string CreateDirectory(string sessionId, string path) { InitFileSystem(); string localPath = formatPath(path.ToLower(), sessionId); string[] pathComponents = localPath.Trim('\\').Split('\\'); string dbPath = ""; FileSystemContext ctx = new FileSystemContext(); if (pathComponents.Count() > 1 && drives.ContainsKey(pathComponents[0])) { string tempPath = drives[pathComponents[0]]; string viewPath = pathComponents[0]; for (int i = 1; i < pathComponents.Count() - 1; i++) { viewPath += $"\\{pathComponents[i]}"; dbPath = $"{viewPath}\\"; if (ctx.Directories.Where(d => d.Path == dbPath).Count() == 0) { throw new FileServiceCommandExeption($"{viewPath} not exist"); } } dbPath = $"{viewPath}\\{pathComponents[pathComponents.Count() - 1]}\\"; if (ctx.Directories.Where(d => d.Path == dbPath).Count() > 0) { throw new FileServiceCommandExeption($"{viewPath}\\{pathComponents[pathComponents.Count() - 1]} allready created"); } else { ctx.Directories.Add(new MdDirectory { Path = dbPath }); ctx.SaveChanges(); } return(localPath); } else { throw new FileServiceCommandExeption("Wrong path format"); } }
private void PopulateContent(Dictionary <string, IEnumerable <string> > rv, FileSystemContext contexts, params string[] paths) { var path = Path.Combine(paths); var root = contexts.Roots.Find(GetRoot(path)); if (root == null) { return; } var pathOrig = root.ToActualPath(path); var di = new DirectoryInfo(pathOrig); if (!di.Exists || !di.FullName.StartsWith(root.Path)) //TODO: Forbidden. { return; } var folders = di.GetDirectories() .FilterDirectories(contexts.FolderFilter) .Select(x => root.ToFakePath(x.FullName)) .ToList(); if (folders.Any()) { rv["folders"] = folders.Select(Path.GetFileName).ToList(); } var fp = string.IsNullOrWhiteSpace(contexts.FileSearchPattern) ? "*" : contexts.FileSearchPattern; var files = di.GetFiles(fp, SearchOption.TopDirectoryOnly) .FilterFiles(contexts.FileFilter) .Select(x => root.ToFakePath(x.FullName)) .ToList(); if (files.Any()) { rv["files"] = files.Select(Path.GetFileName).ToList(); } rv["path"] = path.ToPathParts(); }
//Проверка файла на наличие блокировки 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(',')); } }
//Инициализация файловой системы private static void InitFileSystem() { FileSystemContext ctx = new FileSystemContext(); if (ctx.Directories.Where(d => d.Path == "c:\\").Count() == 0) { ctx.Directories.Add(new MdDirectory() { Path = "c:\\" }); ctx.SaveChanges(); } if (ctx.Directories.Where(d => d.Path == "d:\\").Count() == 0) { ctx.Directories.Add(new MdDirectory() { Path = "d:\\" }); ctx.SaveChanges(); } }
//Перемещение директории public static string MoveDir(string sessionId, string source, string dest) { FileSystemContext ctx = new FileSystemContext(); string 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($" destination {localDest} not exist"); } string newPath = $"{localDest}{localSource.Trim('\\').Split('\\')[localSource.Trim('\\').Split('\\').Length - 1]}\\"; MoveMatches(sessionId, ctx, localSource, newPath); return($"{localSource} moved to {localDest}"); }
//Создание файла 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}"); } }
public FileSystemRepository(string key = "") { this.fileSystemContext = new FileSystemContext <T>(key); }
public FileRepository(FileSystemContext fsContext) { _fsContext = fsContext; _files = fsContext.Set <File>(); }
/// <summary> /// Private constructor for implementation Singleton pattern /// </summary> private BundleTransformerContext() { var configContext = new ConfigurationContext(); CoreSettings coreConfig = configContext.GetCoreSettings(); Configuration = configContext; FileSystem = new FileSystemContext(); Styles = new StyleContext(coreConfig.Styles); Scripts = new ScriptContext(coreConfig.Scripts); }
protected abstract void AnalyzeCompilation(CompilationStartAnalysisContext compilationStartContext, FileSystemContext fileSystemContext);
private static bool ShouldAnalyze(FileSystemContext fileSystemContext) => fileSystemContext.HasReference;
//Получение списка директорий public static List <string> Print() { FileSystemContext ctx = new FileSystemContext(); return((from d in ctx.Directories orderby d.Path select d.Path).ToList()); }
public static void ConfigureContext(string appDataFolder) { FileSystemContext.Configure(appDataFolder); FileSystemContext.RepositoriesMapping.Add(typeof(Settings), () => new DefaultRepository <Settings, Settings.Persistent, long>(new DefaultSerializer <List <Settings.Persistent> >($@"{appDataFolder}\Settings\user.dat"))); Log.Debug("User settings repository added", null); }
public FileManager(FileSystemContext context, IFileStore store) { _context = context; _store = store; }
//Копирование директории 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(); }
public BusinessDataLogic() { _context = new FileSystemContext(); }
public FileSystemFileRepository(FileSystemContext context) { _context = context; }
public BusinessDataLogic(FileSystemContext context) { _context = context; }