public async Task <ActionResult> Edit(int id) { try { FileItem file = await db.FileItems.FindAsync(id); if (file == null) { return(RedirectToAction("Index")); } string path = Server.MapPath(string.Concat(file.Path.Replace("ROOT", RootPath), '/', file.Name)); if (!System.IO.File.Exists(path)) { return(RedirectToAction("Index")); } var result = new EditFileItemModel { Id = file.Id, Path = file.Path, Name = file.Name, CDate = file.CDate, MDate = file.MDate }; using (StreamReader sr = new StreamReader(path)) { result.Content = await sr.ReadToEndAsync(); } return(View(result)); } catch (Exception ex) { throw; } }
public async Task <ActionResult> Edit(int id) { try { FileItem file = await db.FileItems.FindAsync(id); if (file == null) { return(RedirectToAction("Index")); } string path = _hosting.ContentRootPath + file.Path.Replace("ROOT", RootPath).Replace("/", "\\") + "\\" + file.Name; if (path.Contains("\\\\")) { path = path.Replace("\\\\", "\\"); } if (!System.IO.File.Exists(path)) { return(RedirectToAction("Index")); } var result = new EditFileItemModel { Id = file.Id, Path = file.Path, Name = file.Name, CDate = file.CDate, MDate = file.MDate }; using (StreamReader sr = new StreamReader(path)) { result.Content = await sr.ReadToEndAsync(); } return(View(result)); } catch (Exception ex) { return(Json(new OperationResult { Status = OperationStats.Success, Message = StringResources.CodeError })); } }
public async Task <ActionResult> Rename(EditFileItemModel model) { try { FileItem file = await db.FileItems.FindAsync(model.Id); if (file == null) { return(Json(new OperationResult { Status = OperationStats.Error, Message = StringResources.NotFoundInDatabase, })); } string absPath = Server.MapPath(string.Concat(file.Path.Replace("ROOT", RootPath), '/', file.Name)); if (model.IsFolder) { if (!Directory.Exists(absPath)) { return(Json(new OperationResult { Status = OperationStats.Error, Message = StringResources.NotFoundInFileSystem, })); } // Rename the name of current directory // Rename in File System Directory.Move(absPath, RenameFileOrDirectory(absPath, model.Name)); // Rename in Database file.Name = model.Name; file.MDate = DateTime.UtcNow; await db.SaveChangesAsync(); // Change sub directory and file pathes await UpdateSubDirectoryPath(await db.FileItems.Where(x => x.FileId != null && x.FileId == file.Id).ToListAsync()); } else { if (!System.IO.File.Exists(absPath)) { return(Json(new OperationResult { Status = OperationStats.Error, Message = StringResources.NotFoundInFileSystem, })); } // Rename in File System System.IO.File.Move(absPath, RenameFileOrDirectory(absPath, model.Name)); // Rename in Database file.Name = model.Name; file.MDate = DateTime.UtcNow; await db.SaveChangesAsync(); } await db.SaveChangesAsync(); return(Json(new OperationResult { Status = OperationStats.Success, Message = StringResources.NameChanged, })); } catch (Exception ex) { throw; } }