Browse Source

Fixed null reference exceptions

master
Teknikode 4 months ago
parent
commit
67e3446aa3
  1. 41
      Teknik/Areas/Paste/PasteHelper.cs
  2. 2
      Teknik/Areas/Upload/Controllers/UploadController.cs
  3. 40
      Teknik/Areas/Upload/UploadHelper.cs

41
Teknik/Areas/Paste/PasteHelper.cs

@ -150,7 +150,8 @@ namespace Teknik.Areas.Paste @@ -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,19 +161,22 @@ namespace Teknik.Areas.Paste @@ -160,19 +161,22 @@ namespace Teknik.Areas.Paste
public static void DeleteFile(TeknikEntities db, Config config, ILogger<Logger> logger, string url)
{
var paste = GetPaste(db, url);
try
{
var storageService = StorageServiceFactory.GetStorageService(config.PasteConfig.StorageConfig);
storageService.DeleteFile(paste.FileName);
}
catch (Exception ex)
if (paste != null)
{
logger.LogError(ex, "Unable to delete file: {0}", paste.FileName);
}
try
{
var storageService = StorageServiceFactory.GetStorageService(config.PasteConfig.StorageConfig);
storageService.DeleteFile(paste.FileName);
}
catch (Exception ex)
{
logger.LogError(ex, "Unable to delete file: {0}", paste.FileName);
}
// Delete from the DB
db.Pastes.Remove(paste);
db.SaveChanges();
// Delete from the DB
db.Pastes.Remove(paste);
db.SaveChanges();
}
// Remove from the cache
lock (_cacheLock)
@ -189,12 +193,15 @@ namespace Teknik.Areas.Paste @@ -189,12 +193,15 @@ namespace Teknik.Areas.Paste
_pasteCache.UpdateObject(paste.Url, paste);
}
if (!db.Exists(paste))
db.Attach(paste);
if (paste != null)
{
if (!db.Exists(paste))
db.Attach(paste);
// Update the database
db.Entry(paste).State = EntityState.Modified;
db.SaveChanges();
// Update the database
db.Entry(paste).State = EntityState.Modified;
db.SaveChanges();
}
}
}
}

2
Teknik/Areas/Upload/Controllers/UploadController.cs

@ -529,7 +529,7 @@ namespace Teknik.Areas.Upload.Controllers @@ -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 ||

40
Teknik/Areas/Upload/UploadHelper.cs

@ -172,7 +172,8 @@ namespace Teknik.Areas.Upload @@ -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,19 +183,22 @@ namespace Teknik.Areas.Upload @@ -182,19 +183,22 @@ namespace Teknik.Areas.Upload
public static void DeleteFile(TeknikEntities db, Config config, ILogger<Logger> logger, string url)
{
var upload = GetUpload(db, url);
try
{
var storageService = StorageServiceFactory.GetStorageService(config.UploadConfig.StorageConfig);
storageService.DeleteFile(upload.FileName);
}
catch (Exception ex)
if (upload != null)
{
logger.LogError(ex, "Unable to delete file: {0}", upload.FileName);
}
try
{
var storageService = StorageServiceFactory.GetStorageService(config.UploadConfig.StorageConfig);
storageService.DeleteFile(upload.FileName);
}
catch (Exception ex)
{
logger.LogError(ex, "Unable to delete file: {0}", upload.FileName);
}
// Delete from the DB
db.Uploads.Remove(upload);
db.SaveChanges();
// Delete from the DB
db.Uploads.Remove(upload);
db.SaveChanges();
}
// Remove from the cache
lock (_cacheLock)
@ -211,9 +215,15 @@ namespace Teknik.Areas.Upload @@ -211,9 +215,15 @@ namespace Teknik.Areas.Upload
_uploadCache.UpdateObject(upload.Url, upload);
}
// Update the database
db.Entry(upload).State = EntityState.Modified;
db.SaveChanges();
if (upload != null)
{
if (!db.Exists(upload))
db.Attach(upload);
// Update the database
db.Entry(upload).State = EntityState.Modified;
db.SaveChanges();
}
}
}
}

Loading…
Cancel
Save