Exemplo n.º 1
0
        public static async Task <IEnumerable <Upload> > AsUploadFilesEventAsync(this HttpContext httpContext
                                                                                 , IContentTypeTestUtility contentTypeTestUtility
                                                                                 , CancellationToken cancellationToken = default)
        {
            var form = await httpContext.Request.ReadFormAsync(cancellationToken);

            var number = 0U;

            return((
                       from f in form.Files
                       where contentTypeTestUtility.IsAllowed(f.ContentType)
                       select new Upload(
                           id: Guid.NewGuid(),
                           previewId: Guid.NewGuid(),
                           num: number++,
                           name: f.FileName,
                           contentType: f.ContentType,
                           streamAdapter: new FormFileStreamAdapter(new FormFileDecorator(f))
                           )).ToArray());
        }
 public UploadController(IMediator mediator, IContentTypeTestUtility contentTypeTestUtility, ILogger <UploadController> logger) : base(mediator)
 {
     this.contentTypeTestUtility = contentTypeTestUtility;
     this.logger = logger;
 }
 public static IEnumerable <Upload> AsUploads(this UploadRequest uploadRequest, IContentTypeTestUtility contentTypeTestUtility)
 => uploadRequest.Files?
 .AsFileDescriptors(contentTypeTestUtility) ?? new Upload[]
 {
 };
Exemplo n.º 4
0
        public static IEnumerable <Upload> AsFileDescriptors(this IEnumerable <Base64FilePayload> files, IContentTypeTestUtility contentTypeTestUtility)
        {
            var number = 0U;

            foreach (var rawFile in files)
            {
                string contentType;

                var data = rawFile.RawData.AsSpan();

                ReadOnlyMemory <byte> byteArr;
                if (data.StartsWith(Base64FilePayload.DataToken.AsSpan(), StringComparison.InvariantCultureIgnoreCase))
                {
                    (contentType, byteArr) = Base64Parser.Parse(data, rawFile.Name);
                }
                else
                {
                    byteArr     = Base64ConvertHelper.ConvertToBytes(data);
                    contentType = string.Empty;
                }

                if (string.IsNullOrEmpty(contentType))
                {
                    contentType = contentTypeTestUtility.DetectContentType(byteArr.Slice(0, 4).Span);
                }

                if (contentTypeTestUtility.IsAllowed(contentType))
                {
                    yield return(new Upload(
                                     id: Guid.NewGuid(),
                                     previewId: Guid.NewGuid(),
                                     num: number++,
                                     name: rawFile.Name,
                                     contentType: contentType,
                                     streamAdapter: new ByteaStreamAdapter(byteArr)));
                }
            }
        }