Skip to content

Commit

Permalink
Merge pull request #1476 from akpaevj/feature/form-data
Browse files Browse the repository at this point in the history
В веб-сервер добавлена возможность работы с данными форм.
  • Loading branch information
EvilBeaver authored Dec 13, 2024
2 parents cb998f2 + 320db59 commit 72229c7
Show file tree
Hide file tree
Showing 11 changed files with 407 additions and 123 deletions.
16 changes: 8 additions & 8 deletions src/OneScript.Web.Server/CookieOptionsWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public IValue Domain
get
{
if (_cookieOptions.Domain == null)
return BslNullValue.Instance;
return BslUndefinedValue.Instance;
else
return BslStringValue.Create(_cookieOptions.Domain);
}
set
{
if (value is BslNullValue)
if (value is BslUndefinedValue)
_cookieOptions.Domain = null;
else
_cookieOptions.Domain = value.AsString();
Expand All @@ -40,10 +40,10 @@ public IValue Domain
[ContextProperty("Путь", "Path")]
public IValue Path
{
get => _cookieOptions.Path == null ? BslNullValue.Instance : BslStringValue.Create(_cookieOptions.Path);
get => _cookieOptions.Path == null ? BslUndefinedValue.Instance : BslStringValue.Create(_cookieOptions.Path);
set
{
if (value is BslNullValue)
if (value is BslUndefinedValue)
_cookieOptions.Path = null;
else
_cookieOptions.Path = value.AsString();
Expand All @@ -58,11 +58,11 @@ public IValue Expires
if (_cookieOptions.Expires.HasValue)
return BslDateValue.Create(_cookieOptions.Expires.Value.UtcDateTime);
else
return BslNullValue.Instance;
return BslUndefinedValue.Instance;
}
set
{
if (value is BslNullValue)
if (value is BslUndefinedValue)
_cookieOptions.Expires = null;
else
_cookieOptions.Expires = new DateTimeOffset(value.AsDate());
Expand Down Expand Up @@ -98,11 +98,11 @@ public IValue MaxAge
if (_cookieOptions.MaxAge.HasValue)
return BslNumericValue.Create((decimal)_cookieOptions.MaxAge.Value.TotalSeconds);
else
return BslNullValue.Instance;
return BslUndefinedValue.Instance;
}
set
{
if (value is BslNullValue)
if (value is BslUndefinedValue)
_cookieOptions.MaxAge = null;
else
_cookieOptions.MaxAge = TimeSpan.FromSeconds((double)value.AsNumber());
Expand Down
80 changes: 80 additions & 0 deletions src/OneScript.Web.Server/FormCollectionWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using Microsoft.AspNetCore.Http;
using OneScript.Contexts;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;
using OneScript.StandardLibrary.Collections;
using System.Collections.Generic;
using OneScript.StandardLibrary.Binary;
using Microsoft.Extensions.Primitives;

namespace OneScript.Web.Server
{
[ContextClass("Форма", "Form")]
public class FormCollectionWrapper : AutoCollectionContext<FormCollectionWrapper, KeyAndValueImpl>
{
private readonly IFormCollection _items;

internal FormCollectionWrapper(IFormCollection headers)
{
_items = headers;
}

public override bool IsIndexed => true;

public override StringValuesWrapper GetIndexedValue(IValue index)
{
if (_items.TryGetValue(index.AsString(), out var result))
return result;
else
return StringValues.Empty;
}

internal bool ContainsKey(IValue key)
{
return _items.ContainsKey(key.AsString());
}

public IEnumerable<IValue> Keys()
{
foreach (var key in _items.Keys)
yield return ValueFactory.Create(key);
}

#region ICollectionContext Members

[ContextMethod("Получить", "Get")]
public StringValuesWrapper Get(IValue key)
{
return GetIndexedValue(key);
}

[ContextMethod("Количество", "Count")]
public override int Count()
{
return _items.Count;
}

#endregion

#region IEnumerable<IValue> Members

public override IEnumerator<KeyAndValueImpl> GetEnumerator()
{
foreach (var item in _items)
{
yield return new KeyAndValueImpl(ValueFactory.Create(item.Key), (StringValuesWrapper)item.Value);
}
}

#endregion

[ContextProperty("Файлы", "Files", CanWrite = false)]
public FormFileCollectionWrapper Files => new(_items.Files);
}
}
65 changes: 65 additions & 0 deletions src/OneScript.Web.Server/FormFileCollectionWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using Microsoft.AspNetCore.Http;
using OneScript.Contexts;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;
using System.Collections.Generic;
using OneScript.Values;
using OneScript.StandardLibrary.Collections;

namespace OneScript.Web.Server
{
[ContextClass("ФайлыФормы", "FormFiles")]
public class FormFileCollectionWrapper : AutoCollectionContext<FormFileCollectionWrapper, FormFileWrapper>
{
private readonly IFormFileCollection _items;

internal FormFileCollectionWrapper(IFormFileCollection items)
{
_items = items;
}

public override bool IsIndexed => true;

public override IValue GetIndexedValue(IValue index)
{
var result = _items.GetFile(index.AsString());

if (result == null)
return BslUndefinedValue.Instance;
else
return new FormFileWrapper(result);
}

#region ICollectionContext Members

[ContextMethod("Получить", "Get")]
public IValue Retrieve(IValue key)
{
return GetIndexedValue(key);
}

[ContextMethod("Количество", "Count")]
public override int Count()
{
return _items.Count;
}

#endregion

#region IEnumerable<IValue> Members

public override IEnumerator<FormFileWrapper> GetEnumerator()
{
foreach (var item in _items)
yield return new FormFileWrapper(item);
}

#endregion
}
}
47 changes: 47 additions & 0 deletions src/OneScript.Web.Server/FormFileWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using Microsoft.AspNetCore.Http;
using OneScript.Contexts;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;
using OneScript.Values;
using OneScript.StandardLibrary.Binary;

namespace OneScript.Web.Server
{
[ContextClass("ФайлФормы", "FormFile")]
public class FormFileWrapper : AutoContext<FormFileWrapper>
{
private readonly IFormFile _item;

internal FormFileWrapper(IFormFile item)
{
_item = item;
}

[ContextProperty("ТипКонтента", "ContentType", CanWrite = false)]
public IValue ContentType => BslStringValue.Create(_item.ContentType);

[ContextProperty("РасположениеКонтента", "ContentDisposition", CanWrite = false)]
public IValue ContentDisposition => BslStringValue.Create(_item.ContentDisposition);

[ContextProperty("Заголовки", "Headers", CanWrite = false)]
public HeaderDictionaryWrapper Headers => new(_item.Headers);

[ContextProperty("Длина", "Length", CanWrite = false)]
public IValue Length => BslNumericValue.Create(_item.Length);

[ContextProperty("Имя", "Name", CanWrite = false)]
public IValue Name => BslStringValue.Create(_item.Name);

[ContextProperty("ИмяФайла", "FileName", CanWrite = false)]
public IValue FileName => BslStringValue.Create(_item.FileName);

[ContextMethod("ОткрытьПотокЧтения", "OpenReadStream")]
public GenericStream OpenReadStream() => new(_item.OpenReadStream());
}
}
Loading

0 comments on commit 72229c7

Please sign in to comment.