Repository navigation
Expand file tree
/
Copy pathContactController.cs
More file actions
209 lines (185 loc) · 7.96 KB
/
Copy pathContactController.cs
File metadata and controls
209 lines (185 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using Grand.Business.Core.Interfaces.Cms;
using Grand.Business.Core.Interfaces.Marketing.Contacts;
using Grand.Business.Core.Interfaces.Storage;
using Grand.Domain.Catalog;
using Grand.Domain.Media;
using Grand.Domain.Stores;
using Grand.Infrastructure;
using Grand.SharedKernel.Attributes;
using Grand.SharedKernel.Extensions;
using Grand.Web.Commands.Models.Contact;
using Grand.Web.Common.Controllers;
using Grand.Web.Common.Extensions;
using Grand.Web.Common.Filters;
using Grand.Web.Common.Startup;
using Grand.Web.Events;
using Grand.Web.Models.Contact;
using Grand.Mediator;
using Microsoft.AspNetCore.Mvc;
using Grand.Web.Common.Localization;
using Microsoft.AspNetCore.RateLimiting;
namespace Grand.Web.Controllers;
[ApiGroup(SharedKernel.Extensions.ApiConstants.ApiGroupNameV2)]
public class ContactController : BasePublicController
{
#region Constructors
public ContactController(
IRequestTranslationService translationService,
IContextAccessor contextAccessor,
IMediator mediator)
{
_translationService = translationService;
_contextAccessor = contextAccessor;
_mediator = mediator;
}
#endregion
//available even when a store is closed
[ClosedStore(true)]
[HttpGet]
public virtual async Task<ActionResult<ContactUsModel>> Index(
[FromServices] StoreInformationSettings storeInformationSettings,
[FromServices] IPageService pageService)
{
if (storeInformationSettings.StoreClosed)
{
var closestorepage = await pageService.GetPageBySystemName("ContactUs", _contextAccessor.StoreContext.CurrentStore.Id);
if (closestorepage is not { AccessibleWhenStoreClosed: true })
return RedirectToRoute("StoreClosed");
}
var model = await _mediator.Send(new ContactUsCommand {
Customer = _contextAccessor.WorkContext.CurrentCustomer,
Language = _contextAccessor.WorkContext.WorkingLanguage,
Store = _contextAccessor.StoreContext.CurrentStore
});
return View(model);
}
[EnableRateLimiting(GrandCommonStartup.StorefrontFormRateLimitPolicy)]
[HttpPost]
[ClosedStore(true)]
[DenySystemAccount]
public virtual async Task<ActionResult<ContactUsModel>> Index(
[FromServices] StoreInformationSettings storeInformationSettings,
[FromServices] IPageService pageService,
ContactUsModel model)
{
if (storeInformationSettings.StoreClosed)
{
var closeStorePage = await pageService.GetPageBySystemName("ContactUs", _contextAccessor.StoreContext.CurrentStore.Id);
if (closeStorePage is not { AccessibleWhenStoreClosed: true })
return RedirectToRoute("StoreClosed");
}
if (ModelState.IsValid)
{
var result = await _mediator.Send(new ContactUsSendCommand {
Host = _contextAccessor.StoreContext.CurrentHost,
Customer = _contextAccessor.WorkContext.CurrentCustomer,
Store = _contextAccessor.StoreContext.CurrentStore,
Language = _contextAccessor.WorkContext.WorkingLanguage,
Model = model,
IpAddress = HttpContext?.Connection?.RemoteIpAddress?.ToString()
});
//notification
await _mediator.Publish(new ContactUsEvent(_contextAccessor.WorkContext.CurrentCustomer, result));
return View(result);
}
var modelReturn = await _mediator.Send(new ContactUsCommand {
Customer = _contextAccessor.WorkContext.CurrentCustomer,
Language = _contextAccessor.WorkContext.WorkingLanguage,
Store = _contextAccessor.StoreContext.CurrentStore,
Model = model
});
return View(modelReturn);
}
[HttpPost]
[DenySystemAccount]
public virtual async Task<IActionResult> ContactAttributeChange(ContactAttributeChangeModel model)
{
var result = await _mediator.Send(new ContactAttributeChangeCommand {
Attributes = model.Attributes,
Customer = _contextAccessor.WorkContext.CurrentCustomer,
Store = _contextAccessor.StoreContext.CurrentStore
});
return Json(new {
enabledattributeids = result.enabledAttributeIds.ToArray(),
disabledattributeids = result.disabledAttributeIds.ToArray()
});
}
[EnableRateLimiting(GrandCommonStartup.StorefrontFormRateLimitPolicy)]
[HttpPost]
[DenySystemAccount]
public virtual async Task<IActionResult> UploadFileContactAttribute(string attributeId, IFormFile file,
[FromServices] IDownloadService downloadService,
[FromServices] IContactAttributeService contactAttributeService)
{
var attribute = await contactAttributeService.GetContactAttributeById(attributeId);
if (attribute is not { AttributeControlType: AttributeControlType.FileUpload })
return Json(new
{
success = false,
downloadGuid = Guid.Empty
});
if (file == null)
return Json(new
{
success = false,
message = "No file uploaded",
downloadGuid = Guid.Empty
});
var fileName = Path.GetFileName(file.FileName);
var contentType = file.ContentType;
var fileExtension = Path.GetExtension(fileName);
//empty configuration must not mean "any extension allowed" - fall back to the safe default allow-list
var allowedFileExtensions = FileExtensions.GetAllowedMediaFileTypes(attribute.ValidationFileAllowedExtensions);
if (!allowedFileExtensions.IsAllowedMediaFileType(fileExtension))
return Json(new
{
success = false,
message = _translationService.GetResource("ContactUs.ValidationFileAllowed"),
downloadGuid = Guid.Empty
});
if (attribute.ValidationFileMaximumSize.HasValue)
{
//compare in bytes - check the size reported by the multipart headers before buffering the file into memory
var maxFileSizeBytes = attribute.ValidationFileMaximumSize.Value * 1024L;
if (file.Length > maxFileSizeBytes)
//when returning JSON the mime-type must be set to text/plain
//otherwise some browsers will pop-up a "Save As" dialog.
return Json(new
{
success = false,
message = string.Format(_translationService.GetResource("ContactUs.MaximumUploadedFileSize"),
attribute.ValidationFileMaximumSize.Value),
downloadGuid = Guid.Empty
});
}
var fileBinary = file.GetDownloadBits();
var download = new Download
{
DownloadGuid = Guid.NewGuid(),
CustomerId = _contextAccessor.WorkContext.CurrentCustomer.Id,
UseDownloadUrl = false,
DownloadUrl = "",
DownloadBinary = fileBinary,
ContentType = contentType,
Filename = Path.GetFileNameWithoutExtension(fileName),
Extension = fileExtension,
DownloadType = DownloadType.ContactAttribute,
ReferenceId = attributeId
};
await downloadService.InsertDownload(download);
//when returning JSON the mime-type must be set to text/plain
//otherwise some browsers will pop-up a "Save As" dialog.
return Json(new
{
success = true,
message = _translationService.GetResource("ContactUs.FileUploaded"),
downloadUrl = Url.Action("GetFileUpload", "Download", new { downloadId = download.DownloadGuid }),
downloadGuid = download.DownloadGuid
});
}
#region Fields
private readonly IRequestTranslationService _translationService;
private readonly IContextAccessor _contextAccessor;
private readonly IMediator _mediator;
#endregion
}