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:
parent
6d9428e35b
commit
67e3446aa3
@ -150,7 +150,8 @@ namespace Teknik.Areas.Paste
|
|||||||
{
|
{
|
||||||
var paste = _pasteCache.GetObject(url, (key) => db.Pastes.FirstOrDefault(up => up.Url == key));
|
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);
|
db.Attach(paste);
|
||||||
|
|
||||||
return paste;
|
return paste;
|
||||||
@ -160,6 +161,8 @@ namespace Teknik.Areas.Paste
|
|||||||
public static void DeleteFile(TeknikEntities db, Config config, ILogger<Logger> logger, string url)
|
public static void DeleteFile(TeknikEntities db, Config config, ILogger<Logger> logger, string url)
|
||||||
{
|
{
|
||||||
var paste = GetPaste(db, url);
|
var paste = GetPaste(db, url);
|
||||||
|
if (paste != null)
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var storageService = StorageServiceFactory.GetStorageService(config.PasteConfig.StorageConfig);
|
var storageService = StorageServiceFactory.GetStorageService(config.PasteConfig.StorageConfig);
|
||||||
@ -173,6 +176,7 @@ namespace Teknik.Areas.Paste
|
|||||||
// Delete from the DB
|
// Delete from the DB
|
||||||
db.Pastes.Remove(paste);
|
db.Pastes.Remove(paste);
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
// Remove from the cache
|
// Remove from the cache
|
||||||
lock (_cacheLock)
|
lock (_cacheLock)
|
||||||
@ -189,6 +193,8 @@ namespace Teknik.Areas.Paste
|
|||||||
_pasteCache.UpdateObject(paste.Url, paste);
|
_pasteCache.UpdateObject(paste.Url, paste);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (paste != null)
|
||||||
|
{
|
||||||
if (!db.Exists(paste))
|
if (!db.Exists(paste))
|
||||||
db.Attach(paste);
|
db.Attach(paste);
|
||||||
|
|
||||||
@ -197,4 +203,5 @@ namespace Teknik.Areas.Paste
|
|||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -529,7 +529,7 @@ namespace Teknik.Areas.Upload.Controllers
|
|||||||
[HttpOptions]
|
[HttpOptions]
|
||||||
public IActionResult Delete(string id)
|
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 != null)
|
||||||
{
|
{
|
||||||
if (foundUpload.User?.Username == User.Identity.Name ||
|
if (foundUpload.User?.Username == User.Identity.Name ||
|
||||||
|
@ -172,7 +172,8 @@ namespace Teknik.Areas.Upload
|
|||||||
{
|
{
|
||||||
var upload = _uploadCache.GetObject(url, (key) => db.Uploads.FirstOrDefault(up => up.Url == key));
|
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);
|
db.Attach(upload);
|
||||||
|
|
||||||
return upload;
|
return upload;
|
||||||
@ -182,6 +183,8 @@ namespace Teknik.Areas.Upload
|
|||||||
public static void DeleteFile(TeknikEntities db, Config config, ILogger<Logger> logger, string url)
|
public static void DeleteFile(TeknikEntities db, Config config, ILogger<Logger> logger, string url)
|
||||||
{
|
{
|
||||||
var upload = GetUpload(db, url);
|
var upload = GetUpload(db, url);
|
||||||
|
if (upload != null)
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var storageService = StorageServiceFactory.GetStorageService(config.UploadConfig.StorageConfig);
|
var storageService = StorageServiceFactory.GetStorageService(config.UploadConfig.StorageConfig);
|
||||||
@ -195,6 +198,7 @@ namespace Teknik.Areas.Upload
|
|||||||
// Delete from the DB
|
// Delete from the DB
|
||||||
db.Uploads.Remove(upload);
|
db.Uploads.Remove(upload);
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
// Remove from the cache
|
// Remove from the cache
|
||||||
lock (_cacheLock)
|
lock (_cacheLock)
|
||||||
@ -211,9 +215,15 @@ namespace Teknik.Areas.Upload
|
|||||||
_uploadCache.UpdateObject(upload.Url, upload);
|
_uploadCache.UpdateObject(upload.Url, upload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (upload != null)
|
||||||
|
{
|
||||||
|
if (!db.Exists(upload))
|
||||||
|
db.Attach(upload);
|
||||||
|
|
||||||
// Update the database
|
// Update the database
|
||||||
db.Entry(upload).State = EntityState.Modified;
|
db.Entry(upload).State = EntityState.Modified;
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user