From 372997f9958c1658c6ca669648414dba637ae8ec Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Tue, 5 Feb 2019 00:00:50 -0800 Subject: [PATCH] Fixed paste and upload file deletion --- .../Paste/Controllers/PasteController.cs | 60 ++++++----------- .../Upload/Controllers/UploadController.cs | 67 ++++++------------- 2 files changed, 43 insertions(+), 84 deletions(-) diff --git a/Teknik/Areas/Paste/Controllers/PasteController.cs b/Teknik/Areas/Paste/Controllers/PasteController.cs index cb5b6be..f66fdcd 100644 --- a/Teknik/Areas/Paste/Controllers/PasteController.cs +++ b/Teknik/Areas/Paste/Controllers/PasteController.cs @@ -57,19 +57,7 @@ namespace Teknik.Areas.Paste.Controllers // Check Expiration if (PasteHelper.CheckExpiration(paste)) { - if (!string.IsNullOrEmpty(paste.FileName)) - { - string delSub = paste.FileName[0].ToString(); - string delPath = Path.Combine(_config.PasteConfig.PasteDirectory, delSub, paste.FileName); - - // Delete the File - if (System.IO.File.Exists(delPath)) - { - System.IO.File.Delete(delPath); - } - } - _dbContext.Pastes.Remove(paste); - _dbContext.SaveChanges(); + DeleteFile(paste); return new StatusCodeResult(StatusCodes.Status404NotFound); } @@ -231,20 +219,7 @@ namespace Teknik.Areas.Paste.Controllers // Check Expiration if (PasteHelper.CheckExpiration(paste)) { - if (!string.IsNullOrEmpty(paste.FileName)) - { - string delSub = paste.FileName[0].ToString(); - string delPath = Path.Combine(_config.PasteConfig.PasteDirectory, delSub, paste.FileName); - - // Delete the File - if (System.IO.File.Exists(delPath)) - { - System.IO.File.Delete(delPath); - } - } - // Delete from the DB - _dbContext.Pastes.Remove(paste); - _dbContext.SaveChanges(); + DeleteFile(paste); return new StatusCodeResult(StatusCodes.Status404NotFound); } @@ -408,17 +383,7 @@ namespace Teknik.Areas.Paste.Controllers { if (foundPaste.User.Username == User.Identity.Name) { - string subDir = foundPaste.FileName[0].ToString(); - string filePath = Path.Combine(_config.PasteConfig.PasteDirectory, subDir, foundPaste.FileName); - // Delete from the DB - _dbContext.Pastes.Remove(foundPaste); - _dbContext.SaveChanges(); - - // Delete the File - if (System.IO.File.Exists(filePath)) - { - System.IO.File.Delete(filePath); - } + DeleteFile(foundPaste); return Json(new { result = true, redirect = Url.SubRouteUrl("p", "Paste.Index") }); } @@ -451,5 +416,24 @@ namespace Teknik.Areas.Paste.Controllers HttpContext.Session.Remove("PastePassword_" + url); } } + + private void DeleteFile(Models.Paste paste) + { + if (!string.IsNullOrEmpty(paste.FileName)) + { + string delSub = paste.FileName[0].ToString(); + string delPath = Path.Combine(_config.PasteConfig.PasteDirectory, delSub, paste.FileName); + + // Delete the File + if (System.IO.File.Exists(delPath)) + { + System.IO.File.Delete(delPath); + } + } + + // Delete from the DB + _dbContext.Pastes.Remove(paste); + _dbContext.SaveChanges(); + } } } \ No newline at end of file diff --git a/Teknik/Areas/Upload/Controllers/UploadController.cs b/Teknik/Areas/Upload/Controllers/UploadController.cs index 1911620..9a504f0 100644 --- a/Teknik/Areas/Upload/Controllers/UploadController.cs +++ b/Teknik/Areas/Upload/Controllers/UploadController.cs @@ -203,17 +203,7 @@ namespace Teknik.Areas.Upload.Controllers // Check Expiration if (UploadHelper.CheckExpiration(upload)) { - string subDir = upload.FileName[0].ToString(); - string filePath = Path.Combine(_config.UploadConfig.UploadDirectory, subDir, upload.FileName); - // Delete from the DB - _dbContext.Uploads.Remove(upload); - _dbContext.SaveChanges(); - - // Delete the File - if (System.IO.File.Exists(filePath)) - { - System.IO.File.Delete(filePath); - } + DeleteFile(upload); return new StatusCodeResult(StatusCodes.Status404NotFound); } @@ -432,17 +422,7 @@ namespace Teknik.Areas.Upload.Controllers // Check Expiration if (UploadHelper.CheckExpiration(upload)) { - string delDir = upload.FileName[0].ToString(); - string delPath = Path.Combine(_config.UploadConfig.UploadDirectory, delDir, upload.FileName); - // Delete from the DB - _dbContext.Uploads.Remove(upload); - _dbContext.SaveChanges(); - - // Delete the File - if (System.IO.File.Exists(delPath)) - { - System.IO.File.Delete(delPath); - } + DeleteFile(upload); return Json(new { error = new { message = "File Does Not Exist" } }); } @@ -500,17 +480,7 @@ namespace Teknik.Areas.Upload.Controllers model.File = file; if (!string.IsNullOrEmpty(upload.DeleteKey) && upload.DeleteKey == key) { - string subDir = upload.FileName[0].ToString(); - string filePath = Path.Combine(_config.UploadConfig.UploadDirectory, subDir, upload.FileName); - // Delete from the DB - _dbContext.Uploads.Remove(upload); - _dbContext.SaveChanges(); - - // Delete the File - if (System.IO.File.Exists(filePath)) - { - System.IO.File.Delete(filePath); - } + DeleteFile(upload); model.Deleted = true; } else @@ -550,23 +520,28 @@ namespace Teknik.Areas.Upload.Controllers { if (foundUpload.User.Username == User.Identity.Name) { - string subDir = foundUpload.FileName[0].ToString(); - string filePath = Path.Combine(_config.UploadConfig.UploadDirectory, subDir, foundUpload.FileName); - // Delete from the DB - _dbContext.Uploads.Remove(foundUpload); - _dbContext.SaveChanges(); - - // Delete the File - if (System.IO.File.Exists(filePath)) - { - System.IO.File.Delete(filePath); - } - + DeleteFile(foundUpload); return Json(new { result = true }); } return Json(new { error = new { message = "You do not have permission to edit this Paste" } }); } - return Json(new { error = new { message = "This Paste does not exist" } }); + return Json(new { error = new { message = "This Upload does not exist" } }); + } + + private void DeleteFile(Models.Upload upload) + { + string subDir = upload.FileName[0].ToString(); + string filePath = Path.Combine(_config.UploadConfig.UploadDirectory, subDir, upload.FileName); + + // Delete the File + if (System.IO.File.Exists(filePath)) + { + System.IO.File.Delete(filePath); + } + + // Delete from the DB + _dbContext.Uploads.Remove(upload); + _dbContext.SaveChanges(); } } }