Browse Source

Added paste file cleanup and error handling

pull/111/head
Teknikode 11 months ago
parent
commit
3ffac5959f
  1. 60
      ServiceWorker/Program.cs
  2. 2
      ServiceWorker/Properties/PublishProfiles/Teknik Service Worker.pubxml

60
ServiceWorker/Program.cs

@ -144,12 +144,6 @@ namespace Teknik.ServiceWorker @@ -144,12 +144,6 @@ namespace Teknik.ServiceWorker
}
Task.WaitAll(runningTasks.ToArray());
}
bool running = true;
//while (running)
//{
// running = runningTasks.Exists(s => s != null && !s.IsCompleted && !s.IsCanceled && !s.IsFaulted);
//}
Output(string.Format("Scanning Complete. {0} Scanned | {1} Viruses Found | {2} Total Files", totalScans, totalViruses, totalCount));
}
@ -208,10 +202,7 @@ namespace Teknik.ServiceWorker @@ -208,10 +202,7 @@ namespace Teknik.ServiceWorker
db.Uploads.Remove(upload);
// Delete the File
if (File.Exists(filePath))
{
File.Delete(filePath);
}
DeleteFile(filePath);
// Add to transparency report if any were found
Takedown report = new Takedown();
@ -259,12 +250,8 @@ namespace Teknik.ServiceWorker @@ -259,12 +250,8 @@ namespace Teknik.ServiceWorker
string filePath = Path.Combine(config.UploadConfig.UploadDirectory, subDir, upload.FileName);
// Delete the File
if (File.Exists(filePath))
{
File.Delete(filePath);
}
DeleteFile(filePath);
}
db.RemoveRange(uploads);
db.SaveChanges();
@ -277,10 +264,7 @@ namespace Teknik.ServiceWorker @@ -277,10 +264,7 @@ namespace Teknik.ServiceWorker
string filePath = Path.Combine(config.PasteConfig.PasteDirectory, subDir, paste.FileName);
// Delete the File
if (File.Exists(filePath))
{
File.Delete(filePath);
}
DeleteFile(filePath);
}
db.RemoveRange(pastes);
db.SaveChanges();
@ -288,18 +272,48 @@ namespace Teknik.ServiceWorker @@ -288,18 +272,48 @@ namespace Teknik.ServiceWorker
public static void CleanStorage(Config config, TeknikEntities db)
{
var curDate = DateTime.Now;
// Process upload data
Output(string.Format("[{0}] Starting processing upload storage cleaning.", DateTime.Now));
CleanUploadFiles(config, db);
// Process paste data
Output(string.Format("[{0}] Starting processing upload storage cleaning.", DateTime.Now));
CleanPasteFiles(config, db);
}
List<string> uploads = db.Uploads.Select(u => Path.Combine(config.UploadConfig.UploadDirectory, u.FileName[0].ToString(), u.FileName)).Select(u => u.ToLower()).ToList();
public static void CleanUploadFiles(Config config, TeknikEntities db)
{
List<string> uploads = db.Uploads.Where(u => !string.IsNullOrEmpty(u.FileName)).Select(u => Path.Combine(config.UploadConfig.UploadDirectory, u.FileName[0].ToString(), u.FileName)).Select(u => u.ToLower()).ToList();
List<string> files = Directory.GetFiles(config.UploadConfig.UploadDirectory, "*.*", SearchOption.AllDirectories).Select(f => f.ToLower()).ToList();
var orphans = files.Except(uploads);
File.AppendAllLines(orphansFile, orphans);
foreach (var orphan in orphans)
{
File.Delete(orphan);
DeleteFile(orphan);
}
}
public static void CleanPasteFiles(Config config, TeknikEntities db)
{
List<string> pastes = db.Pastes.Where(p => !string.IsNullOrEmpty(p.FileName)).Select(p => Path.Combine(config.PasteConfig.PasteDirectory, p.FileName[0].ToString(), p.FileName)).Select(p => p.ToLower()).ToList();
List<string> files = Directory.GetFiles(config.PasteConfig.PasteDirectory, "*.*", SearchOption.AllDirectories).Select(f => f.ToLower()).ToList();
var orphans = files.Except(pastes);
File.AppendAllLines(orphansFile, orphans);
foreach (var orphan in orphans)
{
DeleteFile(orphan);
}
}
public static void DeleteFile(string filePath)
{
try
{
File.Delete(filePath);
}
catch (Exception ex)
{
Output(string.Format("[{0}] Unable to delete file: {1} | {2}", DateTime.Now, filePath, ex.ToString()));
}
}

2
ServiceWorker/Properties/PublishProfiles/Teknik Service Worker.pubxml

@ -7,7 +7,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. @@ -7,7 +7,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<PublishDir>Output\</PublishDir>
<SelfContained>false</SelfContained>
</PropertyGroup>

Loading…
Cancel
Save