add to git

This commit is contained in:
David Meincke
2023-05-12 09:43:35 +02:00
commit 3fa130d282
64 changed files with 431 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Database\" />
<Folder Include="Database\9ec274a6-f2a1-4629-b52a-a0d5e72eb4b2\" />
</ItemGroup>
</Project>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<NameOfLastUsedPublishProfile>C:\Projects\code-editor-api\Application\Properties\PublishProfiles\FTPProfile.pubxml</NameOfLastUsedPublishProfile>
</PropertyGroup>
</Project>
+13
View File
@@ -0,0 +1,13 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33627.172
MinimumVisualStudioVersion = 10.0.40219.1
Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1746F93F-3E42-4832-B5C1-834781B0426F}
EndGlobalSection
EndGlobal
@@ -0,0 +1,41 @@
using CodeEditorApi.Helpers;
using Microsoft.AspNetCore.Mvc;
namespace CodeEditorApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class SessionController : ControllerBase
{
private readonly ILogger<SessionController> _logger;
public SessionController(ILogger<SessionController> logger)
{
_logger = logger;
}
[HttpPost("create")]
public Session CreateSession([FromBody] Session newSession)
{
return SessionFiles.Create(newSession.Id);
}
[HttpPost("set")]
public void SetData([FromBody] SessionData data)
{
SessionFiles.Save(data);
}
[HttpGet("all")]
public IEnumerable<Session> Sessions()
{
return SessionFiles.Get();
}
[HttpGet("data/{sessionId}")]
public IEnumerable<SessionData> SessionData(Guid sessionId)
{
return SessionFiles.Get(sessionId);
}
}
}
@@ -0,0 +1 @@
console.log("HELLO");
@@ -0,0 +1 @@
var myJs = true;
@@ -0,0 +1 @@
var myJs = true; var myOtherThing = 'string'
@@ -0,0 +1 @@
var myJs = true; var myOtherThing = 'string'
@@ -0,0 +1 @@
console.log("Hello")
@@ -0,0 +1 @@
console.log("Hello 2.01")
@@ -0,0 +1 @@
console.log("Hello 3.1")
@@ -0,0 +1 @@
console.log("Hello 3.1");
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
@@ -0,0 +1 @@
box(10, 10, 10, 10)
+171
View File
@@ -0,0 +1,171 @@
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.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
};
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);
}
}
}
+24
View File
@@ -0,0 +1,24 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.UseCors(options =>
{
options.AllowAnyHeader();
options.AllowAnyMethod();
options.AllowAnyOrigin();
});
app.Run();
+8
View File
@@ -0,0 +1,8 @@
namespace CodeEditorApi
{
public class Session
{
public Guid Id { get; set; }
public int NumberOfChanges { get; set; }
}
}
+46
View File
@@ -0,0 +1,46 @@
namespace CodeEditorApi
{
public class SessionData
{
public DateTime Timestamp { get; set; }
public Guid SessionId { get; set; }
public object Data { get; set; } = default!;
public DataType Type { get; set; }
public string FileName
{
get
{
return SessionId.ToString() + "/" + Helpers.SessionFiles.GetFormattedDate(Timestamp) + FileExtension;
}
}
public string FileExtension {
get
{
if (Type == DataType.Javascript)
{
return ".js";
}
else if (Type == DataType.CSS)
{
return ".css";
}
else if (Type == DataType.HTML)
{
return ".html";
}
return ".txt";
}
}
}
public enum DataType
{
Javascript,
HTML,
CSS,
Text
}
}
+8
View File
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
+9
View File
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}