Browse Source

Fixed paste and upload file deletion

Teknikode 5 months ago
parent
commit
372997f995

+ 22
- 38
Teknik/Areas/Paste/Controllers/PasteController.cs View File

@@ -57,19 +57,7 @@ namespace Teknik.Areas.Paste.Controllers
57 57
                 // Check Expiration
58 58
                 if (PasteHelper.CheckExpiration(paste))
59 59
                 {
60
-                    if (!string.IsNullOrEmpty(paste.FileName))
61
-                    {
62
-                        string delSub = paste.FileName[0].ToString();
63
-                        string delPath = Path.Combine(_config.PasteConfig.PasteDirectory, delSub, paste.FileName);
64
-
65
-                        // Delete the File
66
-                        if (System.IO.File.Exists(delPath))
67
-                        {
68
-                            System.IO.File.Delete(delPath);
69
-                        }
70
-                    }
71
-                    _dbContext.Pastes.Remove(paste);
72
-                    _dbContext.SaveChanges();
60
+                    DeleteFile(paste);
73 61
                     return new StatusCodeResult(StatusCodes.Status404NotFound);
74 62
                 }
75 63
 
@@ -231,20 +219,7 @@ namespace Teknik.Areas.Paste.Controllers
231 219
                 // Check Expiration
232 220
                 if (PasteHelper.CheckExpiration(paste))
233 221
                 {
234
-                    if (!string.IsNullOrEmpty(paste.FileName))
235
-                    {
236
-                        string delSub = paste.FileName[0].ToString();
237
-                        string delPath = Path.Combine(_config.PasteConfig.PasteDirectory, delSub, paste.FileName);
238
-
239
-                        // Delete the File
240
-                        if (System.IO.File.Exists(delPath))
241
-                        {
242
-                            System.IO.File.Delete(delPath);
243
-                        }
244
-                    }
245
-                    // Delete from the DB
246
-                    _dbContext.Pastes.Remove(paste);
247
-                    _dbContext.SaveChanges();
222
+                    DeleteFile(paste);
248 223
                     return new StatusCodeResult(StatusCodes.Status404NotFound);
249 224
                 }
250 225
 
@@ -408,17 +383,7 @@ namespace Teknik.Areas.Paste.Controllers
408 383
             {
409 384
                 if (foundPaste.User.Username == User.Identity.Name)
410 385
                 {
411
-                    string subDir = foundPaste.FileName[0].ToString();
412
-                    string filePath = Path.Combine(_config.PasteConfig.PasteDirectory, subDir, foundPaste.FileName);
413
-                    // Delete from the DB
414
-                    _dbContext.Pastes.Remove(foundPaste);
415
-                    _dbContext.SaveChanges();
416
-
417
-                    // Delete the File
418
-                    if (System.IO.File.Exists(filePath))
419
-                    {
420
-                        System.IO.File.Delete(filePath);
421
-                    }
386
+                    DeleteFile(foundPaste);
422 387
 
423 388
                     return Json(new { result = true, redirect = Url.SubRouteUrl("p", "Paste.Index") });
424 389
                 }
@@ -451,5 +416,24 @@ namespace Teknik.Areas.Paste.Controllers
451 416
                 HttpContext.Session.Remove("PastePassword_" + url);
452 417
             }
453 418
         }
419
+
420
+        private void DeleteFile(Models.Paste paste)
421
+        {
422
+            if (!string.IsNullOrEmpty(paste.FileName))
423
+            {
424
+                string delSub = paste.FileName[0].ToString();
425
+                string delPath = Path.Combine(_config.PasteConfig.PasteDirectory, delSub, paste.FileName);
426
+
427
+                // Delete the File
428
+                if (System.IO.File.Exists(delPath))
429
+                {
430
+                    System.IO.File.Delete(delPath);
431
+                }
432
+            }
433
+
434
+            // Delete from the DB
435
+            _dbContext.Pastes.Remove(paste);
436
+            _dbContext.SaveChanges();
437
+        }
454 438
     }
455 439
 }

+ 21
- 46
Teknik/Areas/Upload/Controllers/UploadController.cs View File

@@ -203,17 +203,7 @@ namespace Teknik.Areas.Upload.Controllers
203 203
                     // Check Expiration
204 204
                     if (UploadHelper.CheckExpiration(upload))
205 205
                     {
206
-                        string subDir = upload.FileName[0].ToString();
207
-                        string filePath = Path.Combine(_config.UploadConfig.UploadDirectory, subDir, upload.FileName);
208
-                        // Delete from the DB
209
-                        _dbContext.Uploads.Remove(upload);
210
-                        _dbContext.SaveChanges();
211
-
212
-                        // Delete the File
213
-                        if (System.IO.File.Exists(filePath))
214
-                        {
215
-                            System.IO.File.Delete(filePath);
216
-                        }
206
+                        DeleteFile(upload);
217 207
                         return new StatusCodeResult(StatusCodes.Status404NotFound);
218 208
                     }
219 209
 
@@ -432,17 +422,7 @@ namespace Teknik.Areas.Upload.Controllers
432 422
                     // Check Expiration
433 423
                     if (UploadHelper.CheckExpiration(upload))
434 424
                     {
435
-                        string delDir = upload.FileName[0].ToString();
436
-                        string delPath = Path.Combine(_config.UploadConfig.UploadDirectory, delDir, upload.FileName);
437
-                        // Delete from the DB
438
-                        _dbContext.Uploads.Remove(upload);
439
-                        _dbContext.SaveChanges();
440
-
441
-                        // Delete the File
442
-                        if (System.IO.File.Exists(delPath))
443
-                        {
444
-                            System.IO.File.Delete(delPath);
445
-                        }
425
+                        DeleteFile(upload);
446 426
                         return Json(new { error = new { message = "File Does Not Exist" } });
447 427
                     }
448 428
 
@@ -500,17 +480,7 @@ namespace Teknik.Areas.Upload.Controllers
500 480
                 model.File = file;
501 481
                 if (!string.IsNullOrEmpty(upload.DeleteKey) && upload.DeleteKey == key)
502 482
                 {
503
-                    string subDir = upload.FileName[0].ToString();
504
-                    string filePath = Path.Combine(_config.UploadConfig.UploadDirectory, subDir, upload.FileName);
505
-                    // Delete from the DB
506
-                    _dbContext.Uploads.Remove(upload);
507
-                    _dbContext.SaveChanges();
508
-
509
-                    // Delete the File
510
-                    if (System.IO.File.Exists(filePath))
511
-                    {
512
-                        System.IO.File.Delete(filePath);
513
-                    }
483
+                    DeleteFile(upload);
514 484
                     model.Deleted = true;
515 485
                 }
516 486
                 else
@@ -550,23 +520,28 @@ namespace Teknik.Areas.Upload.Controllers
550 520
             {
551 521
                 if (foundUpload.User.Username == User.Identity.Name)
552 522
                 {
553
-                    string subDir = foundUpload.FileName[0].ToString();
554
-                    string filePath = Path.Combine(_config.UploadConfig.UploadDirectory, subDir, foundUpload.FileName);
555
-                    // Delete from the DB
556
-                    _dbContext.Uploads.Remove(foundUpload);
557
-                    _dbContext.SaveChanges();
558
-
559
-                    // Delete the File
560
-                    if (System.IO.File.Exists(filePath))
561
-                    {
562
-                        System.IO.File.Delete(filePath);
563
-                    }
564
-
523
+                    DeleteFile(foundUpload);
565 524
                     return Json(new { result = true });
566 525
                 }
567 526
                 return Json(new { error = new { message = "You do not have permission to edit this Paste" } });
568 527
             }
569
-            return Json(new { error = new { message = "This Paste does not exist" } });
528
+            return Json(new { error = new { message = "This Upload does not exist" } });
529
+        }
530
+
531
+        private void DeleteFile(Models.Upload upload)
532
+        {
533
+            string subDir = upload.FileName[0].ToString();
534
+            string filePath = Path.Combine(_config.UploadConfig.UploadDirectory, subDir, upload.FileName);
535
+
536
+            // Delete the File
537
+            if (System.IO.File.Exists(filePath))
538
+            {
539
+                System.IO.File.Delete(filePath);
540
+            }
541
+
542
+            // Delete from the DB
543
+            _dbContext.Uploads.Remove(upload);
544
+            _dbContext.SaveChanges();
570 545
         }
571 546
     }
572 547
 }

Loading…
Cancel
Save