Skip to content

Commit 72229c7

Browse files
authored
Merge pull request EvilBeaver#1476 from akpaevj/feature/form-data
В веб-сервер добавлена возможность работы с данными форм.
2 parents cb998f2 + 320db59 commit 72229c7

11 files changed

+407
-123
lines changed

src/OneScript.Web.Server/CookieOptionsWrapper.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ public IValue Domain
2424
get
2525
{
2626
if (_cookieOptions.Domain == null)
27-
return BslNullValue.Instance;
27+
return BslUndefinedValue.Instance;
2828
else
2929
return BslStringValue.Create(_cookieOptions.Domain);
3030
}
3131
set
3232
{
33-
if (value is BslNullValue)
33+
if (value is BslUndefinedValue)
3434
_cookieOptions.Domain = null;
3535
else
3636
_cookieOptions.Domain = value.AsString();
@@ -40,10 +40,10 @@ public IValue Domain
4040
[ContextProperty("Путь", "Path")]
4141
public IValue Path
4242
{
43-
get => _cookieOptions.Path == null ? BslNullValue.Instance : BslStringValue.Create(_cookieOptions.Path);
43+
get => _cookieOptions.Path == null ? BslUndefinedValue.Instance : BslStringValue.Create(_cookieOptions.Path);
4444
set
4545
{
46-
if (value is BslNullValue)
46+
if (value is BslUndefinedValue)
4747
_cookieOptions.Path = null;
4848
else
4949
_cookieOptions.Path = value.AsString();
@@ -58,11 +58,11 @@ public IValue Expires
5858
if (_cookieOptions.Expires.HasValue)
5959
return BslDateValue.Create(_cookieOptions.Expires.Value.UtcDateTime);
6060
else
61-
return BslNullValue.Instance;
61+
return BslUndefinedValue.Instance;
6262
}
6363
set
6464
{
65-
if (value is BslNullValue)
65+
if (value is BslUndefinedValue)
6666
_cookieOptions.Expires = null;
6767
else
6868
_cookieOptions.Expires = new DateTimeOffset(value.AsDate());
@@ -98,11 +98,11 @@ public IValue MaxAge
9898
if (_cookieOptions.MaxAge.HasValue)
9999
return BslNumericValue.Create((decimal)_cookieOptions.MaxAge.Value.TotalSeconds);
100100
else
101-
return BslNullValue.Instance;
101+
return BslUndefinedValue.Instance;
102102
}
103103
set
104104
{
105-
if (value is BslNullValue)
105+
if (value is BslUndefinedValue)
106106
_cookieOptions.MaxAge = null;
107107
else
108108
_cookieOptions.MaxAge = TimeSpan.FromSeconds((double)value.AsNumber());
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 Get(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 BslUndefinedValue.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)