add TempStorage class
This commit is contained in:
parent
fab11e9c90
commit
252a372477
@ -10,10 +10,12 @@ namespace SimpleTGBot;
|
|||||||
public class TelegramBot
|
public class TelegramBot
|
||||||
{
|
{
|
||||||
private string token;
|
private string token;
|
||||||
|
private TempStorage temp;
|
||||||
|
|
||||||
public TelegramBot(string token)
|
public TelegramBot(string token)
|
||||||
{
|
{
|
||||||
this.token = token;
|
this.token = token;
|
||||||
|
temp = new TempStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -50,6 +52,7 @@ public class TelegramBot
|
|||||||
|
|
||||||
botQuit:
|
botQuit:
|
||||||
cts.Cancel();
|
cts.Cancel();
|
||||||
|
temp.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
56
SimpleTGBot/TempStorage.cs
Normal file
56
SimpleTGBot/TempStorage.cs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
namespace SimpleTGBot;
|
||||||
|
|
||||||
|
internal class TempStorage : IDisposable
|
||||||
|
{
|
||||||
|
private string directory;
|
||||||
|
private Dictionary<FileStream, string> createdTempFiles;
|
||||||
|
private Random rng;
|
||||||
|
|
||||||
|
public TempStorage()
|
||||||
|
{
|
||||||
|
rng = new Random();
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
directory = Path.GetTempPath() + "/demotivatorBot-" + RandomHexString(4);
|
||||||
|
} while (Directory.Exists(directory));
|
||||||
|
Directory.CreateDirectory(directory);
|
||||||
|
|
||||||
|
createdTempFiles = new Dictionary<FileStream, string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
foreach (var kv in createdTempFiles)
|
||||||
|
{
|
||||||
|
kv.Key.Dispose();
|
||||||
|
if (File.Exists(kv.Value))
|
||||||
|
{
|
||||||
|
File.Delete(kv.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Directory.Delete(directory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public (string, FileStream) newTemporaryFile(string? prefix, string? extension)
|
||||||
|
{
|
||||||
|
string filename = directory + "/" + (prefix != null ? prefix + "-" : "") + RandomHexString(8) + (extension != null ? "." + extension : "");
|
||||||
|
FileStream file = File.Open(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
|
||||||
|
createdTempFiles.Add(file, filename);
|
||||||
|
return (filename, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteTemporaryFile(FileStream file)
|
||||||
|
{
|
||||||
|
if (createdTempFiles.ContainsKey(file))
|
||||||
|
{
|
||||||
|
File.Delete(createdTempFiles[file]);
|
||||||
|
}
|
||||||
|
file.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
private string RandomHexString(int n)
|
||||||
|
{
|
||||||
|
return rng.NextInt64(1L << (n << 2)).ToString("X" + n);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user