private List <string> GetSaveAndShowNames(EnumList.MatterixFileType fileType, string objectReference, string fileName) { //ToDo :: Reuse this function in all file adding to file system //Return value from this function is: func[0] (Unique saved name), func[1] (Given name to show or download) //ToDo :: Take in the full name of file with .ex var random = Guid.NewGuid().ToString(); var extension = fileName.Split(".")[1]; switch (fileType) { case EnumList.MatterixFileType.ProfilePicture: break; case EnumList.MatterixFileType.CoursePicture: break; case EnumList.MatterixFileType.CourseBackground: break; case EnumList.MatterixFileType.LectureFile: break; case EnumList.MatterixFileType.Homework: break; case EnumList.MatterixFileType.JobApplicationCv: break; case EnumList.MatterixFileType.MatterixPlusApproval: return(new List <string> { $"{objectReference} - {random.Split("-")[0]}.{extension}", $"{objectReference} - {fileName}" }); break; default: return(new List <string> { "", "" }); } return(new List <string> { "", "" }); }
private void CreateDirectories(EnumList.MatterixFileType fileType) { //ToDo :: Reuse this function in all file adding to file system var directories = new List <string>(); switch (fileType) { case EnumList.MatterixFileType.ProfilePicture: break; case EnumList.MatterixFileType.CoursePicture: break; case EnumList.MatterixFileType.CourseBackground: break; case EnumList.MatterixFileType.LectureFile: break; case EnumList.MatterixFileType.Homework: break; case EnumList.MatterixFileType.JobApplicationCv: break; case EnumList.MatterixFileType.MatterixPlusApproval: directories = new List <string> { PlusApplications, Admissions }; break; default: return; } var path = $"{_environment.ContentRootPath}/wwwroot/Files"; foreach (var dir in directories) { if (Directory.GetDirectories(path, dir).Length == 0) { Directory.CreateDirectory(Path.Combine($"{path}/{dir}")); } path += $"/{dir}"; } }
public string SaveFileToSystem(IFormFile file, EnumList.MatterixFileType type, string objectReference = "", string oldName = "") { //ToDo :: Check if replaceable, delete the old one and create new | Maybe later when editing all files var names = GetSaveAndShowNames(type, objectReference, file.FileName); CreateDirectories(type); if (string.IsNullOrEmpty(names[0]) || string.IsNullOrEmpty(names[1])) { return(string.Empty); } var sizeMb = (decimal)(file.Length / 1024.0 / 1024); var matterixFile = new MatterixFile { FileName = names[0], DisplayName = names[1], ContentType = file.ContentType, MbSize = Math.Round(sizeMb, 2), RootPath = GetRequiredPath(type, objectReference, names[0])[0], WebPath = GetRequiredPath(type, objectReference, names[0])[1] }; //ToDo :: Try save file to system, if success save matterixFile to database and return its Id try { using (var stream = new FileStream(matterixFile.RootPath, FileMode.Create)) { file.CopyToAsync(stream).Wait(); } _context.Add(matterixFile); _context.SaveChanges(); return(matterixFile.Id); } catch (Exception e) { Console.WriteLine(e); Console.WriteLine($"Error saving file of {type.ToString()} with reference of {objectReference}"); return(string.Empty); } }
private List <string> GetRequiredPath(EnumList.MatterixFileType fileType, string objectReference, string fileName) { //ToDo :: Reuse this function in all file adding to file system var rootPath = ""; var webPath = ""; switch (fileType) { case EnumList.MatterixFileType.ProfilePicture: break; case EnumList.MatterixFileType.CoursePicture: break; case EnumList.MatterixFileType.CourseBackground: break; case EnumList.MatterixFileType.LectureFile: break; case EnumList.MatterixFileType.Homework: break; case EnumList.MatterixFileType.JobApplicationCv: break; case EnumList.MatterixFileType.MatterixPlusApproval: rootPath = Path.Combine(_environment.ContentRootPath, "wwwroot", "Files", PlusApplications, Admissions, fileName); webPath = $"~/Files/{PlusApplications}/{Admissions}/{fileName}"; break; default: break; } return(new List <string> { rootPath, webPath }); }
private List <string> AllowedType(EnumList.MatterixFileType fileType) { //ToDo :: Reuse this function in all file adding to file system var allowedType = new List <string>(); switch (fileType) { case EnumList.MatterixFileType.ProfilePicture: break; case EnumList.MatterixFileType.CoursePicture: break; case EnumList.MatterixFileType.CourseBackground: break; case EnumList.MatterixFileType.LectureFile: break; case EnumList.MatterixFileType.Homework: break; case EnumList.MatterixFileType.JobApplicationCv: break; case EnumList.MatterixFileType.MatterixPlusApproval: allowedType = new List <string> { "application/vnd.oasis.opendocument.text", "application/octet-stream", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/pdf", }; break; default: break; } return(allowedType); }
private int AllowedSize(EnumList.MatterixFileType fileType) { //ToDo :: Reuse this function in all file adding to file system var allowedSize = 0; switch (fileType) { case EnumList.MatterixFileType.ProfilePicture: break; case EnumList.MatterixFileType.CoursePicture: break; case EnumList.MatterixFileType.CourseBackground: break; case EnumList.MatterixFileType.LectureFile: break; case EnumList.MatterixFileType.Homework: break; case EnumList.MatterixFileType.JobApplicationCv: break; case EnumList.MatterixFileType.MatterixPlusApproval: allowedSize = 25; break; default: allowedSize = 0; break; } return(allowedSize * 1024 * 1024); }
public bool IsTypeAllowed(EnumList.MatterixFileType fileType, string contentType) { return(AllowedType(fileType).Contains(contentType)); }
public bool IsSizeAllowed(EnumList.MatterixFileType fileType, long size) { return(size <= AllowedSize(fileType)); }