Skip to content

Commit ba904f5

Browse files
committed
В веб-сервер добавлена возможность работы с данными форм.
1 parent 0aea6cd commit ba904f5

File tree

8 files changed

+389
-105
lines changed

8 files changed

+389
-105
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
using Microsoft.AspNetCore.Http;
8+
using OneScript.Contexts;
9+
using ScriptEngine.Machine;
10+
using ScriptEngine.Machine.Contexts;
11+
using OneScript.StandardLibrary.Collections;
12+
using System.Collections.Generic;
13+
using OneScript.StandardLibrary.Binary;
14+
using Microsoft.Extensions.Primitives;
15+
16+
namespace OneScript.Web.Server
17+
{
18+
[ContextClass("Форма", "Form")]
19+
public class FormCollectionWrapper : AutoCollectionContext<FormCollectionWrapper, KeyAndValueImpl>
20+
{
21+
private readonly IFormCollection _items;
22+
23+
internal FormCollectionWrapper(IFormCollection headers)
24+
{
25+
_items = headers;
26+
}
27+
28+
public override bool IsIndexed => true;
29+
30+
public override StringValuesWrapper GetIndexedValue(IValue index)
31+
{
32+
if (_items.TryGetValue(index.AsString(), out var result))
33+
return result;
34+
else
35+
return StringValues.Empty;
36+
}
37+
38+
internal bool ContainsKey(IValue key)
39+
{
40+
return _items.ContainsKey(key.AsString());
41+
}
42+
43+
public IEnumerable<IValue> Keys()
44+
{
45+
foreach (var key in _items.Keys)
46+
yield return ValueFactory.Create(key);
47+
}
48+
49+
#region ICollectionContext Members
50+
51+
[ContextMethod("Получить", "Get")]
52+
public StringValuesWrapper Retrieve(IValue key)
53+
{
54+
return GetIndexedValue(key);
55+
}
56+
57+
[ContextMethod("Количество", "Count")]
58+
public override int Count()
59+
{
60+
return _items.Count;
61+
}
62+
63+
#endregion
64+
65+
#region IEnumerable<IValue> Members
66+
67+
public override IEnumerator<KeyAndValueImpl> GetEnumerator()
68+
{
69+
foreach (var item in _items)
70+
{
71+
yield return new KeyAndValueImpl(ValueFactory.Create(item.Key), (StringValuesWrapper)item.Value);
72+
}
73+
}
74+
75+
#endregion
76+
77+
[ContextProperty("Файлы", "Files", CanWrite = false)]
78+
public FormFileCollectionWrapper Files => new(_items.Files);
79+
}
80+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
using Microsoft.AspNetCore.Http;
8+
using OneScript.Contexts;
9+
using ScriptEngine.Machine;
10+
using ScriptEngine.Machine.Contexts;
11+
using System.Collections.Generic;
12+
using OneScript.Values;
13+
using OneScript.StandardLibrary.Collections;
14+
15+
namespace OneScript.Web.Server
16+
{
17+
[ContextClass("ФайлыФормы", "FormFiles")]
18+
public class FormFileCollectionWrapper : AutoCollectionContext<FormFileCollectionWrapper, FormFileWrapper>
19+
{
20+
private readonly IFormFileCollection _items;
21+
22+
internal FormFileCollectionWrapper(IFormFileCollection items)
23+
{
24+
_items = items;
25+
}
26+
27+
public override bool IsIndexed => true;
28+
29+
public override IValue GetIndexedValue(IValue index)
30+
{
31+
var result = _items.GetFile(index.AsString());
32+
33+
if (result == null)
34+
return BslNullValue.Instance;
35+
else
36+
return new FormFileWrapper(result);
37+
}
38+
39+
#region ICollectionContext Members
40+
41+
[ContextMethod("Получить", "Get")]
42+
public IValue Retrieve(IValue key)
43+
{
44+
return GetIndexedValue(key);
45+
}
46+
47+
[ContextMethod("Количество", "Count")]
48+
public override int Count()
49+
{
50+
return _items.Count;
51+
}
52+
53+
#endregion
54+
55+
#region IEnumerable<IValue> Members
56+
57+
public override IEnumerator<FormFileWrapper> GetEnumerator()
58+
{
59+
foreach (var item in _items)
60+
yield return new FormFileWrapper(item);
61+
}
62+
63+
#endregion
64+
}
65+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
using Microsoft.AspNetCore.Http;
8+
using OneScript.Contexts;
9+
using ScriptEngine.Machine;
10+
using ScriptEngine.Machine.Contexts;
11+
using OneScript.Values;
12+
using OneScript.StandardLibrary.Binary;
13+
14+
namespace OneScript.Web.Server
15+
{
16+
[ContextClass("ФайлФормы", "FormFile")]
17+
public class FormFileWrapper : AutoContext<FormFileWrapper>
18+
{
19+
private readonly IFormFile _item;
20+
21+
internal FormFileWrapper(IFormFile item)
22+
{
23+
_item = item;
24+
}
25+
26+
[ContextProperty("ТипКонтента", "ContentType", CanWrite = false)]
27+
public IValue ContentType => BslStringValue.Create(_item.ContentType);
28+
29+
[ContextProperty("РасположениеКонтента", "ContentDisposition", CanWrite = false)]
30+
public IValue ContentDisposition => BslStringValue.Create(_item.ContentDisposition);
31+
32+
[ContextProperty("Заголовки", "Headers", CanWrite = false)]
33+
public HeaderDictionaryWrapper Headers => new(_item.Headers);
34+
35+
[ContextProperty("Длина", "Length", CanWrite = false)]
36+
public IValue Length => BslNumericValue.Create(_item.Length);
37+
38+
[ContextProperty("Имя", "Name", CanWrite = false)]
39+
public IValue Name => BslStringValue.Create(_item.Name);
40+
41+
[ContextProperty("ИмяФайла", "FileName", CanWrite = false)]
42+
public IValue FileName => BslStringValue.Create(_item.FileName);
43+
44+
[ContextMethod("ОткрытьПотокЧтения", "OpenReadStream")]
45+
public GenericStream OpenReadStream() => new(_item.OpenReadStream());
46+
}
47+
}

0 commit comments

Comments
 (0)