SessionFiles.cs

173 lines | 4.511 kB Blame History Raw Download
using Newtonsoft.Json;
using System.Globalization;
using System.IO;
using System.Net.WebSockets;
using System.Text;
using System.Text.Json.Serialization;

namespace CodeEditorApi.Helpers
{
    public static class SessionFiles
    {
        private static string sessionPath = "database/";
        public static Session Create(Guid sessionId)
        {    
            if (CheckForDirectoryExists(sessionId))
            {
                throw new Exception("Session already exists");
            }
            else
            {
                CreateDirectory(sessionId);
            }

            return GetSession(sessionPath + "/" + sessionId);
        }

        public static void Save(SessionData data)
        {
            if (!CheckForDirectoryExists(data.SessionId))
            {
                CreateDirectory(data.SessionId);
            }

            WriteFile(data);
        }

        public static Session[] Get()
        {
            var dir = Directory.GetDirectories(sessionPath);
            var res = new List<Session>();

            foreach (var folder in dir)
            {
                var files = Directory.GetFiles(folder);

                res.Add(GetSession(folder));
            }

            return res.Where(x => x.Timestamp > DateTime.UtcNow.AddMonths(-1)).OrderByDescending(x => x.Timestamp).ToArray();
        }

        public static SessionData[] Get(Guid sessionId)
        {
            var fileNames = Directory.GetFiles(sessionPath + "/" + sessionId);
            var res = new List<SessionData>();

            foreach (var fileName in fileNames)
            {
                res.Add(GetSessionData(sessionId, fileName));
            }

            return res.ToArray();
        }

        private static bool CheckForDirectoryExists(Guid sessionId)
        {
            if (Directory.Exists(sessionPath + sessionId))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private static bool CreateDirectory(Guid sessionId)
        {
            Directory.CreateDirectory(sessionPath + sessionId);
            return true;
        }

        private static bool CreateFile(SessionData data)
        {
            if (!File.Exists(sessionPath + data.FileName))
            {
                using (FileStream fs = File.Create(sessionPath + data.FileName))
                {
                }

            }

            return true;
        }

        private static bool WriteFile(SessionData data)
        {
            CreateFile(data);
            using (StreamWriter sw = File.CreateText(sessionPath + data.FileName))
            {
               sw.Write(data.Data);
            }

            return true;
        }

        private static DataType GetExtension(string fileName)
        {
            if (fileName.EndsWith(".js"))
            {
                return DataType.Javascript;
            }
            else if (fileName.EndsWith(".css"))
            {
                return DataType.CSS;
            }
            else if (fileName.EndsWith(".html"))
            {
                return DataType.HTML;
            }

            return DataType.Text;
        }

        private static Session GetSession(string path)
        {
            var files = Directory.GetFiles(path);

            var strId = path.Substring(path.LastIndexOf("/") + 1);

            var session = new Session
            {
                Id = Guid.Parse(strId),
                NumberOfChanges = files.Length,
                Timestamp = Directory.GetCreationTime(path)
            };

            return session;
        }

        private static SessionData GetSessionData(Guid sessionId, string path)
        {
            SessionData sessionData;

            using (StreamReader sr = new StreamReader(path))
            {
                var content = sr.ReadToEnd();

                sessionData = new SessionData
                {
                    SessionId = sessionId,
                    Data = content,
                    Type = GetExtension(path),
                    Timestamp = DateTime.Now
                };
            }

            return sessionData;
        }

        public static string GetFormattedDate(DateTime date)
        {
            return date.ToString("yyyy-MM-ddTHH-mm-ss-fff");
        }

        public static DateTime GetDateFromFormat(string date)
        {
            CultureInfo provider = CultureInfo.InvariantCulture;
            return DateTime.ParseExact(date, "yyyy-MM-ddTHH-mm-ss-fff", provider);
        }
    }
}