Esempio n. 1
0
 public IResult Execute(IFormParameter parameter)
 {
     return(this.MailSendService.Execute(parameter.ToMailParameter()));
 }
Esempio n. 2
0
        private async Task <(byte[], bool)> ReadSection(byte[] initialBytes,
                                                        IReadOnlyDictionary <string, List <string> > headers,
                                                        CancellationToken token)
        {
            string name     = null;
            string fileName = null;

            if (headers != null && headers.ContainsKey("Content-Disposition") &&
                headers["Content-Disposition"].Count > 0)
            {
                var cd = headers["Content-Disposition"][0];
                if (cd.Contains("form-data"))
                {
                    var fileMatch = FileMatcher.Match(cd);
                    var nameMatch = NameMatcher.Match(cd);
                    if (fileMatch.Success)
                    {
                        fileName = fileMatch.Groups[1].Value;
                    }
                    if (nameMatch.Success)
                    {
                        name = nameMatch.Groups[1].Value;
                    }
                }
            }

            var        ms         = new MemoryStream();
            FileStream fs         = null;
            Stream     mainStream = ms;

            var(leftovers, finished) =
                await ReadUntilBoundary(initialBytes, _boundaryBytes, mainStream, _endBoundaryBytes, true, token);

            while (leftovers == null && !finished)
            {
                if (fs == null && fileName != null && ms.Length > TempFileLimit)
                {
                    fs = File.OpenWrite(Path.GetTempFileName());
                    await fs.WriteAsync(ms.ToArray(), 0, (int)ms.Length, token);

                    mainStream = fs;
                }

                if (fileName != null && mainStream.Position > MultiPartFileLimit)
                {
                    if (fs != null)
                    {
                        fs.Close();
                        File.Delete(fs.Name);
                    }

                    throw new ArgumentException($"`{name}` is longer than limit: {MultiPartFileLimit}");
                }

                if (fileName == null && ms.Length > MultiPartFieldLimit)
                {
                    throw new ArgumentException($"`{name}` is longer than limit: {MultiPartFieldLimit}");
                }

                (leftovers, finished) =
                    await ReadUntilBoundary(_boundaryBytes, mainStream, _endBoundaryBytes, true, token);
            }

            IFormParameter param       = null;
            string         contentType = null;

            if (fileName != null)
            {
                headers.TryGetValue("Content-Type", out var ct);
                if (ct != null && ct.Count > 0)
                {
                    contentType = ct[0];
                }
            }

            if (fs != null)
            {
                fs.Close();
                param = new FormParameter <IUploadFileParameter>
                {
                    Headers = headers,
                    Name    = name,
                    Data    = new FileParameter(File.OpenRead(fs.Name))
                    {
                        ContentType = contentType,
                        FileName    = fileName
                    }
                };
                fs.Dispose();
                ms.Dispose();
            }
            else if (fileName != null)
            {
                ms.Seek(0, SeekOrigin.Begin);
                param = new FormParameter <IUploadFileParameter>
                {
                    Headers = headers,
                    Name    = name,
                    Data    = new MemoryParameter(ms)
                    {
                        ContentType = contentType,
                        FileName    = fileName
                    }
                };
            }
            else if (name != null)
            {
                ms.Seek(0, SeekOrigin.Begin);
                param = new FormParameter <string>
                {
                    Headers = headers,
                    Name    = name,
                    Data    = new StreamReader(ms).ReadToEnd()
                };
                ms.Dispose();
            }

            if (param != null && name != null)
            {
                if (!Parameters.ContainsKey(name))
                {
                    Parameters[name] = new List <IFormParameter>();
                }
                Parameters[name].Add(param);
            }

            return(leftovers, false);
        }