public ActionResult Index() { db.Folders.RemoveRange(db.Folders); db.FileSystemItems.RemoveRange(db.FileSystemItems); db.SaveChanges(); return(View()); }
//[ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "FileDataDetailId,FileDataId,FilePath,Status,UploadTime")] FileDataDetail fileDataDetail, FileDataDetail fDD, HttpPostedFileBase fileupload) { if (fileupload.ContentLength > 0) { string _FileName = Path.GetFileName(fileupload.FileName); string extension = Path.GetExtension(fileupload.FileName); _FileName = fDD.FileDataId + "-" + fDD.Status + extension; string _path = Path.Combine(Server.MapPath("~/App_Data/uploads"), _FileName); fDD.FilePath = _path; fDD.UploadTime = DateTime.Now.ToString("MM/dd/yyyy hh:mm tt"); fileupload.SaveAs(fDD.FilePath); } try { if (ModelState.IsValid) { db.FileDataDetails.Add(fDD); db.SaveChanges(); return(RedirectToAction("Index", new { filedataid = fDD.FileDataId })); } } catch (Exception ex) { TempData["Error"] = ex.Message; } return(View()); }
public int MkDir(Directory directory) { _context.Directories.Add(directory); var dbResult = _context.SaveChanges();//returns number of rows affected return(dbResult); }
public long Create(File newFile) { newFile.Create(); _files.Add(newFile); _fsContext.SaveChanges(); return(newFile.ID); }
internal Directory CreateRootDirectory(Directory root) { _context.Directories.Add(root); //root is now being tracked _context.SaveChanges(); return(root); }
public ActionResult Create([Bind(Include = "Id,Username,Password,UserType,CurrentSO,PreviousSO,Dateofjoining")] Login login) { if (ModelState.IsValid) { db.Logins.Add(login); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(login)); }
internal void Setup() { var dir = _context.Directories.Where(d => d.Name == "root" && d.Id == 1).FirstOrDefault(); if (dir == null) { root = new Directory() { Name = "root" }; _context.Directories.Add(root); _context.SaveChanges(); } }
//удаление директории и всех директорий, входящих в неё 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 ActionResult ChangePassword([Bind(Include = "Id,Username,Password,UserType")] Login checkuser, string changepassword, string confirmpassword) { using (FileSystemContext db = new FileSystemContext()) { var DoesUserExist = db.Logins.Where(x => x.Username == checkuser.Username && x.Password == checkuser.Password).FirstOrDefault(); if (DoesUserExist != null) { if (changepassword == confirmpassword) { checkuser.Password = confirmpassword; DoesUserExist.Password = checkuser.Password; db.Entry(DoesUserExist).State = EntityState.Modified; db.SaveChanges(); TempData["Msg"] = "Password Change Successfully."; return(RedirectToAction("Login", "Account")); } else { TempData["Message"] = "Confirm Password Do Not Match."; return(View()); } } else { TempData["Message"] = "Username or Password Does Not Match or Exist."; return(View()); } } }
//Переместить директорию 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 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 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 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}"); } }
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); } }
//[ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "Id,Date,Filename,Filenumber,Subject,Type,Givennumber,Pages,Addressee,Sectionoforigin,Receivedby,Status,Pdfdirectory")] FileData fileData, FileData FD, HttpPostedFileBase file, FileDataDetail filedatadetail) { FD.Receivedby = User.Identity.Name.ToString(); if (file.ContentLength > 0) { string _FileName = Path.GetFileName(file.FileName); string extension = Path.GetExtension(file.FileName); _FileName = FD.FileDataId + "-" + FD.Filename + FD.Status + extension; string _path = Path.Combine(Server.MapPath("~/App_Data/uploads"), _FileName); string uploadedfilename; uploadedfilename = _path; FD.Pdfdirectory = _FileName; // setting values for filedatadetails values filedatadetail.FileDataId = FD.FileDataId; filedatadetail.FilePath = _path; filedatadetail.Status = FD.Status; filedatadetail.UploadTime = DateTime.Now.ToString("MM/dd/yyyy hh:mm tt"); file.SaveAs(uploadedfilename); } try { if (ModelState.IsValid) { db.FileDatas.Add(FD); db.FileDataDetails.Add(filedatadetail); db.SaveChanges(); TempData["Msg"] = "Data Successfully Added"; return(RedirectToAction("Create")); } } catch (Exception ex) { TempData["mess"] = ex.Message; } return(View()); }
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"); } }
//Создание файла 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(); }
internal void Touch(File file) { _context.Files.Add(file); _context.SaveChanges(); }