mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
Add ability to set directory to store sessions
Now user can set directory to store sessions somewhere else other than the folder where the program's executable is. (Based on patch from Felony <onyfel@gmail.com> contributed in https://github.com/sochix/TLSharp/pull/874 )
This commit is contained in:
parent
8a15feea7c
commit
e4d0e97571
|
|
@ -14,9 +14,24 @@ namespace TLSharp.Core
|
||||||
|
|
||||||
public class FileSessionStore : ISessionStore
|
public class FileSessionStore : ISessionStore
|
||||||
{
|
{
|
||||||
|
private readonly DirectoryInfo basePath;
|
||||||
|
|
||||||
|
public FileSessionStore(DirectoryInfo basePath = null)
|
||||||
|
{
|
||||||
|
if (basePath != null && !basePath.Exists)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("basePath doesn't exist", nameof(basePath));
|
||||||
|
}
|
||||||
|
this.basePath = basePath;
|
||||||
|
}
|
||||||
|
|
||||||
public void Save(Session session)
|
public void Save(Session session)
|
||||||
{
|
{
|
||||||
using (var stream = new FileStream($"{session.SessionUserId}.dat", FileMode.OpenOrCreate))
|
string sessionFileName = $"{session.SessionUserId}.dat";
|
||||||
|
var sessionPath = basePath == null ? sessionFileName :
|
||||||
|
Path.Combine(basePath.FullName, sessionFileName);
|
||||||
|
|
||||||
|
using (var stream = new FileStream(sessionPath, FileMode.OpenOrCreate))
|
||||||
{
|
{
|
||||||
var result = session.ToBytes();
|
var result = session.ToBytes();
|
||||||
stream.Write(result, 0, result.Length);
|
stream.Write(result, 0, result.Length);
|
||||||
|
|
@ -25,11 +40,14 @@ namespace TLSharp.Core
|
||||||
|
|
||||||
public Session Load(string sessionUserId)
|
public Session Load(string sessionUserId)
|
||||||
{
|
{
|
||||||
var sessionFileName = $"{sessionUserId}.dat";
|
string sessionFileName = $"{sessionUserId}.dat";
|
||||||
if (!File.Exists(sessionFileName))
|
var sessionPath = basePath == null ? sessionFileName :
|
||||||
|
Path.Combine(basePath.FullName, sessionFileName);
|
||||||
|
|
||||||
|
if (!File.Exists(sessionPath))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
using (var stream = new FileStream(sessionFileName, FileMode.Open))
|
using (var stream = new FileStream(sessionPath, FileMode.Open))
|
||||||
{
|
{
|
||||||
var buffer = new byte[2048];
|
var buffer = new byte[2048];
|
||||||
stream.Read(buffer, 0, 2048);
|
stream.Read(buffer, 0, 2048);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue