/// <summary> /// Checking if the user is allowed to manipulate the file. /// </summary> private bool IsAllowedToManipulateFile(FILE file) { if (this.IsEmployee || file.USERS_ID == this.DbUser.ID) { return true; } return false; }
public ActionResult Upload() { var errors = new List<string>(); // If there are files sent to the server. if (this.Request.Files.Count > 0) { HttpPostedFileBase uploadFile = this.Request.Files[0]; // Check if the file is not null and that the file contains data. if (uploadFile != null && uploadFile.ContentLength > 0) { string extension = Path.GetExtension(uploadFile.FileName); string filenameWithoutExtension = Path.GetFileNameWithoutExtension(uploadFile.FileName); string filename = filenameWithoutExtension + "_" + DateTime.Now.ToFileTime() + extension; string path = Path.Combine(this.Server.MapPath("~/Files/"), filename); uploadFile.SaveAs(path); FILES_CATEGORIES fileCategory = this.Db.FILES_CATEGORIES.Find(int.Parse(this.Request.Form["CATEGORY"])); // Sets the file entity. var file = new FILE(); file.USERS_ID = this.DbUser.ID; file.FILES_CATEGORIES_ID = fileCategory != null ? fileCategory.ID : -1; file.NAME = this.Request.Form["NAME"].Trim(); file.DESCRIPTION = this.Request.Form["DESCRIPTION"].Trim(); file.DIRECTORIES_ID = 1; file.DOWNLOADS = 0; file.PRIVATE = this.Request.Form["PRIVATE"] != null ? 1 : 0; file.EXTENSION = extension.ToLower(); file.FILESIZE = uploadFile.ContentLength; file.IMGINDEX = 1; file.PATH = filename; file.CREATED = DateTime.Now; if (file.NAME.Length < 1) { errors.Add("Incorrecte naam."); } if (file.FILES_CATEGORIES_ID == -1) { errors.Add("Incorrecte categorie."); } if (errors.Count == 0) { // Save new file to the database. this.Db.FILES.Add(file); this.Db.SaveChanges(); return this.RedirectToAction("Index"); } } else { errors.Add("Bestand niet correct geupload."); } } else { errors.Add("Geen bestand geupload."); } if (errors.Count > 0) { this.SetFlash( "De volgende fouten zijn opgetreden: <ul>" + string.Join(string.Empty, errors.ConvertAll(x => "<li>" + x + "</li>")) + "</ul>"); } return this.View("Create"); }