Browse Source

Fixed service data deletions

Teknikode 1 week ago
parent
commit
94546b1881

+ 25
- 0
ServiceWorker/Program.cs View File

@@ -222,11 +222,36 @@ namespace Teknik.ServiceWorker
222 222
 
223 223
             // Process uploads
224 224
             List<Upload> uploads = db.Uploads.Where(u => u.ExpireDate != null && u.ExpireDate < curDate).ToList();
225
+
226
+            foreach (Upload upload in uploads)
227
+            {
228
+                string subDir = upload.FileName[0].ToString();
229
+                string filePath = Path.Combine(config.UploadConfig.UploadDirectory, subDir, upload.FileName);
230
+
231
+                // Delete the File
232
+                if (File.Exists(filePath))
233
+                {
234
+                    File.Delete(filePath);
235
+                }
236
+            }
237
+
225 238
             db.RemoveRange(uploads);
226 239
             db.SaveChanges();
227 240
 
228 241
             // Process Pastes
229 242
             List<Paste> pastes = db.Pastes.Where(p => p.ExpireDate != null && p.ExpireDate < curDate).ToList();
243
+
244
+            foreach (Paste paste in pastes)
245
+            {
246
+                string subDir = paste.FileName[0].ToString();
247
+                string filePath = Path.Combine(config.PasteConfig.PasteDirectory, subDir, paste.FileName);
248
+
249
+                // Delete the File
250
+                if (File.Exists(filePath))
251
+                {
252
+                    File.Delete(filePath);
253
+                }
254
+            }
230 255
             db.RemoveRange(pastes);
231 256
             db.SaveChanges();
232 257
         }

+ 26
- 1
Teknik/Areas/Paste/Controllers/PasteController.cs View File

@@ -57,6 +57,17 @@ 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
+                    }
60 71
                     _dbContext.Pastes.Remove(paste);
61 72
                     _dbContext.SaveChanges();
62 73
                     return new StatusCodeResult(StatusCodes.Status404NotFound);
@@ -220,6 +231,18 @@ namespace Teknik.Areas.Paste.Controllers
220 231
                 // Check Expiration
221 232
                 if (PasteHelper.CheckExpiration(paste))
222 233
                 {
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
223 246
                     _dbContext.Pastes.Remove(paste);
224 247
                     _dbContext.SaveChanges();
225 248
                     return new StatusCodeResult(StatusCodes.Status404NotFound);
@@ -377,6 +400,7 @@ namespace Teknik.Areas.Paste.Controllers
377 400
         }
378 401
 
379 402
         [HttpPost]
403
+        [HttpOptions]
380 404
         public IActionResult Delete(string id)
381 405
         {
382 406
             Models.Paste foundPaste = _dbContext.Pastes.Where(p => p.Url == id).FirstOrDefault();
@@ -384,7 +408,8 @@ namespace Teknik.Areas.Paste.Controllers
384 408
             {
385 409
                 if (foundPaste.User.Username == User.Identity.Name)
386 410
                 {
387
-                    string filePath = foundPaste.FileName;
411
+                    string subDir = foundPaste.FileName[0].ToString();
412
+                    string filePath = Path.Combine(_config.PasteConfig.PasteDirectory, subDir, foundPaste.FileName);
388 413
                     // Delete from the DB
389 414
                     _dbContext.Pastes.Remove(foundPaste);
390 415
                     _dbContext.SaveChanges();

+ 1
- 0
Teknik/Areas/Shortener/Controllers/ShortenerController.cs View File

@@ -79,6 +79,7 @@ namespace Teknik.Areas.Shortener.Controllers
79 79
         }
80 80
 
81 81
         [HttpPost]
82
+        [HttpOptions]
82 83
         public IActionResult Delete(string id)
83 84
         {
84 85
             ShortenedUrl shortenedUrl = _dbContext.ShortenedUrls.Where(s => s.ShortUrl == id).FirstOrDefault();

+ 23
- 2
Teknik/Areas/Upload/Controllers/UploadController.cs View File

@@ -203,8 +203,17 @@ 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
206 209
                         _dbContext.Uploads.Remove(upload);
207 210
                         _dbContext.SaveChanges();
211
+
212
+                        // Delete the File
213
+                        if (System.IO.File.Exists(filePath))
214
+                        {
215
+                            System.IO.File.Delete(filePath);
216
+                        }
208 217
                         return new StatusCodeResult(StatusCodes.Status404NotFound);
209 218
                     }
210 219
 
@@ -423,8 +432,17 @@ namespace Teknik.Areas.Upload.Controllers
423 432
                     // Check Expiration
424 433
                     if (UploadHelper.CheckExpiration(upload))
425 434
                     {
435
+                        string delDir = upload.FileName[0].ToString();
436
+                        string delPath = Path.Combine(_config.UploadConfig.UploadDirectory, delDir, upload.FileName);
437
+                        // Delete from the DB
426 438
                         _dbContext.Uploads.Remove(upload);
427 439
                         _dbContext.SaveChanges();
440
+
441
+                        // Delete the File
442
+                        if (System.IO.File.Exists(delPath))
443
+                        {
444
+                            System.IO.File.Delete(delPath);
445
+                        }
428 446
                         return Json(new { error = new { message = "File Does Not Exist" } });
429 447
                     }
430 448
 
@@ -482,7 +500,8 @@ namespace Teknik.Areas.Upload.Controllers
482 500
                 model.File = file;
483 501
                 if (!string.IsNullOrEmpty(upload.DeleteKey) && upload.DeleteKey == key)
484 502
                 {
485
-                    string filePath = upload.FileName;
503
+                    string subDir = upload.FileName[0].ToString();
504
+                    string filePath = Path.Combine(_config.UploadConfig.UploadDirectory, subDir, upload.FileName);
486 505
                     // Delete from the DB
487 506
                     _dbContext.Uploads.Remove(upload);
488 507
                     _dbContext.SaveChanges();
@@ -523,6 +542,7 @@ namespace Teknik.Areas.Upload.Controllers
523 542
         }
524 543
 
525 544
         [HttpPost]
545
+        [HttpOptions]
526 546
         public IActionResult Delete(string id)
527 547
         {
528 548
             Models.Upload foundUpload = _dbContext.Uploads.Where(u => u.Url == id).FirstOrDefault();
@@ -530,7 +550,8 @@ namespace Teknik.Areas.Upload.Controllers
530 550
             {
531 551
                 if (foundUpload.User.Username == User.Identity.Name)
532 552
                 {
533
-                    string filePath = foundUpload.FileName;
553
+                    string subDir = foundUpload.FileName[0].ToString();
554
+                    string filePath = Path.Combine(_config.UploadConfig.UploadDirectory, subDir, foundUpload.FileName);
534 555
                     // Delete from the DB
535 556
                     _dbContext.Uploads.Remove(foundUpload);
536 557
                     _dbContext.SaveChanges();

+ 31
- 0
Teknik/Areas/User/Controllers/UserController.cs View File

@@ -1396,5 +1396,36 @@ namespace Teknik.Areas.Users.Controllers
1396 1396
                 return Json(new { error = ex.GetFullMessage(true) });
1397 1397
             }
1398 1398
         }
1399
+
1400
+        [HttpPost]
1401
+        [ValidateAntiForgeryToken]
1402
+        public IActionResult DeleteData(string type, string id)
1403
+        {
1404
+            var context = new ControllerContext();
1405
+            context.HttpContext = Request.HttpContext;
1406
+            context.RouteData = RouteData;
1407
+            context.ActionDescriptor = new Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor();
1408
+
1409
+            switch (type)
1410
+            {
1411
+                case "upload":
1412
+                    var uploadController = new Upload.Controllers.UploadController(_logger, _config, _dbContext);
1413
+                    uploadController.ControllerContext = context;
1414
+                    return uploadController.Delete(id);
1415
+                case "paste":
1416
+                    var pasteController = new Paste.Controllers.PasteController(_logger, _config, _dbContext);
1417
+                    pasteController.ControllerContext = context;
1418
+                    return pasteController.Delete(id);
1419
+                case "shortenedUrl":
1420
+                    var shortenController = new Shortener.Controllers.ShortenerController(_logger, _config, _dbContext);
1421
+                    shortenController.ControllerContext = context;
1422
+                    return shortenController.Delete(id);
1423
+                case "vault":
1424
+                    var vaultController = new Vault.Controllers.VaultController(_logger, _config, _dbContext);
1425
+                    vaultController.ControllerContext = context;
1426
+                    return vaultController.Delete(id);
1427
+            }
1428
+            return Json(new { error = "Invalid Type" });
1429
+        }
1399 1430
     }
1400 1431
 }

+ 1
- 4
Teknik/Areas/User/Views/User/ViewServiceData.cshtml View File

@@ -1,10 +1,7 @@
1 1
 @model Teknik.Areas.Users.ViewModels.ViewServiceDataViewModel
2 2
 
3 3
 <script>
4
-    var deleteUploadURL = '@Url.SubRouteUrl("u", "Upload.Delete")';
5
-    var deletePasteURL = '@Url.SubRouteUrl("p", "Paste.Delete")';
6
-    var deleteShortenURL = '@Url.SubRouteUrl("s", "Shortener.Delete")';
7
-    var deleteVaultURL = '@Url.SubRouteUrl("v", "Vault.Delete")';
4
+    var deleteDataURL = '@Url.SubRouteUrl("account", "User.Action", new { action = "DeleteData" })';
8 5
 </script>
9 6
 
10 7
 <div class="container">

+ 1
- 0
Teknik/Areas/Vault/Controllers/VaultController.cs View File

@@ -428,6 +428,7 @@ namespace Teknik.Areas.Vault.Controllers
428 428
         }
429 429
 
430 430
         [HttpPost]
431
+        [HttpOptions]
431 432
         public IActionResult Delete(string id)
432 433
         {
433 434
             Vault.Models.Vault foundVault = _dbContext.Vaults.Where(v => v.Url == id).FirstOrDefault();

+ 5
- 1
Teknik/Scripts/.eslintrc View File

@@ -22,6 +22,7 @@
22 22
         "importScripts": false,
23 23
         "CryptoJS": false,
24 24
         "Uint8Array": false,
25
+        "Oidc": false,
25 26
 
26 27
         // Common.js Functions
27 28
         "deleteConfirm": false,
@@ -46,6 +47,9 @@
46 47
         "isValidURL": false,
47 48
         "pageloadTimerCount": false,
48 49
         "pageloadDoTimer": false,
49
-        "pageloadStopTimer": false
50
+        "pageloadStopTimer": false,
51
+
52
+        // Common variables
53
+        "oidcConfig":  false
50 54
     }
51 55
 }

+ 11
- 9
Teknik/Scripts/User/ViewServiceData.js View File

@@ -1,27 +1,27 @@
1
-/* globals deleteUploadURL, deletePasteURL, deleteShortenURL, deleteVaultURL */
1
+/* globals deleteDataURL */
2 2
 $(document).ready(function () {
3 3
     $('.delete-upload-button').click(function () {
4 4
         var id = $(this).data('upload-id');
5 5
         var element = $('#uploads [id="' + id + '"');
6
-        deleteItem(deleteUploadURL, id, element, "Are you sure you want to delete this upload?");
6
+        deleteItem('upload', id, element, "Are you sure you want to delete this upload?");
7 7
     });
8 8
 
9 9
     $('.delete-paste-button').click(function () {
10 10
         var id = $(this).data('paste-id');
11 11
         var element = $('#pastes [id="' + id + '"');
12
-        deleteItem(deletePasteURL, id, element, "Are you sure you want to delete this paste?");
12
+        deleteItem('paste', id, element, "Are you sure you want to delete this paste?");
13 13
     });
14 14
 
15 15
     $('.delete-shorten-button').click(function () {
16 16
         var id = $(this).data('shorten-id');
17 17
         var element = $('#shortenedUrls [id="' + id + '"');
18
-        deleteItem(deleteShortenURL, id, element, "Are you sure you want to delete this shortened url?");
18
+        deleteItem('shortenedUrl', id, element, "Are you sure you want to delete this shortened url?");
19 19
     });
20 20
 
21 21
     $('.delete-vault-button').click(function () {
22 22
         var id = $(this).data('vault-id');
23 23
         var element = $('#vaults [id="' + id + '"');
24
-        deleteItem(deleteVaultURL, id, element, "Are you sure you want to delete this vault?");
24
+        deleteItem('vault', id, element, "Are you sure you want to delete this vault?");
25 25
     });
26 26
 
27 27
     $('a[data-toggle="tab"]').on('shown.bs.tab', function () {
@@ -36,14 +36,16 @@ $(document).ready(function () {
36 36
     }
37 37
 });
38 38
 
39
-function deleteItem(url, id, element, confirmationMsg) {
39
+function deleteItem(type, id, element, confirmationMsg) {
40 40
     deleteConfirm(confirmationMsg, function (result) {
41 41
         if (result) {
42 42
             $.ajax({
43 43
                 type: "POST",
44
-                url: url,
45
-                data: { id: id },
46
-                headers: { 'X-Requested-With': 'XMLHttpRequest' },
44
+                url: deleteDataURL,
45
+                data: AddAntiForgeryToken({ type: type, id: id }),
46
+                headers: {
47
+                    'X-Requested-With': 'XMLHttpRequest'
48
+                },
47 49
                 xhrFields: {
48 50
                     withCredentials: true
49 51
                 },

+ 1
- 0
Teknik/gulpfile.js View File

@@ -42,6 +42,7 @@ var assets = [
42 42
     { './node_modules/marked/lib/marked.js': 'lib/marked/js' },
43 43
     { './node_modules/sanitize-html/dist/sanitize-html.js': 'lib/sanitize-html/js' },
44 44
     { './node_modules/underscore/underscore.js': 'lib/underscore/js' },
45
+    { './node_modules/oidc-client/dist/oidc-client.js': 'lib/oidc-client/js' },
45 46
 
46 47
     // App JS Files
47 48
     { './Scripts/**/*': 'js/app' },

+ 857
- 803
Teknik/package-lock.json
File diff suppressed because it is too large
View File


+ 1
- 0
Teknik/package.json View File

@@ -16,6 +16,7 @@
16 16
         "jquery": "^3.3.1",
17 17
         "jquery-validation": "^1.17.0",
18 18
         "marked": "^0.4.0",
19
+        "oidc-client": "^1.6.1",
19 20
         "sanitize-html": "^1.18.2",
20 21
         "underscore": "^1.9.1"
21 22
     },

Loading…
Cancel
Save