// Delete a file from the database public void DeleteFile(int id) { LMSFile file = db.Files.Find(id); db.Files.Remove(file); db.SaveChanges(); }
public ActionResult Upload(int courseId = 0, bool isPublic = false) { UserManager <ApplicationUser> userManager = LMSRepo.GetUserManager(); LMSFile dbFile = new LMSFile(); ApplicationUser uploader = userManager.FindById(User.Identity.GetUserId()); string uploaderName = uploader.RealName; //string courseName = LMSRepo.GetCourseByID(courseID).CourseName; string courseName = "Def"; string location = "~/Content/Uploads/Shared/"; //Creates a slugs for the course name and the uploader name courseName = ExtensionClass.GenerateSlug(courseName); uploaderName = ExtensionClass.GenerateSlug(uploaderName); //If the file is not public, set it in a user personal folder if (!isPublic) { location = "~/Content/Uploads/" + courseName + "/" + uploaderName + "/"; var directoryPath = Path.Combine(Server.MapPath(location), ""); //Converts the folder location in absolute path //if the folder does not exist, create a new one if (!Directory.Exists(directoryPath)) { //Try if the location is writable try { DirectoryInfo di = Directory.CreateDirectory(directoryPath); } catch (Exception) { //To-Do: Set proper catch block ViewBag.ErrorMessage = "Unable to create folder at this location. Make sure the location is writable."; } } } if (Request.Files.Count <= 0) { return(RedirectToAction("Index")); } var file = Request.Files[0]; if (file == null || file.ContentLength <= 0) { return(RedirectToAction("Index")); } var fileSize = file.ContentLength; var fileFormat = file.ContentType; var fileName = file.FileName; //Sluggish the file name and trim length //fileName = ExtensionClass.GenerateSlug(fileName); var path = Path.Combine(Server.MapPath(location), fileName); //Verifies if the file has already been uploaded in the same directory var existingFilesWithSameName = LMSRepo.GetAllFiles().Where(f => f.Name == fileName || f.Name.Replace(" - copy", "") == fileName && f.URL == path).ToList(); if (existingFilesWithSameName.Count > 0) { var existingFilesWithSameNameAndSize = existingFilesWithSameName.Where(ef => ef.Size == fileSize).ToList(); if (existingFilesWithSameNameAndSize.Count > 0) //Found file with same name and size { var existingFilesWithSameNameAndSizeAndFormat = existingFilesWithSameNameAndSize .Where(ef => ef.Format == fileFormat).ToList(); if (existingFilesWithSameNameAndSizeAndFormat.Count > 0) //Exact same file found { return(null); //To-Do: proper handling } else //File with same name and size found but different format: Change name { fileName += " - copy"; } } else //If same names but different sizes, Change upload name, add - copy { fileName += " - copy"; } } //Add file information in the file object to be saved in the database dbFile.Name = fileName; dbFile.Size = fileSize; dbFile.Uploader = uploader; dbFile.Format = fileFormat; dbFile.IsPublicVisible = isPublic; dbFile.UploadDate = DateTime.Now; dbFile.URL = path; //dbFile.Course = new Course(); //Add file to the database LMSRepo.AddFile(dbFile); //Moves the file to the server file.SaveAs(path); return(RedirectToAction("Index")); }
// Edit a file in the database public void EditFile(LMSFile file) { db.Entry(file).State = EntityState.Modified; db.SaveChanges(); }
// Add a file to the database public void AddFile(LMSFile file) { db.Files.Add(file); db.SaveChanges(); }