diff --git a/Teknik/Areas/Paste/PasteHelper.cs b/Teknik/Areas/Paste/PasteHelper.cs index 4d90010..404b6ae 100644 --- a/Teknik/Areas/Paste/PasteHelper.cs +++ b/Teknik/Areas/Paste/PasteHelper.cs @@ -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 public static void DeleteFile(TeknikEntities db, Config config, ILogger logger, string url) { var paste = GetPaste(db, url); - try + if (paste != null) { - var storageService = StorageServiceFactory.GetStorageService(config.PasteConfig.StorageConfig); - storageService.DeleteFile(paste.FileName); - } - catch (Exception ex) - { - 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 _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(); + } } } } \ No newline at end of file diff --git a/Teknik/Areas/Upload/Controllers/UploadController.cs b/Teknik/Areas/Upload/Controllers/UploadController.cs index 9b10ac8..be9efbe 100644 --- a/Teknik/Areas/Upload/Controllers/UploadController.cs +++ b/Teknik/Areas/Upload/Controllers/UploadController.cs @@ -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 || diff --git a/Teknik/Areas/Upload/UploadHelper.cs b/Teknik/Areas/Upload/UploadHelper.cs index e317404..de7e328 100644 --- a/Teknik/Areas/Upload/UploadHelper.cs +++ b/Teknik/Areas/Upload/UploadHelper.cs @@ -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 public static void DeleteFile(TeknikEntities db, Config config, ILogger logger, string url) { var upload = GetUpload(db, url); - try + if (upload != null) { - var storageService = StorageServiceFactory.GetStorageService(config.UploadConfig.StorageConfig); - storageService.DeleteFile(upload.FileName); - } - catch (Exception ex) - { - 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 _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(); + } } } }