From 11af3f2f6b9c553c97ad5812f0ce3aa4fb1cdbb8 Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Mon, 4 Feb 2019 20:45:53 -0800 Subject: [PATCH] Changed paste file deletion to after successful saving of new file. --- Teknik/Areas/Paste/Controllers/PasteController.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Teknik/Areas/Paste/Controllers/PasteController.cs b/Teknik/Areas/Paste/Controllers/PasteController.cs index 0a56c1c..aa7f865 100644 --- a/Teknik/Areas/Paste/Controllers/PasteController.cs +++ b/Teknik/Areas/Paste/Controllers/PasteController.cs @@ -333,11 +333,9 @@ namespace Teknik.Areas.Paste.Controllers } } - // Delete the old file + // get the old file string subDir = paste.FileName[0].ToString(); - string filePath = Path.Combine(_config.PasteConfig.PasteDirectory, subDir, paste.FileName); - if (System.IO.File.Exists(filePath)) - System.IO.File.Delete(filePath); + string oldFile = Path.Combine(_config.PasteConfig.PasteDirectory, subDir, paste.FileName); // Generate a unique file name that does not currently exist string newFilePath = FileHelper.GenerateRandomFileName(_config.PasteConfig.PasteDirectory, _config.PasteConfig.FileExtension, 10); @@ -363,6 +361,10 @@ namespace Teknik.Areas.Paste.Controllers _dbContext.Entry(paste).State = EntityState.Modified; _dbContext.SaveChanges(); + // Delete the old file + if (System.IO.File.Exists(oldFile)) + System.IO.File.Delete(oldFile); + return Redirect(Url.SubRouteUrl("p", "Paste.View", new { type = "Full", url = paste.Url })); } }