From 1fc0e31a14021fbe26a643dfff33758e1c07d8a0 Mon Sep 17 00:00:00 2001 From: Felony Date: Wed, 2 Oct 2019 18:23:38 +0330 Subject: [PATCH] Add ability to set session storage path in FileSessionStore --- TLSharp.Core/Session.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/TLSharp.Core/Session.cs b/TLSharp.Core/Session.cs index 6264770..bb4967b 100644 --- a/TLSharp.Core/Session.cs +++ b/TLSharp.Core/Session.cs @@ -14,9 +14,18 @@ namespace TLSharp.Core public class FileSessionStore : ISessionStore { + private readonly string _storePath; + + public FileSessionStore(string storePath = "") + { + _storePath = storePath; + } + public void Save(Session session) { - using (var stream = new FileStream($"{session.SessionUserId}.dat", FileMode.OpenOrCreate)) + string sessionPath = Path.Combine(_storePath, $"{session.SessionUserId}.dat"); + + using (var stream = new FileStream(sessionPath, FileMode.OpenOrCreate)) { var result = session.ToBytes(); stream.Write(result, 0, result.Length); @@ -25,11 +34,12 @@ namespace TLSharp.Core public Session Load(string sessionUserId) { - var sessionFileName = $"{sessionUserId}.dat"; - if (!File.Exists(sessionFileName)) + string sessionPath = Path.Combine(_storePath, $"{sessionUserId}.dat"); + + if (!File.Exists(sessionPath)) return null; - using (var stream = new FileStream(sessionFileName, FileMode.Open)) + using (var stream = new FileStream(sessionPath, FileMode.Open)) { var buffer = new byte[2048]; stream.Read(buffer, 0, 2048);