131 lines
4.4 KiB
C#
Raw Normal View History

2021-11-12 15:00:41 +05:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ShowTrend3.Libs
{
public static class TempDirectory
{
private static readonly char sl = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? '/' : '\\';
private static readonly string td = Directory.GetCurrentDirectory() + sl + "temp";
private static Dictionary<string, int> readFiles = new Dictionary<string, int>();
private static List<string> writeFiles = new List<string>();
private static long DirectorySize(DirectoryInfo d)
{
long Size = 0;
FileInfo[] fis = d.GetFiles();
foreach (FileInfo fi in fis)
Size += fi.Length;
DirectoryInfo[] dis = d.GetDirectories();
foreach (DirectoryInfo di in dis)
Size += DirectorySize(di);
return (Size);
}
public static bool Clear(long maxSize = 1073741824)
{
try
{
if (!Directory.Exists(td)) Directory.CreateDirectory(td);
var files = new DirectoryInfo(td).GetFiles();
foreach (var f in files)
{
var t = f.Name.Split('.');
var dateData = new DateTime(int.Parse(t[0]), int.Parse(t[1]), int.Parse(t[2]));
if (readFiles.ContainsKey(f.Name) ||
writeFiles.Contains(f.Name) ||
(f.CreationTime - dateData).TotalHours > 25 ||
(DateTime.Now - f.CreationTime).TotalMinutes <= 2)
continue;
f.Delete();
}
if (DirectorySize(new DirectoryInfo(td)) < maxSize)
return true;
files = new DirectoryInfo(td).GetFiles();
foreach(var f in files)
{
if (readFiles.ContainsKey(f.Name) ||
writeFiles.Contains(f.Name))
continue;
f.Delete();
}
return true;
}
catch (Exception e)
{
Console.WriteLine(CustomConverters.ExceptionToString(e));
return false;
}
}
public static async Task<bool> Delete(string name)
{
bool flag = false;
try
{
if ( !writeFiles.Contains(name) && !File.Exists(td + sl + name))
return true;
while (writeFiles.Contains(name))
await Task.Delay(100);
writeFiles.Add(name);
flag = true;
while (readFiles.ContainsKey(name))
await Task.Delay(100);
File.Delete(td + sl + name);
writeFiles.Remove(name);
return true;
}
catch(Exception e)
{
Console.WriteLine(CustomConverters.ExceptionToString(e));
if(flag) writeFiles.Remove(name);
return false;
}
}
public static bool IsFileExist(string name)
{
return File.Exists(td + sl + name);
}
public static FileStream StartWrite(string name)
{
writeFiles.Add(name);
return new FileStream(td + sl + name, FileMode.CreateNew);
}
public static void EndWrite(string name)
{
writeFiles.Remove(name);
}
public static void LockRead(string name)
{
if (!readFiles.ContainsKey(name))
readFiles.Add(name, 0);
readFiles[name]++;
}
public static bool CanRead(string name)
{
if (!IsFileExist(name))
return false;
return !writeFiles.Contains(name);
}
public static FileStream StartRead(string name)
{
return new FileStream(td + sl + name, FileMode.Open);
}
public static void EndRead(string name)
{
if (!readFiles.ContainsKey(name))
return;
if (readFiles[name] > 0)
readFiles[name]--;
else
readFiles.Remove(name);
}
}
}