Skip to content

Commit

Permalink
В веб-сервер добавлена возможность работы с данными форм.
Browse files Browse the repository at this point in the history
  • Loading branch information
akpaevj committed Dec 6, 2024
1 parent 0aea6cd commit ba904f5
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 105 deletions.
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 Retrieve(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 BslNullValue.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 ba904f5

Please sign in to comment.