private bool DeleteFile(MatterixFile file) { try { File.Delete(file.RootPath); return(true); } catch (Exception e) { Console.WriteLine(e); return(false); } }
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); } }