Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

В веб-сервер добавлена возможность работы с данными форм. #1476

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
akpaevj marked this conversation as resolved.
Show resolved Hide resolved

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)
akpaevj marked this conversation as resolved.
Show resolved Hide resolved
{
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