1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Fixed paste and upload file deletion

This commit is contained in:
Uncled1023 2019-02-05 00:00:50 -08:00
parent 94546b1881
commit 372997f995
2 changed files with 43 additions and 84 deletions

View File

@ -57,19 +57,7 @@ namespace Teknik.Areas.Paste.Controllers
// Check Expiration // Check Expiration
if (PasteHelper.CheckExpiration(paste)) if (PasteHelper.CheckExpiration(paste))
{ {
if (!string.IsNullOrEmpty(paste.FileName)) DeleteFile(paste);
{
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();
return new StatusCodeResult(StatusCodes.Status404NotFound); return new StatusCodeResult(StatusCodes.Status404NotFound);
} }
@ -231,20 +219,7 @@ namespace Teknik.Areas.Paste.Controllers
// Check Expiration // Check Expiration
if (PasteHelper.CheckExpiration(paste)) if (PasteHelper.CheckExpiration(paste))
{ {
if (!string.IsNullOrEmpty(paste.FileName)) DeleteFile(paste);
{
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();
return new StatusCodeResult(StatusCodes.Status404NotFound); return new StatusCodeResult(StatusCodes.Status404NotFound);
} }
@ -408,17 +383,7 @@ namespace Teknik.Areas.Paste.Controllers
{ {
if (foundPaste.User.Username == User.Identity.Name) if (foundPaste.User.Username == User.Identity.Name)
{ {
string subDir = foundPaste.FileName[0].ToString(); DeleteFile(foundPaste);
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);
}
return Json(new { result = true, redirect = Url.SubRouteUrl("p", "Paste.Index") }); 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); 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();
}
} }
} }

View File

@ -203,17 +203,7 @@ namespace Teknik.Areas.Upload.Controllers
// Check Expiration // Check Expiration
if (UploadHelper.CheckExpiration(upload)) if (UploadHelper.CheckExpiration(upload))
{ {
string subDir = upload.FileName[0].ToString(); DeleteFile(upload);
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);
}
return new StatusCodeResult(StatusCodes.Status404NotFound); return new StatusCodeResult(StatusCodes.Status404NotFound);
} }
@ -432,17 +422,7 @@ namespace Teknik.Areas.Upload.Controllers
// Check Expiration // Check Expiration
if (UploadHelper.CheckExpiration(upload)) if (UploadHelper.CheckExpiration(upload))
{ {
string delDir = upload.FileName[0].ToString(); DeleteFile(upload);
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);
}
return Json(new { error = new { message = "File Does Not Exist" } }); return Json(new { error = new { message = "File Does Not Exist" } });
} }
@ -500,17 +480,7 @@ namespace Teknik.Areas.Upload.Controllers
model.File = file; model.File = file;
if (!string.IsNullOrEmpty(upload.DeleteKey) && upload.DeleteKey == key) if (!string.IsNullOrEmpty(upload.DeleteKey) && upload.DeleteKey == key)
{ {
string subDir = upload.FileName[0].ToString(); DeleteFile(upload);
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);
}
model.Deleted = true; model.Deleted = true;
} }
else else
@ -550,11 +520,18 @@ namespace Teknik.Areas.Upload.Controllers
{ {
if (foundUpload.User.Username == User.Identity.Name) if (foundUpload.User.Username == User.Identity.Name)
{ {
string subDir = foundUpload.FileName[0].ToString(); DeleteFile(foundUpload);
string filePath = Path.Combine(_config.UploadConfig.UploadDirectory, subDir, foundUpload.FileName); return Json(new { result = true });
// Delete from the DB }
_dbContext.Uploads.Remove(foundUpload); return Json(new { error = new { message = "You do not have permission to edit this Paste" } });
_dbContext.SaveChanges(); }
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 // Delete the File
if (System.IO.File.Exists(filePath)) if (System.IO.File.Exists(filePath))
@ -562,11 +539,9 @@ namespace Teknik.Areas.Upload.Controllers
System.IO.File.Delete(filePath); System.IO.File.Delete(filePath);
} }
return Json(new { result = true }); // Delete from the DB
} _dbContext.Uploads.Remove(upload);
return Json(new { error = new { message = "You do not have permission to edit this Paste" } }); _dbContext.SaveChanges();
}
return Json(new { error = new { message = "This Paste does not exist" } });
} }
} }
} }