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

Fixed null reference exceptions

This commit is contained in:
Uncled1023 2022-03-13 23:44:15 -07:00
parent 6d9428e35b
commit 67e3446aa3
3 changed files with 50 additions and 33 deletions

View File

@ -150,7 +150,8 @@ namespace Teknik.Areas.Paste
{
var paste = _pasteCache.GetObject(url, (key) => db.Pastes.FirstOrDefault(up => up.Url == key));
if (!db.Exists(paste))
if (paste != null &&
!db.Exists(paste))
db.Attach(paste);
return paste;
@ -160,6 +161,8 @@ namespace Teknik.Areas.Paste
public static void DeleteFile(TeknikEntities db, Config config, ILogger<Logger> logger, string url)
{
var paste = GetPaste(db, url);
if (paste != null)
{
try
{
var storageService = StorageServiceFactory.GetStorageService(config.PasteConfig.StorageConfig);
@ -173,6 +176,7 @@ namespace Teknik.Areas.Paste
// Delete from the DB
db.Pastes.Remove(paste);
db.SaveChanges();
}
// Remove from the cache
lock (_cacheLock)
@ -189,6 +193,8 @@ namespace Teknik.Areas.Paste
_pasteCache.UpdateObject(paste.Url, paste);
}
if (paste != null)
{
if (!db.Exists(paste))
db.Attach(paste);
@ -197,4 +203,5 @@ namespace Teknik.Areas.Paste
db.SaveChanges();
}
}
}
}

View File

@ -529,7 +529,7 @@ namespace Teknik.Areas.Upload.Controllers
[HttpOptions]
public IActionResult Delete(string id)
{
Models.Upload foundUpload = _dbContext.Uploads.Where(u => u.Url == id).FirstOrDefault();
Models.Upload foundUpload = UploadHelper.GetUpload(_dbContext, id);
if (foundUpload != null)
{
if (foundUpload.User?.Username == User.Identity.Name ||

View File

@ -172,7 +172,8 @@ namespace Teknik.Areas.Upload
{
var upload = _uploadCache.GetObject(url, (key) => db.Uploads.FirstOrDefault(up => up.Url == key));
if (!db.Exists(upload))
if (upload != null &&
!db.Exists(upload))
db.Attach(upload);
return upload;
@ -182,6 +183,8 @@ namespace Teknik.Areas.Upload
public static void DeleteFile(TeknikEntities db, Config config, ILogger<Logger> logger, string url)
{
var upload = GetUpload(db, url);
if (upload != null)
{
try
{
var storageService = StorageServiceFactory.GetStorageService(config.UploadConfig.StorageConfig);
@ -195,6 +198,7 @@ namespace Teknik.Areas.Upload
// Delete from the DB
db.Uploads.Remove(upload);
db.SaveChanges();
}
// Remove from the cache
lock (_cacheLock)
@ -211,9 +215,15 @@ namespace Teknik.Areas.Upload
_uploadCache.UpdateObject(upload.Url, upload);
}
if (upload != null)
{
if (!db.Exists(upload))
db.Attach(upload);
// Update the database
db.Entry(upload).State = EntityState.Modified;
db.SaveChanges();
}
}
}
}