Exemplo n.º 1
0
        public async Task <IActionResult> ReceiveFile()
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                throw new Exception("Not a multipart request");
            }

            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType),
                _defaultFormOptions.MultipartBoundaryLengthLimit
                );

            string albumName = "";

            var reader  = new MultipartReader(boundary, Request.Body);
            var section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(
                    section.ContentDisposition,
                    out var contentDisposition
                    );

                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                    {
                        using (var streamReader = new StreamReader(
                                   section.Body,
                                   Encoding.UTF8,
                                   detectEncodingFromByteOrderMarks: true,
                                   bufferSize: 1024,
                                   leaveOpen: false))
                        {
                            var value = await streamReader.ReadToEndAsync();

                            albumName = value;
                        }
                    }
                    else if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        var fileName = contentDisposition.FileNameStar.ToString();
                        if (string.IsNullOrEmpty(fileName))
                        {
                            fileName = contentDisposition.FileName.ToString();
                        }

                        if (string.IsNullOrEmpty(fileName))
                        {
                            throw new Exception("No filename defined.");
                        }

                        using (var fileStream = section.Body)
                        {
                            await _fileService.UploadFile(albumName, fileName, fileStream);
                        }
                    }
                }

                section = await reader.ReadNextSectionAsync();
            }

            var uploadResult = _fileService.GetUploadRequestResult();
            var vm           = _mapper.Map <UploadResultViewModel>(uploadResult);

            return(View("success", vm));
        }
Exemplo n.º 2
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Post()
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
            }

            // Used to accumulate all the form url encoded key value pairs in the
            // request.
            var    formAccumulator = new KeyValueAccumulator();
            string targetFilePath  = null;

            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType),
                _defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader = new MultipartReader(boundary, HttpContext.Request.Body);

            var section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                ContentDispositionHeaderValue contentDisposition;
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);

                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        var folder = Path.Combine(Path.GetTempPath(), "FileUploadExperimentVault");
                        if (!Directory.Exists(folder))
                        {
                            Directory.CreateDirectory(folder);
                        }
                        targetFilePath = Path.Combine(folder, $"{Guid.NewGuid()}.tmp");

                        using (var targetStream = System.IO.File.Create(targetFilePath))
                        {
                            await section.Body.CopyToAsync(targetStream);

                            _logger.LogInformation($"Copied the uploaded file '{targetFilePath}'");
                        }
                    }
                    else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                    {
                        // Content-Disposition: form-data; name="key"
                        //
                        // value

                        // Do not limit the key name length here because the
                        // multipart headers length limit is already in effect.
                        var key      = HeaderUtilities.RemoveQuotes(contentDisposition.Name);
                        var encoding = GetEncoding(section);
                        using (var streamReader = new StreamReader(
                                   section.Body,
                                   encoding,
                                   detectEncodingFromByteOrderMarks: true,
                                   bufferSize: 1024,
                                   leaveOpen: true))
                        {
                            // The value length limit is enforced by MultipartBodyLengthLimit
                            var value = await streamReader.ReadToEndAsync();

                            if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
                            {
                                value = String.Empty;
                            }
                            formAccumulator.Append(key.Value, value);

                            if (formAccumulator.ValueCount > _defaultFormOptions.ValueCountLimit)
                            {
                                throw new InvalidDataException($"Form key count limit {_defaultFormOptions.ValueCountLimit} exceeded.");
                            }
                        }
                    }
                }

                // Drains any remaining section body that has not been consumed and
                // reads the headers for the next section.
                section = await reader.ReadNextSectionAsync();
            }

            // Bind form data to a model
            //var user = new User();
            //var formValueProvider = new FormValueProvider(
            //    BindingSource.Form,
            //    new FormCollection(formAccumulator.GetResults()),
            //    CultureInfo.CurrentCulture);
            //
            //var bindingSuccessful = await TryUpdateModelAsync(user, prefix: "",
            //    valueProvider: formValueProvider);
            //if (!bindingSuccessful)
            //{
            //  if (!ModelState.IsValid)
            //  {
            //    return BadRequest(ModelState);
            //  }
            //}
            //
            //var uploadedData = new UploadedData()
            //{
            //  Name = user.Name,
            //  Age = user.Age,
            //  Zipcode = user.Zipcode,
            //  FilePath = targetFilePath
            //};
            //return Json(uploadedData);
            return(Ok());
        }
        public async Task <IActionResult> Create()
        {
            try
            {
                var profile           = _profileStorage.Current;
                var shareDestinations = Request.Headers["X-Li-Destination"];

                List <string[]> paths = new List <string[]>();

                if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
                {
                    _logger.LogCritical($"Expected a multipart request, but got {Request.ContentType}");
                    return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
                }

                var boundary = MultipartRequestHelper.GetBoundary(
                    MediaTypeHeaderValue.Parse(Request.ContentType),
                    int.MaxValue);

                bool   firstShare = true;
                string firstFile  = null;

                var connection = _connectionFinder.GetPrimaryLITEConnection(profile);

                foreach (var shareDestination in shareDestinations)
                {
                    foreach (string share in shareDestination.Split(",", StringSplitOptions.RemoveEmptyEntries))
                    {
                        var      filename   = Guid.NewGuid() + "-b-" + boundary;
                        var      targetDir  = connection.resourcePath + Path.DirectorySeparatorChar + connection.name + Path.DirectorySeparatorChar + share.Trim();
                        var      targetFile = connection.resourcePath + Path.DirectorySeparatorChar + connection.name + Path.DirectorySeparatorChar + share.Trim() + Path.DirectorySeparatorChar + filename;
                        string[] targetURI  = { share.Trim(), filename.ToString() };

                        Directory.CreateDirectory(targetDir);

                        if (firstShare)
                        {
                            using (var targetStream = System.IO.File.Create(targetFile + ".tmp"))
                            {
                                await HttpContext.Request.Body.CopyToAsync(targetStream);

                                firstShare = false;
                                firstFile  = targetFile;
                            }
                            System.IO.File.Move(targetFile + ".tmp", targetFile);
                        }
                        else
                        {
                            System.IO.File.Copy(firstFile, targetFile + ".tmp", true);
                            System.IO.File.Move(targetFile + ".tmp", targetFile);
                        }

                        paths.Add(targetURI);
                        _logger.LogInformation($"Copied the uploaded file '{targetFile}' uri '{targetURI}'");
                    }
                }

                if (paths.Count > 0)
                {
                    //notify all connected LITEs and EGS instances.  This needs to be more specific to those interested in sharing dests
                    await _hubContext.Clients.All.SendAsync("ReceiveMessage", User.Identity.Name, JsonSerializer.Serialize(paths));

                    //look up notify table to find clients that are interested in these sharing dests
                    await _hubContext.Clients.Groups(new List <string>(shareDestinations[0].Split(",", StringSplitOptions.RemoveEmptyEntries))).SendAsync("ReceiveMessage", User.Identity.Name, JsonSerializer.Serialize(paths));

                    //{boxUuid}/{fileID}
                    return(CreatedAtRoute("GetFile", new { boxUuid = paths[0], fileID = paths[0] }, paths));
                }
                else
                {
                    _logger.LogCritical($"{(int)HttpStatusCode.UnprocessableEntity}");
                    return(StatusCode((int)HttpStatusCode.UnprocessableEntity)); //if there is no share dest
                }
            }
            catch (TaskCanceledException)
            {
                _logger.LogInformation($"Task was canceled.");
                return(StatusCode(503));
            }
            catch (Exception e)
            {
                _logger.LogCritical($"{e.Message} {e.StackTrace}");
                return(StatusCode(503));
            }
        }
        public async Task <ActionResult <BlobInfo[]> > UploadAssetAsync([FromQuery] string folderUrl, [FromQuery] string url = null, [FromQuery] string name = null)
        {
            // https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.1
            if (url == null && !MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
            }

            var retVal = new List <BlobInfo>();

            if (url != null)
            {
                var fileName = name ?? HttpUtility.UrlDecode(Path.GetFileName(url));
                var fileUrl  = folderUrl + "/" + fileName;
                using (var client = new WebClient())
                    using (var blobStream = _blobProvider.OpenWrite(fileUrl))
                        using (var remoteStream = client.OpenRead(url))
                        {
                            remoteStream.CopyTo(blobStream);
                            var blobInfo = AbstractTypeFactory <BlobInfo> .TryCreateInstance();

                            blobInfo.Name        = fileName;
                            blobInfo.RelativeUrl = fileUrl;
                            blobInfo.Url         = _urlResolver.GetAbsoluteUrl(fileUrl);
                            retVal.Add(blobInfo);
                        }
            }
            else
            {
                var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit);
                var reader   = new MultipartReader(boundary, HttpContext.Request.Body);

                var section = await reader.ReadNextSectionAsync();

                if (section != null)
                {
                    var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);

                    if (hasContentDispositionHeader)
                    {
                        if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                        {
                            var fileName = contentDisposition.FileName.Value;

                            var targetFilePath = folderUrl + "/" + fileName;

                            using (var targetStream = _blobProvider.OpenWrite(targetFilePath))
                            {
                                await section.Body.CopyToAsync(targetStream);
                            }

                            var blobInfo = AbstractTypeFactory <BlobInfo> .TryCreateInstance();

                            blobInfo.Name        = fileName;
                            blobInfo.RelativeUrl = targetFilePath;
                            blobInfo.Url         = _urlResolver.GetAbsoluteUrl(targetFilePath);
                            blobInfo.ContentType = MimeTypeResolver.ResolveContentType(fileName);
                            retVal.Add(blobInfo);
                        }
                    }
                }
            }

            return(Ok(retVal.ToArray()));
        }
Exemplo n.º 5
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Physical()
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                ModelState.AddModelError("File", $"The request couldn't be processed (Error 1).");                  // Log error
                return(BadRequest(ModelState));
            }

            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType),
                _defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader  = new MultipartReader(boundary, HttpContext.Request.Body);
            var section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                var hasContentDispositionHeader =
                    ContentDispositionHeaderValue.TryParse(
                        section.ContentDisposition, out var contentDisposition);

                if (hasContentDispositionHeader)
                {
                    // This check assumes that there's a file
                    // present without form data. If form data
                    // is present, this method immediately fails
                    // and returns the model error.
                    if (!MultipartRequestHelper
                        .HasFileContentDisposition(contentDisposition))
                    {
                        ModelState.AddModelError("File",
                                                 $"The request couldn't be processed (Error 2).");
                        // Log error

                        return(BadRequest(ModelState));
                    }
                    else
                    {
                        // Don't trust the file name sent by the client. To display
                        // the file name, HTML-encode the value.
                        var trustedFileNameForDisplay = WebUtility.HtmlEncode(
                            contentDisposition.FileName.Value);
                        var trustedFileNameForFileStorage = Path.GetRandomFileName();

                        // **WARNING!**
                        // In the following example, the file is saved without
                        // scanning the file's contents. In most production
                        // scenarios, an anti-virus/anti-malware scanner API
                        // is used on the file before making the file available
                        // for download or for use by other systems.
                        // For more information, see the topic that accompanies
                        // this sample.

                        var streamedFileContent = await FileHelpers.ProcessStreamedFile(
                            section, contentDisposition, ModelState,
                            _permittedExtensions, _fileSizeLimit);

                        if (!ModelState.IsValid)
                        {
                            return(BadRequest(ModelState));
                        }
                        var userId   = User.FindFirstValue(ClaimTypes.NameIdentifier);                       // will give the user's userId
                        var userPath = Path.Combine(_targetFilePath, userId);
                        Directory.CreateDirectory(userPath);

                        var fileFolderPath = Path.Combine(userPath, trustedFileNameForFileStorage);
                        Directory.CreateDirectory(fileFolderPath);

                        var filePath = Path.Combine(fileFolderPath, trustedFileNameForFileStorage);

                        using (var targetStream = System.IO.File.Create(filePath))
                        {
                            await targetStream.WriteAsync(streamedFileContent);

                            _logger.LogInformation(
                                "Uploaded file '{TrustedFileNameForDisplay}' saved to " +
                                "'{TargetFilePath}' as {TrustedFileNameForFileStorage}",
                                trustedFileNameForDisplay, _targetFilePath,
                                trustedFileNameForFileStorage);
                        }

                        using (_context)
                        {
                            var std = new CsvFile()
                            {
                                UserId          = userId,
                                Origin          = "local",
                                UploadTime      = DateTime.Now,
                                Size            = new FileInfo(filePath).Length,
                                FileNameStorage = trustedFileNameForFileStorage,
                                FileNameDisplay = trustedFileNameForDisplay
                            };
                            _context.CsvFile.Add(std);
                            _context.SaveChanges();
                        }
                    }
                }

                // Drain any remaining section body that hasn't been consumed and
                // read the headers for the next section.
                section = await reader.ReadNextSectionAsync();
            }

            return(Created(nameof(FileUploadController), null));
        }
        public async Task <IActionResult> UploadFile([FromQuery] string folderId)
        {
            if (!_multipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
            }
            try
            {
                var createFolder = string.IsNullOrEmpty(folderId) && !await _filesRepository.FolderExists(folderId);

                if (createFolder)
                {
                    var folder = new Folder
                    {
                        NameId = "My Folder Id",
                    };
                    await _filesRepository.AddFolder(folder);

                    folderId = folder.Id;
                }
                var boundary = _multipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit);
                var reader   = new MultipartReader(boundary, HttpContext.Request.Body);
                var fileDesc = new FileDescription
                {
                    Url = "api/Files/download-file?id={id}&file={path}"
                };
                FileAccumulator newFile = null;

                newFile = await _mongoStreamer.UploadFileAsync(reader);

                if (newFile != null)
                {
                    fileDesc.Id           = newFile.Id;
                    fileDesc.Url          = fileDesc.Url.Replace("{id}", newFile.Id).Replace("{path}", Uri.EscapeDataString(newFile.Filename));
                    fileDesc.Name         = newFile.Filename;
                    fileDesc.Created_time = DateTime.UtcNow;
                    if (!await _filesRepository.AddFileToFolder(folderId, fileDesc))
                    {
                        return
                            (BadRequest(new
                        {
                            ErrorDescription = "Could not upload."
                        }));
                    }
                    return(Ok(new { Folder = folderId, FileUrl = fileDesc.Url }));
                }
                else
                {
                    _logger.LogError($"{DateTime.UtcNow} Upload failed.");
                    return
                        (BadRequest(new
                    {
                        Errors = new { Image = new object[] { new { ErrorMessage = "There is no image to upload" } } },
                        ErrorDescription = "Could not upload."
                    }));
                }


                //return Ok(new { Edge = post_edge, ImageUrl = template.Url });
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return
                    (BadRequest(new
                {
                    e.StackTrace,
                    ErrorDescription = "Could not fulfill the request."
                }));
            }
        }
        public static async Task <FormValueProvider> StreamFile(this HttpRequest request, Stream targetStream)
        {
            if (!MultipartRequestHelper.IsMultipartContentType(request.ContentType))
            {
                throw new Exception($"Expected a multipart request, but got {request.ContentType}");
            }

            // Used to accumulate all the form url encoded key value pairs in the
            // request.
            var    formAccumulator = new KeyValueAccumulator();
            string targetFilePath  = null;

            var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader   = new MultipartReader(boundary, request.Body);

            var section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                ContentDispositionHeaderValue contentDisposition;
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);

                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        await section.Body.CopyToAsync(targetStream);
                    }
                    else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                    {
                        // Content-Disposition: form-data; name="key"
                        //
                        // value

                        // Do not limit the key name length here because the
                        // multipart headers length limit is already in effect.
                        var key      = HeaderUtilities.RemoveQuotes(contentDisposition.Name);
                        var encoding = GetEncoding(section);
                        using (var streamReader = new StreamReader(
                                   section.Body,
                                   encoding,
                                   detectEncodingFromByteOrderMarks: true,
                                   bufferSize: 1024,
                                   leaveOpen: true))
                        {
                            // The value length limit is enforced by MultipartBodyLengthLimit
                            var value = await streamReader.ReadToEndAsync();

                            if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
                            {
                                value = String.Empty;
                            }
                            formAccumulator.Append(key.Value, value); // For .NET Core <2.0 remove ".Value" from key

                            if (formAccumulator.ValueCount > _defaultFormOptions.ValueCountLimit)
                            {
                                throw new InvalidDataException($"Form key count limit {_defaultFormOptions.ValueCountLimit} exceeded.");
                            }
                        }
                    }
                }

                // Drains any remaining section body that has not been consumed and
                // reads the headers for the next section.
                section = await reader.ReadNextSectionAsync();
            }

            // Bind form data to a model
            var formValueProvider = new FormValueProvider(BindingSource.Form, new FormCollection(formAccumulator.GetResults()), CultureInfo.CurrentCulture);

            return(formValueProvider);
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Upload()
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
            }

            // Used to accumulate all the form url encoded key value pairs in the request
            var formAccumulator = new KeyValueAccumulator();

            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType),
                DefaultFormOptions.MultipartBoundaryLengthLimit);
            var reader = new MultipartReader(boundary, HttpContext.Request.Body);

            using (_logger.BeginScope(new
            {
                Boundary = boundary,
                Request.ContentType
            }))
            {
                _logger.LogTrace("Reading next section");
                var section = await reader.ReadNextSectionAsync();

                while (section != null)
                {
                    var hasContentDispositionHeader =
                        ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);

                    if (hasContentDispositionHeader)
                    {
                        _logger.LogTrace("Content Disposition Header: {0}", section.ContentDisposition);

                        if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                        {
                            var fileName = contentDisposition.FileName.Value;

                            if (!SubmissionDataFormFileNamesLazy.Value.Contains(contentDisposition.Name.Value))
                            {
                                _logger.LogInformation($"Unknown file '{contentDisposition.Name.Value}' with fileName: '{fileName}' is being ignored.");
                                // Drains any remaining section body that has not been consumed and
                                // reads the headers for the next section.
                                section = await reader.ReadNextSectionAsync();

                                continue;
                            }

                            formAccumulator.Append(contentDisposition.Name.Value, fileName);

                            var path = await _tempFileService.CreateFromStreamAsync(fileName, section.Body);

                            _logger.LogInformation($"Copied the uploaded file '{fileName}' to path: '{path}'");
                        }
                        else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                        {
                            // Content-Disposition: form-data; name="key"
                            //
                            // value

                            // Do not limit the key name length here because the
                            // multipart headers length limit is already in effect.
                            var key = HeaderUtilities.RemoveQuotes(contentDisposition.Name);

                            _logger.LogDebug("Retrieving value for {0}", key);

                            var encoding = GetEncoding(section);
                            using (var streamReader = new StreamReader(
                                       section.Body,
                                       encoding,
                                       detectEncodingFromByteOrderMarks: true,
                                       bufferSize: 1024,
                                       leaveOpen: true))
                            {
                                // The value length limit is enforced by MultipartBodyLengthLimit
                                var value = await streamReader.ReadToEndAsync();

                                if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
                                {
                                    value = String.Empty;
                                }

                                formAccumulator.Append(key.Value, value);

                                if (formAccumulator.ValueCount > DefaultFormOptions.ValueCountLimit)
                                {
                                    throw new InvalidDataException(
                                              $"Form key count limit {DefaultFormOptions.ValueCountLimit} exceeded.");
                                }
                            }
                        }
                    }

                    // Drains any remaining section body that has not been consumed and
                    // reads the headers for the next section.
                    section = await reader.ReadNextSectionAsync();
                }
            }

            // Bind form data to a model
            var submissionData = new SubmissionData();

            var bindingSuccessful = await BindDataAsync(submissionData, formAccumulator.GetResults());

            if (!bindingSuccessful)
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
            }

            var requiredFormFileProperties = typeof(SubmissionData).GetProperties()
                                             .Where(p => p.GetCustomAttributes(typeof(RequiredAttribute), true).Any())
                                             .Where(p => p.GetCustomAttributes(typeof(FormFileAttribute), true).Any());

            foreach (var requiredFormFileProperty in requiredFormFileProperties)
            {
                var fileName = requiredFormFileProperty.GetValue(submissionData);
                if (!_tempFileService.Files.Contains(fileName))
                {
                    ModelState.AddModelError(requiredFormFileProperty.Name, $"File '{requiredFormFileProperty.Name}' with name: '{fileName}' not found in request.");
                    return(BadRequest(ModelState));
                }
            }

            var repositoryOwner = User.Claims.FirstOrDefault(c => c.Type == "urn:msbloc:repositoryOwner")?.Value;
            var repositoryName  = User.Claims.FirstOrDefault(c => c.Type == "urn:msbloc:repositoryName")?.Value;

            var checkRun = await _logAnalyzerService.SubmitAsync(
                repositoryOwner,
                repositoryName,
                submissionData.CommitSha,
                submissionData.CloneRoot,
                _tempFileService.GetFilePath(submissionData.BinaryLogFile));

            return(Json(checkRun));
        }
Exemplo n.º 9
0
        //[RequestFormLimits(MultipartBodyLengthLimit = 209715200)]
        //[RequestSizeLimit(209715200)]
        public async Task <IActionResult> UploadPhysical([ModelBinder(BinderType = typeof(JsonModelBinder))] List <FileTag> tags)
        {
            Program.CleanAppMre.WaitOne();
            Program.CleanChatMre.WaitOne();
            Program.StreamingMre.Reset();

            try
            {
                _logger.LogInformation("File tags: {FileTags}", tags);

                var i = 0;
                var successUploads = new List <UploadInfoDto>();
                var failedUploads  = new List <UploadInfoDto>();

                if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
                {
                    throw new ClientException("上传失败", new List <string> {
                        "Not multipart content type."
                    });
                }

                var boundary = MultipartRequestHelper.GetBoundary(
                    MediaTypeHeaderValue.Parse(Request.ContentType),
                    _defaultFormOptions.MultipartBoundaryLengthLimit);
                var reader  = new MultipartReader(boundary, HttpContext.Request.Body);
                var section = await reader.ReadNextSectionAsync();

                while (section != null)
                {
                    var hasContentDispositionHeader =
                        ContentDispositionHeaderValue.TryParse(
                            section.ContentDisposition, out var contentDisposition);

                    if (hasContentDispositionHeader)
                    {
                        // 1. This check assumes that there's a file
                        // present without form data. If form data
                        // is present, this method immediately fails
                        // and returns the model error.
                        // 2. also check there is a tag correspond to the file
                        if (!MultipartRequestHelper.HasFileContentDisposition(contentDisposition) || (tags != null && i >= tags.Count))
                        {
                            if (tags != null && i >= tags.Count)
                            {
                                _logger.LogError($"Upload file {contentDisposition.FileName.Value} failed, no conresponding tag.");
                            }
                            else
                            {
                                _logger.LogError($"Upload file {contentDisposition.FileName.Value} failed, no file content disposition.");
                            }

                            // 记录上传失败的文件
                            failedUploads.Add(new UploadInfoDto {
                                Name = contentDisposition.FileName.Value, Index = i
                            });
                        }
                        else
                        {
                            _logger.LogInformation($"Uploading file {contentDisposition.FileName.Value} with tag {tags?[i].ToString() ?? "N/A"}.");

                            // Don't trust the file name sent by the client. To display
                            // the file name, HTML-encode the value.
                            var trustedFileNameForDisplay = WebUtility.HtmlEncode(contentDisposition.FileName.Value);
                            //var trustedFileNameForFileStorage = Path.GetRandomFileName();
                            var trustedFileNameForFileStorage = trustedFileNameForDisplay;

                            var fileInfo = tags == null ? null : await _fileInfoRepository.GetFileInfoAsync(trustedFileNameForFileStorage, tags[i]);

                            if (fileInfo == null)
                            {
                                // 文件不存在,保存文件并记录到数据库
                                // **WARNING!**
                                // In the following example, the file is saved without
                                // scanning the file's contents. In most production
                                // scenarios, an anti-virus/anti-malware scanner API
                                // is used on the file before making the file available
                                // for download or for use by other systems.
                                // For more information, see the topic that accompanies
                                // this sample.

                                var streamedFileContent = await FileHelpers.ProcessStreamedFile(
                                    section, contentDisposition, ModelState,
                                    _permittedExtensions, _streamingSettings.Value.FileSizeLimit);

                                if (!ModelState.IsValid)
                                {
                                    // 记录上传失败的文件
                                    failedUploads.Add(new UploadInfoDto {
                                        Name = trustedFileNameForFileStorage, Index = i
                                    });
                                    ModelState.Clear();

                                    _logger.LogError($"Upload file {contentDisposition.FileName.Value} failed.");
                                }
                                else
                                {
                                    var folder = tags == null ? _streamingSettings.Value.StoredFilesPath : GetFolderByTag(tags[i]);
                                    //var folder = Path.Combine(_streamingSettings.Value.StoredFilesPath, tags?[i].ToString().ToLower() ?? string.Empty);

                                    _logger.LogInformation("folder: {folder}", folder);

                                    //Directory.CreateDirectory(folder);
                                    var filePath = Path.Combine(folder, trustedFileNameForFileStorage);

                                    _logger.LogInformation("Save file at path: {filePath}", filePath);

                                    // 文件写入文件系统
                                    using (var targetStream = System.IO.File.Create(filePath))
                                    {
                                        await targetStream.WriteAsync(streamedFileContent);
                                    }

                                    // 文件信息存入到数据库中
                                    var command = new CreateFileInfoCommand {
                                        Name = trustedFileNameForFileStorage, FileTag = tags[i]
                                    };
                                    await _mediator.Send(command);

                                    // 记录上传成功的文件
                                    successUploads.Add(new UploadInfoDto {
                                        Name = trustedFileNameForFileStorage, Index = i
                                    });

                                    // 向前兼容:拷贝文件
                                    if (tags == null)
                                    {
                                        //var distFolder = Path.Combine(_streamingSettings.Value.StoredFilesPath, FileTag.App.ToString().ToLower());
                                        var distFolder = GetFolderByTag(FileTag.App);
                                        System.IO.File.Copy(filePath, Path.Combine(distFolder, trustedFileNameForFileStorage), true);
                                        _logger.LogInformation($"Copied file {filePath} to {distFolder}");

                                        //distFolder = Path.Combine(_streamingSettings.Value.StoredFilesPath, FileTag.AppOriginal.ToString().ToLower());
                                        distFolder = GetFolderByTag(FileTag.AppOriginal);
                                        System.IO.File.Copy(filePath, Path.Combine(distFolder, trustedFileNameForFileStorage), true);
                                        _logger.LogInformation($"Copied file {filePath} to {distFolder}");

                                        //distFolder = Path.Combine(_streamingSettings.Value.StoredFilesPath, FileTag.AppThumbnail.ToString().ToLower());
                                        distFolder = GetFolderByTag(FileTag.AppThumbnail);
                                        System.IO.File.Copy(filePath, Path.Combine(distFolder, trustedFileNameForFileStorage), true);
                                        _logger.LogInformation($"Copied file {filePath} to {distFolder}");

                                        //distFolder = Path.Combine(_streamingSettings.Value.StoredFilesPath, FileTag.AppVideo.ToString().ToLower());
                                        distFolder = GetFolderByTag(FileTag.AppVideo);
                                        System.IO.File.Copy(filePath, Path.Combine(distFolder, trustedFileNameForFileStorage), true);
                                        _logger.LogInformation($"Copied file {filePath} to {distFolder}");

                                        //distFolder = Path.Combine(_streamingSettings.Value.StoredFilesPath, FileTag.Chat.ToString().ToLower());
                                        distFolder = GetFolderByTag(FileTag.Chat);
                                        System.IO.File.Copy(filePath, Path.Combine(distFolder, trustedFileNameForFileStorage), true);
                                        _logger.LogInformation($"Copied file {filePath} to {distFolder}");

                                        //distFolder = Path.Combine(_streamingSettings.Value.StoredFilesPath, FileTag.ChatThumbnail.ToString().ToLower());
                                        distFolder = GetFolderByTag(FileTag.ChatThumbnail);
                                        System.IO.File.Copy(filePath, Path.Combine(distFolder, trustedFileNameForFileStorage), true);
                                        _logger.LogInformation($"Copied file {filePath} to {distFolder}");

                                        //distFolder = Path.Combine(_streamingSettings.Value.StoredFilesPath, FileTag.ChatVideo.ToString().ToLower());
                                        distFolder = GetFolderByTag(FileTag.ChatVideo);
                                        System.IO.File.Copy(filePath, Path.Combine(distFolder, trustedFileNameForFileStorage), true);
                                        _logger.LogInformation($"Copied file {filePath} to {distFolder}");
                                    }
                                    else
                                    {
                                        var distFolder = _streamingSettings.Value.StoredFilesPath;
                                        System.IO.File.Copy(filePath, Path.Combine(distFolder, trustedFileNameForFileStorage), true);
                                        _logger.LogInformation($"Copied file {filePath} to {distFolder}");
                                    }

                                    _logger.LogInformation(
                                        "Uploaded file '{TrustedFileNameForDisplay}' saved to " +
                                        "'{TargetFilePath}' as {TrustedFileNameForFileStorage}",
                                        trustedFileNameForDisplay, folder,
                                        trustedFileNameForFileStorage);
                                }
                            }
                            else
                            {
                                // 文件已存在,直接记录为上传成功
                                successUploads.Add(new UploadInfoDto {
                                    Name = trustedFileNameForFileStorage, Index = i
                                });
                                _logger.LogInformation($"File {trustedFileNameForFileStorage} already exists in database, skip.");
                            }
                        }
                    }

                    i++;
                    // Drain any remaining section body that hasn't been consumed and
                    // read the headers for the next section.
                    section = await reader.ReadNextSectionAsync();
                }

                //return Created(nameof(StreamingController), null);
                var uploadStatus = new UploadStatusDto {
                    SuccessUploads = successUploads, FailedUploads = failedUploads
                };
                if (failedUploads.Count > 0)
                {
                    return(StatusCode((int)HttpStatusCode.BadRequest,
                                      ResponseWrapper.CreateErrorResponseWrapper(Arise.DDD.API.Response.StatusCode.ClientError, "Upload Failed.", null, uploadStatus)));
                }
                else
                {
                    return(Ok(ResponseWrapper.CreateOkResponseWrapper(uploadStatus)));
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                Program.StreamingMre.Set();
            }
        }
        public async Task <ActionResult <ModuleDescriptor> > UploadModuleArchive()
        {
            EnsureModulesCatalogInitialized();
            ModuleDescriptor result = null;

            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
            }
            var uploadPath = Path.GetFullPath(_platformOptions.LocalUploadFolderPath);

            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            string targetFilePath = null;

            var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader   = new MultipartReader(boundary, HttpContext.Request.Body);

            var section = await reader.ReadNextSectionAsync();

            if (section != null)
            {
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out ContentDispositionHeaderValue contentDisposition);

                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        var fileName = contentDisposition.FileName.Value;
                        targetFilePath = Path.Combine(uploadPath, fileName);

                        using (var targetStream = System.IO.File.Create(targetFilePath))
                        {
                            await section.Body.CopyToAsync(targetStream);
                        }
                    }
                }
                using (var packageStream = System.IO.File.Open(targetFilePath, FileMode.Open))
                    using (var package = new ZipArchive(packageStream, ZipArchiveMode.Read))
                    {
                        var entry = package.GetEntry("module.manifest");
                        if (entry != null)
                        {
                            using (var manifestStream = entry.Open())
                            {
                                var manifest           = ManifestReader.Read(manifestStream);
                                var module             = new ManifestModuleInfo(manifest);
                                var alreadyExistModule = _externalModuleCatalog.Modules.OfType <ManifestModuleInfo>().FirstOrDefault(x => x.Equals(module));
                                if (alreadyExistModule != null)
                                {
                                    module = alreadyExistModule;
                                }
                                else
                                {
                                    //Force dependency validation for new module
                                    _externalModuleCatalog.CompleteListWithDependencies(new[] { module }).ToList().Clear();
                                    _externalModuleCatalog.AddModule(module);
                                }
                                module.Ref = targetFilePath;
                                result     = new ModuleDescriptor(module);
                            }
                        }
                    }
            }
            return(Ok(result));
        }
Exemplo n.º 11
0
        /// based on microsoft example https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/mvc/models/file-uploads/samples/
        /// and large file streaming example https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-5.0#upload-large-files-with-streaming
        private async Task <FileDto> UploadMultipartContent(Stream requestBody, string?contentType, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            // Check if HttpRequest (Form Data) is a Multipart Content Type
            if (!MultipartRequestHelper.IsMultipartContentType(contentType))
            {
                throw new InvalidDataException($"Expected a multipart request, but got {contentType}");
            }
            var defaultFormOptions = new FormOptions();
            // Create a Collection of KeyValue Pairs.
            var formAccumulator = new KeyValueAccumulator();
            // Determine the Multipart Boundary.
            var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(contentType), defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader   = new MultipartReader(boundary, requestBody);
            var section  = await reader.ReadNextSectionAsync(cancellationToken);

            var fileDto = new FileDto();

            // Loop through each 'Section', starting with the current 'Section'.
            while (section != null)
            {
                // Check if the current 'Section' has a ContentDispositionHeader.
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        if (contentDisposition != null)
                        {
                            var sectionFileName = contentDisposition.FileName.Value;
                            // use an encoded filename in case there is anything weird
                            var encodedFileName = WebUtility.HtmlEncode(Path.GetFileName(sectionFileName));

                            // read the section filename to get the content type
                            var fileExtension = Path.GetExtension(sectionFileName);

                            // now make it unique
                            var    uniqueFileName = $"{Guid.NewGuid()}{fileExtension}";
                            string?md5Hash;

                            // TODO MimeDetective does not work when stream has already been uploaded - figure out a solution
                            //if (fileContentTypeMatchesExtension is false)
                            //{
                            //    await _blobStorageProvider.DeleteFileAsync(uniqueFileName);
                            //    _logger.LogError("File extension:{0} does not match the file signature", fileExtension);
                            //    throw new FormatException("File extension} does not match the file signature");
                            //}

                            if (!_acceptedFileTypes.Contains(fileExtension.ToLower()))
                            {
                                _logger.LogError("file extension:{0} is not an accepted file", fileExtension);
                                throw new ConstraintException("The file is not an accepted file");
                            }
                            try
                            {
                                md5Hash = await _blobStorageProvider.UploadFileAsync(section.Body, uniqueFileName, MimeTypesMap.GetMimeType(encodedFileName), cancellationToken);
                            }
                            catch (Exception ex)
                            {
                                _logger.LogError(ex, "An error occurred uploading file to blob storage");
                                throw;
                            }

                            // TODO MimeDetective does not work when stream has already been uploaded - figure out a solution
                            //if (fileContentTypeMatchesExtension is false)
                            //{
                            //    await _blobStorageProvider.DeleteFileAsync(uniqueFileName);
                            //    _logger.LogError("File extension:{0} does not match the file signature", fileExtension);
                            //    throw new FormatException("File extension} does not match the file signature");
                            //}

                            var now = _systemClock.UtcNow.UtcDateTime;

                            // trick to get the size without reading the stream in memory
                            var size = section.Body.Position;

                            fileDto.FileName      = encodedFileName;
                            fileDto.FileExtension = fileExtension;
                            fileDto.FileSizeBytes = size;
                            fileDto.BlobName      = uniqueFileName;
                            fileDto.CreatedAtUTC  = now;
                            if (md5Hash != null)
                            {
                                fileDto.BlobHash = Convert.FromBase64String(md5Hash);
                            }
                        }
                    }
                    else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                    {
                        // if for some reason other form data is sent it would get processed here
                        var key = HeaderUtilities.RemoveQuotes(contentDisposition?.Name.ToString().ToLowerInvariant());

                        var encoding = GetEncoding(section);
                        using (var streamReader = new StreamReader(section.Body, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true))
                        {
                            var value = await streamReader.ReadToEndAsync();

                            if (string.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
                            {
                                value = string.Empty;
                            }
                            formAccumulator.Append(key.Value, value);
                            if (formAccumulator.ValueCount > defaultFormOptions.ValueCountLimit)
                            {
                                _logger.LogError("FileUpload: Form key count limit {0} exceeded.", defaultFormOptions.ValueCountLimit);
                                throw new FormatException($"Form key count limit { defaultFormOptions.ValueCountLimit } exceeded.");
                            }
                        }
                    }
                }
                // Begin reading the next 'Section' inside the 'Body' of the Request.
                section = await reader.ReadNextSectionAsync(cancellationToken);
            }

            if (formAccumulator.HasValues)
            {
                var formValues = formAccumulator.GetResults();
                var titleFound = formValues.TryGetValue("title", out var title);
                if (titleFound is false)
                {
                    throw new ArgumentNullException($"Title was not provided");
                }
                var descriptionFound = formValues.TryGetValue("description", out var description);
                if (descriptionFound is false)
                {
                    throw new ArgumentNullException($"Description was not provided");
                }

                fileDto.Title       = title;
                fileDto.Description = description;
            }
            return(fileDto);
        }
Exemplo n.º 12
0
        public async Task <IActionResult> Upload()
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got '{Request.ContentType}'."));
            }

            // Used to accumulate all the form url encoded key value pairs in the request.
            var    formAccumulator = new KeyValueAccumulator();
            string targetFilePath  = null;

            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader = new MultipartReader(boundary, HttpContext.Request.Body);

            var section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                ContentDispositionHeaderValue contentDisposition;
                ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);

                if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                {
                    var name     = HeaderUtilities.RemoveQuotes(contentDisposition.Name) ?? string.Empty;
                    var fileName = HeaderUtilities.RemoveQuotes(contentDisposition.FileName) ?? string.Empty;

                    // Here the uploaded file is being copied to local disk but you can also for example, copy the
                    // stream directly to let's say Azure blob storage
                    targetFilePath = Path.Combine(_hostingEnvironment.ContentRootPath, Guid.NewGuid().ToString());
                    using (var targetStream = System.IO.File.Create(targetFilePath))
                    {
                        await section.Body.CopyToAsync(targetStream);

                        _logger.LogInformation($"Copied the uploaded file '{fileName}' to '{targetFilePath}'.");
                    }
                }
                else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                {
                    // Content-Disposition: form-data; name="key"
                    //
                    // value

                    // Do not limit the key name length here because the mulipart headers length
                    // limit is already in effect.
                    var key = HeaderUtilities.RemoveQuotes(contentDisposition.Name);
                    MediaTypeHeaderValue mediaType;
                    MediaTypeHeaderValue.TryParse(section.ContentType, out mediaType);
                    var encoding = FilterEncoding(mediaType?.Encoding);
                    using (var streamReader = new StreamReader(
                               section.Body,
                               encoding,
                               detectEncodingFromByteOrderMarks: true,
                               bufferSize: 1024,
                               leaveOpen: true))
                    {
                        // The value length limit is enforced by MultipartBodyLengthLimit
                        var value = await streamReader.ReadToEndAsync();

                        formAccumulator.Append(key, value);

                        if (formAccumulator.ValueCount > _defaultFormOptions.ValueCountLimit)
                        {
                            throw new InvalidDataException(
                                      $"Form key count limit {_defaultFormOptions.ValueCountLimit} exceeded.");
                        }
                    }
                }

                // Drains any remaining section body that has not been consumed and
                // reads the headers for the next section.
                section = await reader.ReadNextSectionAsync();
            }

            // Bind form data to a model
            var user = new User();
            var formValueProvider = new FormValueProvider(
                BindingSource.Form,
                new FormCollection(formAccumulator.GetResults()),
                CultureInfo.CurrentCulture);

            var bindingSuccessful = await TryUpdateModelAsync(user, prefix : "", valueProvider : formValueProvider);

            if (!bindingSuccessful)
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
            }

            var uploadedData = new UploadedData()
            {
                Name     = user.Name,
                Age      = user.Age,
                Zipcode  = user.Zipcode,
                FilePath = targetFilePath
            };

            return(Json(uploadedData));
        }
Exemplo n.º 13
0
        public async Task <JsonResult> UpdateAvatar()
        {
            if (!MultipartRequestHelper.IsMultipartContentType(HttpContext.Request.ContentType))
            {
                return(new JsonResult(new CommonResponse {
                    StatusCode = -1
                }));                                                           // 没有在Header处声明Multipart/form-data.
            }
            var UserID = User.Claims.ToList()[0].Value;

            try
            {
                var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), 70);
                var reader   = new MultipartReader(boundary, Request.Body);
                var section  = await reader.ReadNextSectionAsync();

                string fileExt = string.Empty;
                while (section != null)
                {
                    ContentDispositionHeaderValue contentDisposition;
                    var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);

                    if (hasContentDispositionHeader)
                    {
                        if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                        {
                            var fileName = contentDisposition.FileName.HasValue ? contentDisposition.FileName.Value.Trim() : string.Empty;
                            if (string.IsNullOrEmpty(fileName))
                            {
                                return(new JsonResult(new CommonResponse()
                                {
                                    StatusCode = -5
                                }));                                                             // 没有fileName
                            }

                            var fileNamez = fileName.Split(".");
                            if (fileNamez.Length < 2)
                            {
                                return(new JsonResult(new CommonResponse()
                                {
                                    StatusCode = -6
                                }));                                                                                   // fileName没有后缀
                            }
                            fileExt = fileNamez[1];

                            var path = Path.Combine(_resolver.GetAvatar(), $"{UserID}.{fileExt}");
                            using (var targetStream = System.IO.File.Create(path))
                            {
                                await section.Body.CopyToAsync(targetStream);
                            }
                        }
                    }
                    section = await reader.ReadNextSectionAsync();
                }

                var CurrentUserProfile = _profDb.GetProfile(UserID);
                CurrentUserProfile.Avatar = $"{UserID}.{fileExt}";

                return(new JsonResult(await _profile.ModifyProfile(UserID, CurrentUserProfile)));
            }
            catch (InvalidDataException exceed)
            {
                System.Console.WriteLine(exceed.Message);
                return(new JsonResult(new CommonResponse {
                    StatusCode = -2
                }));                                                           // 上传的图片大小超过了2MB
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message); // 服务器的其他未知错误。
                return(new JsonResult(new CommonResponse {
                    StatusCode = -3
                }));                                                           // 服务器发生未知错误
            }
        }
        public static async Task <UploadResponse> StreamFile(this HttpRequest request, AssetSettings assetSettings)
        {
            Guard.AgainstInvalidArgument(nameof(request.ContentType), MultipartRequestHelper.IsMultipartContentType(request.ContentType));

            var results = new UploadResponse();

            results.Messages = new List <UploadResponseMessage>();
            var formAccumulator = new KeyValueAccumulator();

            // Used to accumulate all the form url encoded key value pairs in the request.
            var fileDtos = new List <FileDto>();
            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(request.ContentType),
                _defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader = new MultipartReader(boundary, request.Body);

            var section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                ContentDispositionHeaderValue contentDisposition;
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);

                if (hasContentDispositionHeader)
                {
                    string orginalFileName = contentDisposition.FileName.Value;
                    string fileName        = $"{DateTime.Now:yyyyMMddHHmmss}_{orginalFileName}";
                    string targetFile      = Path.Combine(assetSettings.TempDataFolder, fileName);

                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        using (var memoryStream = File.Create(targetFile))
                        {
                            await section.Body.CopyToAsync(memoryStream);

                            if (memoryStream.Length == 0)
                            {
                                results.Messages.Add(new UploadResponseMessage()
                                {
                                    Text = $"The file {orginalFileName} is empty.",
                                    Code = "FileEmpty"
                                });
                            }
                            else if (memoryStream.Length > assetSettings.FileSizeLimit)
                            {
                                var megabyteSizeLimit = assetSettings.FileSizeLimit / 1048576; //1MB
                                results.Messages.Add(new UploadResponseMessage()
                                {
                                    Text = $"The file {orginalFileName} exceeds limit {megabyteSizeLimit:N1} MB.",
                                    Code = "ExceedLimit"
                                });
                            }
                            else
                            {
                                string extension = Path.GetExtension(targetFile);

                                var fileDto = new FileDto()
                                {
                                    OriginalFileName                        = orginalFileName,
                                    FilePath                                = targetFile,
                                    Extension                               = extension != null?extension.Replace(".", "") : "",
                                                                   FileName = fileName
                                };

                                // Copy stream to dto would be faster then re-read file from physical disk
                                fileDto.StreamData    = new MemoryStream();
                                memoryStream.Position = 0;
                                await memoryStream.CopyToAsync(fileDto.StreamData);

                                fileDtos.Add(fileDto);
                            }
                        }
                    }
                    else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                    {
                        // Don't limit the key name length because the multipart headers length limit is already in effect.
                        var key = HeaderUtilities
                                  .RemoveQuotes(contentDisposition.Name).Value;
                        var encoding = GetEncoding(section);

                        using (var streamReader = new StreamReader(
                                   section.Body,
                                   encoding,
                                   detectEncodingFromByteOrderMarks: true,
                                   bufferSize: 1024,
                                   leaveOpen: true))
                        {
                            // The value length limit is enforced by MultipartBodyLengthLimit
                            var value = await streamReader.ReadToEndAsync();

                            if (string.Equals(value, "undefined",
                                              StringComparison.OrdinalIgnoreCase))
                            {
                                value = string.Empty;
                            }
                            formAccumulator.Append(key, value);
                        }
                    }
                }

                // Drains any remaining section body that has not been consumed and
                // reads the headers for the next section.
                section = await reader.ReadNextSectionAsync();
            }

            results.Files           = fileDtos;
            results.FormAccumulator = formAccumulator;
            results.Status          = true;
            results.ErrorCode       = ResponseErrorCode.None;
            results.Message         = "Ok";
            return(results);
        }
Exemplo n.º 15
0
        // [ValidateAntiForgeryToken]


        public async Task <IActionResult> Upload()
        {
            try
            {
                if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
                {
                    return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
                }

                // Used to accumulate all the form url encoded key value pairs in the
                // request.
                var    formAccumulator      = new KeyValueAccumulator();
                var    nameFile             = String.Empty;
                string targetFilePathBackUp = string.Empty;
                var    boundary             = MultipartRequestHelper.GetBoundary(
                    Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse(Request.ContentType),
                    _defaultFormOptions.MultipartBoundaryLengthLimit);
                var reader  = new MultipartReader(boundary, HttpContext.Request.Body);
                var section = await reader.ReadNextSectionAsync();

                var extension = String.Empty;

                while (section != null)
                {
                    Microsoft.Net.Http.Headers.ContentDispositionHeaderValue contentDisposition;
                    var hasContentDispositionHeader = Microsoft.Net.Http.Headers.ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);
                    if (hasContentDispositionHeader)
                    {
                        if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                        {
                            extension = GetDefaultExtension(section.ContentType);
                            if (string.IsNullOrEmpty(extension))
                            {
                                extension = contentDisposition.FileName.Value.Substring(contentDisposition.FileName.Value.IndexOf('.'), (contentDisposition.FileName.Value.Length - contentDisposition.FileName.Value.IndexOf('.')));
                            }

                            nameFile             = contentDisposition.FileName.Value;
                            targetFilePathBackUp = string.Format("{0}{1}", configuration.GetValue <string>("Files:RutaEvidencia"), nameFile);
                            using (var targetStream = System.IO.File.Create(targetFilePathBackUp))
                            {
                                await section.Body.CopyToAsync(targetStream);
                            }
                        }
                        else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                        {
                            // Content-Disposition: form-data; name="key"
                            //
                            // value

                            // Do not limit the key name length here because the
                            // multipart headers length limit is already in effect.
                            var key      = HeaderUtilities.RemoveQuotes(contentDisposition.Name);
                            var encoding = GetEncoding(section);
                            using (var streamReader = new StreamReader(
                                       section.Body,
                                       encoding,
                                       detectEncodingFromByteOrderMarks: true,
                                       bufferSize: 1024,
                                       leaveOpen: true))
                            {
                                // The value length limit is enforced by MultipartBodyLengthLimit
                                var value = await streamReader.ReadToEndAsync();

                                if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
                                {
                                    value = String.Empty;
                                }
                                formAccumulator.Append(key.Value, value);

                                if (formAccumulator.ValueCount > _defaultFormOptions.ValueCountLimit)
                                {
                                    throw new InvalidDataException($"Form key count limit {_defaultFormOptions.ValueCountLimit} exceeded.");
                                }
                            }

                            //var lista_trola = GetArchives(formAccumulator.GetResults());
                        }
                    }

                    // Drains any remaining section body that has not been consumed and
                    // reads the headers for the next section.
                    section = await reader.ReadNextSectionAsync();
                }
                // OJO
                var document = new Common.Import_Product();

                document.list_Agreement_Document = GetArchives(formAccumulator.GetResults());

                document = await ProcessPathArchiveAsync(document, extension);

                var message = new Message();
                message.BusinessLogic = configuration.GetValue <string>("AppSettings:BusinessLogic:Gbl_Mtr_Evidence");
                message.Connection    = configuration.GetValue <string>("ConnectionStrings:MEXPRESS_AC");
                message.Operation     = Operation.Save;
                message.MessageInfo   = document.SerializeObject();
                using (var businessLgic = new ServiceManager())
                {
                    var result = await businessLgic.DoWork(message);

                    if (result.Status == Status.Failed)
                    {
                        return(BadRequest(result.Result));
                    }
                    var resultModel = result.DeSerializeObject <Common.Ac_Mtr_Agreement_Document>();

                    var dataSuccess = new
                    {
                        Data          = resultModel,
                        MessageResult = Backend.Common.Enum.Status.Success,
                        Message       = string.Empty,
                        RegisterType  = string.Empty
                    };

                    return(Ok(dataSuccess));
                }
            }
            catch (System.Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Creates a data element by reading the first multipart element or body of the request.
        /// </summary>
        private async Task <(Stream, DataElement)> ReadRequestAndCreateDataElementAsync(HttpRequest request, string elementType, List <Guid> refs, Instance instance)
        {
            DateTime creationTime = DateTime.UtcNow;
            Stream   theStream    = null;

            string contentType     = null;
            string contentFileName = null;
            long   fileSize        = 0;

            if (MultipartRequestHelper.IsMultipartContentType(request.ContentType))
            {
                // Only read the first section of the mulitpart message.
                MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse(request.ContentType);
                string boundary = MultipartRequestHelper.GetBoundary(mediaType, _defaultFormOptions.MultipartBoundaryLengthLimit);

                MultipartSection section = null;

                MultipartReader reader = new MultipartReader(boundary, request.Body);
                section = await reader.ReadNextSectionAsync();

                theStream   = section.Body;
                contentType = section.ContentType;

                bool hasContentDisposition = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out ContentDispositionHeaderValue contentDisposition);

                if (hasContentDisposition)
                {
                    contentFileName = HttpUtility.UrlDecode(contentDisposition.FileName.ToString());
                    fileSize        = contentDisposition.Size ?? 0;
                }
            }
            else
            {
                theStream = request.Body;
                if (request.Headers.TryGetValue("Content-Disposition", out StringValues headerValues))
                {
                    string        contentDisposition = headerValues.ToString();
                    List <string> contenDispValues   = contentDisposition.Split(';').ToList();

                    string fileNameValue = contenDispValues.FirstOrDefault(x => x.Contains("filename", StringComparison.CurrentCultureIgnoreCase));

                    if (!string.IsNullOrEmpty(fileNameValue))
                    {
                        string[] valueParts = fileNameValue.Split('=');

                        if (valueParts.Count() == 2)
                        {
                            contentFileName = HttpUtility.UrlDecode(valueParts[1]);
                        }
                    }
                }

                contentType = request.ContentType;
            }

            string user = User.GetUserOrOrgId();

            DataElement newData = DataElementHelper.CreateDataElement(elementType, refs, instance, creationTime, contentType, contentFileName, fileSize, user);

            return(theStream, newData);
        }
Exemplo n.º 17
0
        public async Task <IActionResult> Upload()
        {
            var user = await base.GetAuthenticatedUserAsync();

            if (user == null)
            {
                return(base.UnauthorizedException());
            }

            // Ensure we are dealing with a multipart request
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
            }

            // Used to accumulate all the form url
            // encoded key value pairs in the request.
            var formAccumulator = new KeyValueAccumulator();
            var boundary        = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType),
                _defaultFormOptions.MultipartBoundaryLengthLimit);

            var reader = new MultipartReader(boundary, HttpContext.Request.Body);

            var  name          = string.Empty;
            var  contentType   = string.Empty;
            long contentLength = 0;

            byte[] bytes = null;

            var section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);

                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        name        = contentDisposition.FileName.ToString();
                        contentType = section.ContentType;

                        // Create an in-memory stream to get the bytes and length
                        using (var ms = new MemoryStream())
                        {
                            // Read the seciton into our memory stream
                            await section.Body.CopyToAsync(ms);

                            // get bytes and length
                            bytes         = ms.StreamToByteArray();
                            contentLength = ms.Length;
                        }
                    }
                    else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                    {
                        // Content-Disposition: form-data; name="key" value
                        // Do not limit the key name length here because the
                        // multipart headers length limit is already in effect.

                        var key      = HeaderUtilities.RemoveQuotes(contentDisposition.Name).ToString();
                        var encoding = GetEncoding(section);

                        using (var streamReader = new StreamReader(
                                   section.Body,
                                   encoding,
                                   detectEncodingFromByteOrderMarks: true,
                                   bufferSize: 1024,
                                   leaveOpen: true))
                        {
                            // The value length limit is enforced by MultipartBodyLengthLimit
                            var value = await streamReader.ReadToEndAsync();

                            if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
                            {
                                value = String.Empty;
                            }
                            formAccumulator.Append(key, value);

                            if (formAccumulator.ValueCount > _defaultFormOptions.ValueCountLimit)
                            {
                                throw new InvalidDataException($"Form key count limit {_defaultFormOptions.ValueCountLimit} exceeded.");
                            }
                        }
                    }
                }

                // Drains any remaining section body that has not been consumed and
                // reads the headers for the next section.
                section = await reader.ReadNextSectionAsync();
            }

            // Get btye array from memory stream for storage

            var output = new List <UploadedFile>();

            if (bytes == null)
            {
                return(BadRequest($"Could not obtain a byte array for the uploaded file."));
            }

            // Store media
            var media = await _mediaStore.CreateAsync(new Models.Media
            {
                Name          = name,
                ContentType   = contentType,
                ContentLength = contentLength,
                ContentBlob   = bytes,
                CreatedUserId = user.Id
            });

            // Build friendly results
            if (media != null)
            {
                output.Add(new UploadedFile()
                {
                    Id           = media.Id,
                    Name         = media.Name,
                    FriendlySize = media.ContentLength.ToFriendlyFileSize(),
                    IsImage      = IsContentTypeSupported(media.ContentType, SupportedImageContentTypes),
                    IsBinary     = IsContentTypeSupported(media.ContentType, SupportedBinaryContentTypes),
                });
            }

            return(base.Result(output));
        }
Exemplo n.º 18
0
            public async Task <Response> Handle(Request request, CancellationToken cancellationToken)
            {
                var httpContext        = _httpContextAccessor.HttpContext;
                var defaultFormOptions = new FormOptions();
                var digitalAssets      = new List <DigitalAsset>();

                if (!MultipartRequestHelper.IsMultipartContentType(httpContext.Request.ContentType))
                {
                    throw new Exception($"Expected a multipart request, but got {httpContext.Request.ContentType}");
                }

                var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(httpContext.Request.ContentType);

                var boundary = MultipartRequestHelper.GetBoundary(
                    mediaTypeHeaderValue,
                    defaultFormOptions.MultipartBoundaryLengthLimit);

                var reader = new MultipartReader(boundary, httpContext.Request.Body);

                var section = await reader.ReadNextSectionAsync();

                while (section != null)
                {
                    DigitalAsset digitalAsset = default;

                    var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out ContentDispositionHeaderValue contentDisposition);

                    if (hasContentDispositionHeader)
                    {
                        if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                        {
                            using (var targetStream = new MemoryStream())
                            {
                                await section.Body.CopyToAsync(targetStream);

                                var name        = $"{contentDisposition.FileName}".Trim(new char[] { '"' }).Replace("&", "and");
                                var bytes       = StreamHelper.ReadToEnd(targetStream);
                                var contentType = section.ContentType;

                                digitalAsset = _context.DigitalAssets.SingleOrDefault(x => x.Name == name);

                                if (digitalAsset == null)
                                {
                                    digitalAsset = new DigitalAsset(name, bytes, contentType);

                                    _context.DigitalAssets.Add(digitalAsset);
                                }
                                else
                                {
                                    digitalAsset.Update(bytes, contentType);
                                }
                            }
                        }
                    }

                    digitalAssets.Add(digitalAsset);

                    section = await reader.ReadNextSectionAsync();
                }

                await _context.SaveChangesAsync(cancellationToken);

                return(new ()
                {
                    DigitalAssetIds = digitalAssets.Select(x => x.DigitalAssetId).ToList()
                });
            }
Exemplo n.º 19
0
        public async Task <IActionResult> Upload()
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest());
            }

            try
            {
                if (!Request.Form.Files.Any())
                {
                    return(BadRequest("There is no file."));
                }

                var client = new MongoDB.Driver.MongoClient("mongodb://*****:*****@localhost/?safe=true");
                var db     = client.GetDatabase("memoryvault");

                var coll = db.GetCollection <MemoryItemModel>("memories");

                foreach (var fileReq in Request.Form.Files)
                {
                    GridFSBucketOptions opts = new GridFSBucketOptions();
                    opts.BucketName = "memoryfiles";

                    var bucket = new GridFSBucket(db, opts);

                    ObjectId res;

                    using (var stream = fileReq.OpenReadStream())
                    {
                        res = await bucket.UploadFromStreamAsync(fileReq.FileName, stream);
                    }

                    var ext = System.IO.Path.GetExtension(fileReq.FileName);

                    var memory = new MemoryItemModel
                    {
                        CreateTime    = DateTime.Now,
                        ItemId        = res,
                        FileName      = fileReq.FileName,
                        FileExtension = ext
                    };

                    await coll.InsertOneAsync(memory);

                    QueueRepository.Enqueue(new CreateThumbnailCommand {
                        MemoryId = memory.Id.ToString()
                    });
                }
            }
            catch (MongoWriteException exc)
            {
                if (((MongoBulkWriteException)exc.InnerException).WriteErrors.Any(p => p.Category == ServerErrorCategory.DuplicateKey))
                {
                    return(Ok());
                }

                return(Ok(exc.Message));
            }
            catch (Exception exc)
            {
                return(Ok(exc.Message));
            }

            return(Ok());
        }
Exemplo n.º 20
0
        public async Task <ImportFormParserResult> ParseImportFormAsync(HttpRequest request, string userId,
                                                                        Func <ImportFormData, IFormCollection, Task <bool> > bindAsyncFunc)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (userId == null)
            {
                throw new ArgumentNullException(nameof(userId));
            }
            if (bindAsyncFunc == null)
            {
                throw new ArgumentNullException(nameof(bindAsyncFunc));
            }
            if (!MultipartRequestHelper.IsMultipartContentType(request.ContentType))
            {
                return(new ImportFormParserResult($"Expected a multipart request, but got {request.ContentType}"));
            }

            var defaultFormOptions = new FormOptions();

            var jobInfo = new ImportJobRequestInfo
            {
                UserId = userId
            };

            var formAccumulator = new KeyValueAccumulator();

            var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(request.ContentType),
                                                              defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader  = new MultipartReader(boundary, request.Body);
            var section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                var hasContentDispositionHeader =
                    ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        jobInfo.CsvFileName = string.Empty;
                        jobInfo.DatasetId   = string.Empty;
                        Log.Information("api/data(POST): Starting conversion job. UserId='{0}', File='{1}'", userId,
                                        "");
                        var csvFileId = await _fileStore.AddFileAsync(section.Body);

                        Log.Information($"Saved the uploaded CSV file '{csvFileId}'");
                        jobInfo.CsvFileId = csvFileId;
                    }
                    else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                    {
                        var key      = HeaderUtilities.RemoveQuotes(contentDisposition.Name);
                        var encoding = GetEncoding(section);
                        using (var streamReader = new StreamReader(
                                   section.Body,
                                   encoding,
                                   detectEncodingFromByteOrderMarks: true,
                                   bufferSize: 1024,
                                   leaveOpen: true))
                        {
                            // The value length limit is enforced by MultipartBodyLengthLimit
                            var value = await streamReader.ReadToEndAsync();

                            if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
                            {
                                value = String.Empty;
                            }

                            formAccumulator.Append(key.ToString(), value);

                            if (formAccumulator.ValueCount > defaultFormOptions.ValueCountLimit)
                            {
                                throw new InvalidDataException(
                                          $"Form key count limit {defaultFormOptions.ValueCountLimit} exceeded.");
                            }
                        }
                    }
                }

                // Drains any remaining section body that has not been consumed and
                // reads the headers for the next section.
                section = await reader.ReadNextSectionAsync();
            }

            // Bind form data to a model
            var formData          = new ImportFormData();
            var bindingSuccessful = await bindAsyncFunc(formData, new FormCollection(formAccumulator.GetResults()));

            if (!bindingSuccessful)
            {
                return(new ImportFormParserResult("Import form data validation failed"));
            }

            jobInfo.OwnerId      = formData.OwnerId;
            jobInfo.RepositoryId = formData.RepoId;
            if (string.IsNullOrEmpty(formData.OwnerId) || string.IsNullOrEmpty(formData.RepoId))
            {
                Log.Error("DataController: POST called with no owner or repo set in FormData");
                return(new ImportFormParserResult("No target repository supplied"));
            }

            if (formData.Metadata == null)
            {
                Log.Error("DataController: POST called with no metadata present in FormData");
                return(new ImportFormParserResult("No metadata supplied"));
            }

            var parser = new JsonSerializer();

            Log.Debug("DataController: Metadata: {0}", formData.Metadata);
            var metadataObject = parser.Deserialize(new JsonTextReader(new StringReader(formData.Metadata))) as JObject;

            if (metadataObject == null)
            {
                Log.Error(
                    "DataController: Error deserializing metadata as object, unable to create conversion job. Metadata = '{0}'",
                    formData.Metadata);
                return(new ImportFormParserResult("Metadata badly formatted"));
            }

            var datasetIri   = metadataObject["url"]?.ToString();
            var datasetTitle = metadataObject["dc:title"]?.ToString();

            if (string.IsNullOrEmpty(datasetIri))
            {
                Log.Error("DataController: No dataset IRI supplied in metadata.");
                return(new ImportFormParserResult("No dataset IRI supplied in metadata"));
            }

            var datasetId       = formData.Filename;
            var datasetIriSplit = datasetIri.Split("/");

            if (datasetIriSplit != null && datasetIriSplit.Length > 1)
            {
                datasetId = datasetIriSplit[datasetIriSplit.Length - 1];
            }

            Log.Debug("DataController: datasetIri = '{0}'", datasetIri);
            jobInfo.DatasetIri = datasetIri;

            // save CSVW to file storage
            if (string.IsNullOrEmpty(jobInfo.CsvFileName))
            {
                jobInfo.CsvFileName = formData.Filename;
                jobInfo.DatasetId   = datasetId;
            }
            jobInfo.IsPublic = formData.ShowOnHomePage;

            byte[] byteArray      = Encoding.UTF8.GetBytes(formData.Metadata);
            var    metadataStream = new MemoryStream(byteArray);
            var    csvwFileId     = await _fileStore.AddFileAsync(metadataStream);

            jobInfo.CsvmFileId            = csvwFileId;
            jobInfo.OverwriteExistingData = formData.OverwriteExisting;

            if (formData.SaveAsSchema)
            {
                Log.Information("api/data(POST): Saving metadata as template.");

                var schema = new JObject(new JProperty("dc:title", "Template from " + datasetTitle),
                                         new JProperty("metadata", metadataObject));
                Log.Information("api/data(POST): Starting schema creation job. UserId='{0}', Repository='{1}'", userId,
                                formData.RepoId);

                byte[] schemaByteArray = Encoding.UTF8.GetBytes(schema.ToString());
                var    schemaStream    = new MemoryStream(schemaByteArray);
                var    schemaFileId    = await _fileStore.AddFileAsync(schemaStream);

                if (!string.IsNullOrEmpty(schemaFileId))
                {
                    Log.Information("api/data(POST): Schema temp file saved: {0}.", schemaFileId);
                    var schemaJobRequest = new SchemaImportJobRequestInfo()
                    {
                        UserId       = userId,
                        SchemaFileId = schemaFileId,
                        OwnerId      = formData.OwnerId,
                        RepositoryId = formData.RepoId
                    };
                    return(new ImportFormParserResult(jobInfo, schemaJobRequest, formData.Metadata));
                }

                Log.Error(
                    "api/data(POST): Error saving schema content to temporary file storage, unable to start schema creation job");
            }

            return(new ImportFormParserResult(jobInfo, formData.Metadata));
        }
Exemplo n.º 21
0
        public async Task StreamUpload()
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                throw new AbpException($"Expected a multipart request, but got {Request.ContentType}");
            }

            var uniqueName      = "";
            var formAccumulator = new KeyValueAccumulator();
            var targetFilePath  = _environment.WebRootPath + "\\uploads\\";

            if (!Directory.Exists(targetFilePath))
            {
                Directory.CreateDirectory(targetFilePath);
            }

            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType),
                FormOptions.DefaultMultipartBoundaryLengthLimit);

            var reader = new MultipartReader(boundary, HttpContext.Request.Body);

            var section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                var hasContentDispositionHeader =
                    ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);

                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        uniqueName = Guid.NewGuid() + "_" + contentDisposition.FileName;
                        var fullPath = Path.Combine(targetFilePath, uniqueName);
                        using (var targetStream = System.IO.File.Create(fullPath))
                        {
                            await section.Body.CopyToAsync(targetStream);
                        }
                    }
                    else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                    {
                        var key = HeaderUtilities.RemoveQuotes(contentDisposition.Name);
                        using (var streamReader = new StreamReader(
                                   section.Body,
                                   true))
                        {
                            // The value length limit is enforced by MultipartBodyLengthLimit
                            var value = await streamReader.ReadToEndAsync();

                            if (string.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
                            {
                                value = string.Empty;
                            }

                            formAccumulator.Append(key.ToString(), value);

                            if (formAccumulator.ValueCount > FormOptions.DefaultBufferBodyLengthLimit)
                            {
                                throw new AbpException("Form key count limit exceeded");
                            }
                        }
                    }
                    section = await reader.ReadNextSectionAsync();
                }
            }
            var userInfo          = new UserInfoDto();
            var formValueProvider = new FormValueProvider(
                BindingSource.Form,
                new FormCollection(formAccumulator.GetResults()),
                CultureInfo.CurrentCulture);
            var bindingSuccessful = await TryUpdateModelAsync(userInfo, "", formValueProvider);

            if (bindingSuccessful)
            {
                if (ModelState.IsValid)
                {
                    var user = new UserInfo
                    {
                        Username  = userInfo.Username,
                        LastName  = userInfo.LastName,
                        FirstName = userInfo.FirstName,
                        Role      = userInfo.Role,
                        Image     = uniqueName
                    };

                    _repository.Insert(user);
                }
            }
        }
Exemplo n.º 22
0
        public async Task <IActionResult> Post()
        {
            var messages = new LinkedList <string>();

            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
            }

            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType),
                DefaultFormOptions.MultipartBoundaryLengthLimit);
            var reader = new MultipartReader(boundary, HttpContext.Request.Body);

            var parser = new AParser(_aContext, _fileStore, _logger, messages);
            MultipartSection section;

            while ((section = await reader.ReadNextSectionAsync()) != null)
            {
                ContentDispositionHeaderValue contentDisposition = null;
                if (!ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition))
                {
                    continue;
                }

                var match = ParamParser.Match(HeaderUtilities.RemoveQuotes(contentDisposition.Name));
                if (!match.Success)
                {
                    messages.AddLast($"Unexpected parameter {contentDisposition.Name}. Expected guid[index], file1[index], file2[index], file3[index].");
                    continue;
                }

                var field = match.Groups["field"].Value;
                var index = int.Parse(match.Groups["index"].Value);

                if (string.Equals(field, "guid", StringComparison.OrdinalIgnoreCase))
                {
                    if (!MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                    {
                        messages.AddLast($"A form data content disposition is expected but was not provided for guid[{index}].");
                        continue;
                    }

                    var encoding = GetEncoding(section);
                    using (var streamReader = new StreamReader(
                               section.Body,
                               encoding,
                               detectEncodingFromByteOrderMarks: true,
                               bufferSize: _bufferSize,
                               leaveOpen: true))
                    {
                        // The value length limit is enforced by MultipartBodyLengthLimit
                        var value = await streamReader.ReadToEndAsync();

                        Guid id;
                        if (!Guid.TryParse(value, out id))
                        {
                            messages.AddLast($"Unable to parse guid[{index}].");
                            continue;
                        }
                        await parser.NextId(index, id);
                    }
                }
                else // file1, file2 or file3
                {
                    if (!MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        messages.AddLast($"A file content disposition is expected but was not provided for {field}[{index}].");
                        continue;
                    }

                    var fileNo = field[4] - '0';
                    await parser.NextFile(index, fileNo, section.Body);
                }
            }
            await parser.Finish();

            return(Json(messages));
        }
Exemplo n.º 23
0
        private async Task <(GroupDto, ImageDto?)> AdminUploadGroupImageMultipartContent(Guid userId, Stream requestBody, string?contentType, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            // Check if HttpRequest (Form Data) is a Multipart Content Type
            if (!MultipartRequestHelper.IsMultipartContentType(contentType))
            {
                throw new InvalidDataException($"Expected a multipart request, but got {contentType}");
            }

            var now = _systemClock.UtcNow.UtcDateTime;

            var defaultFormOptions = new FormOptions();
            // Create a Collection of KeyValue Pairs.
            var formAccumulator = new KeyValueAccumulator();
            // Determine the Multipart Boundary.
            var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(contentType), defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader   = new MultipartReader(boundary, requestBody);
            var section  = await reader.ReadNextSectionAsync(cancellationToken);

            ImageDto?imageDto = null;
            var      groupDto = new GroupDto();

            // Loop through each 'Section', starting with the current 'Section'.
            while (section is not null)
            {
                // Check if the current 'Section' has a ContentDispositionHeader.
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        if (contentDisposition is not null)
                        {
                            var sectionFileName = contentDisposition.FileName.Value;
                            // use an encoded filename in case there is anything weird
                            var encodedFileName = WebUtility.HtmlEncode(Path.GetFileName(sectionFileName));

                            // read the section filename to get the content type
                            var fileExtension = Path.GetExtension(sectionFileName);

                            // now make it unique
                            var uniqueFileName = $"{Guid.NewGuid()}{fileExtension}";

                            if (!_acceptedFileTypes.Contains(fileExtension.ToLower()))
                            {
                                _logger.LogError("file extension:{0} is not an accepted image file", fileExtension);
                                throw new ValidationException("Image", "The image is not in an accepted format");
                            }

                            var compressedImage = _imageService.TransformImageForGroupHeader(section.Body);

                            try
                            {
                                await _blobStorageProvider.UploadFileAsync(compressedImage.Image, uniqueFileName,
                                                                           MimeTypesMap.GetMimeType(encodedFileName), cancellationToken);
                            }
                            catch (Exception ex)
                            {
                                _logger.LogError(ex, "An error occurred uploading file to blob storage");
                                throw;
                            }

                            // trick to get the size without reading the stream in memory
                            var size = section.Body.Position;

                            // TODO MimeDetective does not work when stream has already been uploaded - figure out a solution
                            //if (fileContentTypeMatchesExtension is false)
                            //{
                            //    await _blobStorageProvider.DeleteFileAsync(uniqueFileName);
                            //    _logger.LogError("File extension:{0} does not match the file signature", fileExtension);
                            //    throw new FormatException("File extension} does not match the file signature");
                            //}

                            imageDto = new ImageDto
                            {
                                FileSizeBytes = size,
                                FileName      = uniqueFileName,
                                Height        = compressedImage.Height,
                                Width         = compressedImage.Width,
                                IsDeleted     = false,
                                MediaType     = compressedImage.MediaType,
                                CreatedBy     = userId,
                                CreatedAtUtc  = now
                            };

                            var imageValidator        = new ImageValidator(MaxFileSizeBytes);
                            var imageValidationResult = await imageValidator.ValidateAsync(imageDto, cancellationToken);

                            if (imageValidationResult.Errors.Count > 0)
                            {
                                await _blobStorageProvider.DeleteFileAsync(uniqueFileName);

                                _logger.LogError("File size:{0} is greater than the max allowed size:{1}", size, MaxFileSizeBytes);
                                throw new ValidationException(imageValidationResult);
                            }
                        }
                    }
                    else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                    {
                        // if for some reason other form data is sent it would get processed here
                        var key = HeaderUtilities.RemoveQuotes(contentDisposition?.Name.ToString().ToLowerInvariant());

                        var encoding = GetEncoding(section);
                        using var streamReader = new StreamReader(section.Body, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true);

                        var value = await streamReader.ReadToEndAsync();

                        if (string.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
                        {
                            value = string.Empty;
                        }
                        formAccumulator.Append(key.Value, value);
                        if (formAccumulator.ValueCount > defaultFormOptions.ValueCountLimit)
                        {
                            _logger.LogError("GroupEdit: Form key count limit {0} exceeded.", defaultFormOptions.ValueCountLimit);
                            throw new FormatException($"Form key count limit { defaultFormOptions.ValueCountLimit } exceeded.");
                        }
                    }
                }
                // Begin reading the next 'Section' inside the 'Body' of the Request.
                section = await reader.ReadNextSectionAsync(cancellationToken);
            }

            if (formAccumulator.HasValues)
            {
                var formValues = formAccumulator.GetResults();

                // Get values from multipart form
                var nameFound = formValues.TryGetValue("name", out var name);
                if (nameFound is false)
                {
                    throw new ArgumentNullException($"Name was not provided");
                }
                var strapLineFound = formValues.TryGetValue("strapline", out var strapLine);
                if (strapLineFound is false)
                {
                    throw new ArgumentNullException($"Strap Line was not provided");
                }
                var themeFound = formValues.TryGetValue("themeid", out var theme);
                if (themeFound is false)
                {
                    throw new ArgumentNullException($"Theme was not provided");
                }

                formValues.TryGetValue("imageid", out var image);
                formValues.TryGetValue("groupOwnerId", out var groupOwner);
                formValues.TryGetValue("groupAdminId", out var groupAdmin);

                if (Guid.TryParse(theme, out var themeId) is false || themeId == new Guid())
                {
                    throw new ValidationException(nameof(GroupDto.ThemeId), "Enter the theme");
                }

                var imageId = Guid.TryParse(image, out var imageGuid) ? (Guid?)imageGuid : null;
                if (imageId.HasValue)
                {
                    if (imageId == new Guid())
                    {
                        throw new ArgumentOutOfRangeException($"Incorrect Id provided");
                    }
                }

                var groupOwnerId = Guid.TryParse(groupOwner, out var groupOwnerGuid) ? (Guid?)groupOwnerGuid : null;
                if (groupOwnerId.HasValue)
                {
                    if (groupOwnerId == new Guid())
                    {
                        throw new ArgumentOutOfRangeException($"Incorrect Id provided");
                    }
                }

                var groupAdminGuids = new List <Guid?>();
                foreach (var user in groupAdmin)
                {
                    var groupUserId = Guid.TryParse(user, out var groupUserGuid) ? (Guid?)groupUserGuid : null;

                    if (groupUserId.HasValue)
                    {
                        if (groupUserId == new Guid())
                        {
                            throw new ArgumentOutOfRangeException($"Incorrect Id provided");
                        }

                        groupAdminGuids.Add(groupUserId);
                    }
                }

                formValues.TryGetValue("isPublic", out var publicGroup);
                var isPublic = bool.TryParse(publicGroup, out var isPublicBool) ? isPublicBool : false;

                groupDto = new GroupDto
                {
                    Slug            = Guid.NewGuid().ToString(),
                    Name            = _htmlSanitizer.Sanitize(name),
                    Strapline       = _htmlSanitizer.Sanitize(strapLine),
                    ThemeId         = themeId,
                    ImageId         = imageId,
                    GroupOwnerId    = groupOwnerId,
                    GroupAdminUsers = groupAdminGuids,
                    ModifiedBy      = userId,
                    ModifiedAtUtc   = now,
                    CreatedAtUtc    = now,
                    IsPublic        = isPublic,
                };
            }

            return(groupDto, imageDto);
        }
Exemplo n.º 24
0
        public async Task <IActionResult> UploadDatabase()
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                ModelState.AddModelError("File",
                                         $"The request couldn't be processed (Error 1).");
                // Log error

                return(BadRequest(ModelState));
            }

            // User Profile
            var name    = User.Identity.Name;
            var profile = await _userManager.FindByNameAsync(name);

            // Accumulate the form data key-value pairs in the request (formAccumulator).
            var formAccumulator               = new KeyValueAccumulator();
            var trustedFileNameForDisplay     = string.Empty;
            var untrustedFileNameForStorage   = string.Empty;
            var trustedFilePathStorage        = string.Empty;
            var trustedFileNameForFileStorage = string.Empty;
            var streamedFileImageContent      = new byte[0];
            var streamedFilePhysicalContent   = new byte[0];


            // List Byte for file storage
            List <byte[]> filesByteStorage         = new List <byte[]>();
            List <string> filesNameStorage         = new List <string>();
            List <string> storedPaths              = new List <string>();
            List <string> storedPathDictionaryKeys = new List <string>();
            var           fileStoredData           = new Dictionary <string, byte[]>();

            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType),
                _defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader = new MultipartReader(boundary, HttpContext.Request.Body);

            var section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                var hasContentDispositionHeader =
                    ContentDispositionHeaderValue.TryParse(
                        section.ContentDisposition, out var contentDisposition);

                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper
                        .HasFileContentDisposition(contentDisposition))
                    {
                        untrustedFileNameForStorage = contentDisposition.FileName.Value;
                        // Don't trust the file name sent by the client. To display
                        // the file name, HTML-encode the value.
                        trustedFileNameForDisplay = WebUtility.HtmlEncode(
                            contentDisposition.FileName.Value);

                        if (!Directory.Exists(_targetFilePath))
                        {
                            string path = String.Format("{0}", _targetFilePath);
                            Directory.CreateDirectory(path);
                        }

                        //streamedFileContent =
                        //    await FileHelpers.ProcessStreamedFile(section, contentDisposition,
                        //        ModelState, _permittedExtentions, _fileSizeLimit);

                        streamedFilePhysicalContent = await FileHelpers.ProcessStreamedFile(
                            section, contentDisposition, ModelState,
                            _permittedExtentions, _fileSizeLimit);

                        filesNameStorage.Add(trustedFileNameForDisplay);
                        filesByteStorage.Add(streamedFilePhysicalContent);
                        fileStoredData.Add(trustedFileNameForDisplay, streamedFilePhysicalContent);

                        if (!ModelState.IsValid)
                        {
                            return(BadRequest(ModelState));
                        }
                    }
                    else if (MultipartRequestHelper
                             .HasFormDataContentDisposition(contentDisposition))
                    {
                        // Don't limit the key name length because the
                        // multipart headers length limit is already in effect.
                        var key = HeaderUtilities
                                  .RemoveQuotes(contentDisposition.Name).Value;
                        var encoding = GetEncoding(section);

                        if (encoding == null)
                        {
                            ModelState.AddModelError("File",
                                                     $"The request couldn't be processed (Error 2).");
                            // Log error

                            return(BadRequest(ModelState));
                        }

                        using (var streamReader = new StreamReader(
                                   section.Body,
                                   encoding,
                                   detectEncodingFromByteOrderMarks: true,
                                   bufferSize: 1024,
                                   leaveOpen: true))
                        {
                            // The value length limit is enforced by
                            // MultipartBodyLengthLimit
                            var value = await streamReader.ReadToEndAsync();

                            if (string.Equals(value, "undefined",
                                              StringComparison.OrdinalIgnoreCase))
                            {
                                value = string.Empty;
                            }

                            formAccumulator.Append(key, value);

                            if (formAccumulator.ValueCount >
                                _defaultFormOptions.ValueCountLimit)
                            {
                                // Form key count limit of
                                // _defaultFormOptions.ValueCountLimit
                                // is exceeded.
                                ModelState.AddModelError("File",
                                                         $"The request couldn't be processed (Error 3).");
                                // Log error

                                return(BadRequest(ModelState));
                            }
                        }
                    }
                }

                // Drain any remaining section body that hasn't been consumed and
                // read the headers for the next section.
                section = await reader.ReadNextSectionAsync();
            }

            // Bind form data to the model
            var formData          = new StreamFormDataMusic();
            var formValueProvider = new FormValueProvider(
                BindingSource.Form,
                new FormCollection(formAccumulator.GetResults()),
                CultureInfo.CurrentCulture);
            var bindingSuccessful = await TryUpdateModelAsync(formData, prefix : "",
                                                              valueProvider : formValueProvider);

            var keyPathFolder = FilePathUrlHelper.GenerateKeyPath(profile.Id);

            trustedFilePathStorage = String.Format("{0}\\{1}\\{2}\\{3}",
                                                   _targetFolderPath,
                                                   keyPathFolder,
                                                   GenerateSecurity.Encode(profile.Id),
                                                   Path.GetRandomFileName());

            if (!bindingSuccessful)
            {
                ModelState.AddModelError("File",
                                         "The request couldn't be processed (Error 5).");
                // Log error

                return(BadRequest(ModelState));
            }

            // **WARNING!**
            // In the following example, the file is saved without
            // scanning the file's contents. In most production
            // scenarios, an anti-virus/anti-malware scanner API
            // is used on the file before making the file available
            // for download or for use by other systems.
            // For more information, see the topic that accompanies
            // this sample app.

            Directory.CreateDirectory(trustedFilePathStorage);

            foreach (var item in fileStoredData)
            {
                using (var targetStream = System.IO.File.Create(
                           Path.Combine(trustedFilePathStorage, item.Key)))
                {
                    await targetStream.WriteAsync(item.Value);

                    _logger.LogInformation(
                        "Uploaded file '{TrustedFileNameForDisplay}' saved to " +
                        "'{TargetFilePath}' as {TrustedFileNameForFileStorage}",
                        item.Key, trustedFilePathStorage,
                        item.Key);
                }
                storedPaths.Add(Path.Combine(trustedFilePathStorage, item.Key));
                storedPathDictionaryKeys.Add(item.Key);
            }

            var keyValue   = storedPathDictionaryKeys[0];
            var keyConvert = fileStoredData[keyValue];
            var file       = new ClamUserMusic()
            {
                SongTitle   = formData.SongTitle,
                SongArtist  = formData.SongArtist,
                ItemPath    = storedPaths[0],
                Size        = keyConvert.Length,
                DateCreated = DateTime.Now,
                Status      = bool.Parse(formData.Status),
                UserId      = profile.Id
            };

            _context.Add(file);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 25
0
        public async Task <IActionResult> UploadingStream()
        {
            if (Directory.Exists(basePathOut))//如果存在文件删除
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(basePathOut);
                FileInfo[]    files         = directoryInfo.GetFiles();
                foreach (var f in files)
                {
                    f.Delete();
                }
            }



            List <FMFileInfo> items = new List <FMFileInfo>();

            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                ModelState.AddModelError("File",
                                         $"The request couldn't be processed (Error 1).");
                // Log error

                return(BadRequest(ModelState));
            }

            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType),
                _defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader  = new MultipartReader(boundary, HttpContext.Request.Body);
            var section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                var hasContentDispositionHeader =
                    ContentDispositionHeaderValue.TryParse(
                        section.ContentDisposition, out var contentDisposition);

                if (hasContentDispositionHeader)
                {
                    // This check assumes that there's a file
                    // present without form data. If form data
                    // is present, this method immediately fails
                    // and returns the model error.
                    if (!MultipartRequestHelper
                        .HasFileContentDisposition(contentDisposition))
                    {
                        ModelState.AddModelError("File",
                                                 $"The request couldn't be processed (Error 2).");
                        // Log error

                        return(BadRequest(ModelState));
                    }
                    else
                    {
                        // Don't trust the file name sent by the client. To display
                        // the file name, HTML-encode the value.
                        var trustedFileNameForDisplay = WebUtility.HtmlEncode(
                            contentDisposition.FileName.Value);
                        var ext = Path.GetExtension(trustedFileNameForDisplay).ToLowerInvariant();
                        var trustedFileNameForFileStorage = Guid.NewGuid().ToString("N") + ext;

                        // **WARNING!**
                        // In the following example, the file is saved without
                        // scanning the file's contents. In most production
                        // scenarios, an anti-virus/anti-malware scanner API
                        // is used on the file before making the file available
                        // for download or for use by other systems.
                        // For more information, see the topic that accompanies
                        // this sample.

                        var streamedFileContent = await FileHelpers.ProcessStreamedFile(
                            section, contentDisposition, ModelState,
                            _permittedExtensions, _fileSizeLimit);

                        if (!ModelState.IsValid)
                        {
                            return(BadRequest(ModelState));
                        }
                        var pathfull = Path.Combine(basePath, trustedFileNameForFileStorage);
                        if (!Directory.Exists(basePath))         //判断是否存在
                        {
                            Directory.CreateDirectory(basePath); //创建新路径
                        }
                        using (var targetStream = System.IO.File.Create(pathfull))
                        {
                            await targetStream.WriteAsync(streamedFileContent);

                            items.Add(new FMFileInfo
                            {
                                Name = trustedFileNameForDisplay,
                                Path = pathfull,
                            });
                            _logger.LogInformation(
                                "Uploaded file '{TrustedFileNameForDisplay}' saved to " +
                                "'{TargetFilePath}' as {TrustedFileNameForFileStorage}",
                                trustedFileNameForDisplay, basePath,
                                trustedFileNameForFileStorage);
                        }
                    }
                }

                // Drain any remaining section body that hasn't been consumed and
                // read the headers for the next section.
                section = await reader.ReadNextSectionAsync();
            }

            /*
             * //获取boundary
             * var boundary = HeaderUtilities.RemoveQuotes(MediaTypeHeaderValue.Parse(Request.ContentType).Boundary).Value;
             * //得到reader
             * var reader = new MultipartReader(boundary, HttpContext.Request.Body);
             * //{ BodyLengthLimit = 2000 };//
             * var section = await reader.ReadNextSectionAsync();
             *
             * List<FMFileInfo> items = new List<FMFileInfo>();
             * //读取section
             * while (section != null)
             * {
             *  var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
             *  if (hasContentDispositionHeader)
             *  {
             *      var trustedFileNameForDisplay = WebUtility.HtmlEncode(value: contentDisposition.FileName.Value);
             *
             *      var trustedFileNameForFileStorage = Path.GetRandomFileName();
             *      var pathfull = Path.Combine(basePath, trustedFileNameForFileStorage);
             *      if (!Directory.Exists(basePath))//判断是否存在
             *      {
             *          Directory.CreateDirectory(basePath);//创建新路径
             *      }
             *      _logger.LogInformation(
             *          $"Uploaded file '{trustedFileNameForDisplay}' saved to '{basePath}' as {trustedFileNameForFileStorage}");
             *      using (var targetStream = System.IO.File.Create(pathfull))
             *      {
             *          using (var memoryStream = new MemoryStream())
             *          {
             *              await section.Body.CopyToAsync(memoryStream);
             *              await targetStream.WriteAsync(memoryStream.ToArray());
             *          }
             *      }
             *      items.Add(new FMFileInfo
             *      {
             *          Name = trustedFileNameForDisplay,
             *          Path = pathfull,
             *      });
             *
             *  }
             *  section = await reader.ReadNextSectionAsync();
             * }
             *
             */
            var result = await base.createMultiEntity(items);

            return(result);
        }
Exemplo n.º 26
0
        public async Task <ActionResult <ContentItem[]> > UploadContent(string contentType, string storeId, [FromQuery] string folderUrl, [FromQuery] string url = null)
        {
            if (url == null && !MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
            }

            var retVal          = new List <ContentFile>();
            var storageProvider = _blobContentStorageProviderFactory.CreateProvider(GetContentBasePath(contentType, storeId));

            if (url != null)
            {
                var fileName = HttpUtility.UrlDecode(Path.GetFileName(url));
                var fileUrl  = folderUrl + "/" + fileName;

                using (var client = _httpClientFactory.CreateClient())
                    using (var blobStream = storageProvider.OpenWrite(fileUrl))
                        using (var remoteStream = await client.GetStreamAsync(url))
                        {
                            remoteStream.CopyTo(blobStream);

                            var сontentFile = AbstractTypeFactory <ContentFile> .TryCreateInstance();

                            сontentFile.Name = fileName;
                            сontentFile.Url  = storageProvider.GetAbsoluteUrl(fileUrl);
                            retVal.Add(сontentFile);
                        }
            }
            else
            {
                var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit);
                var reader   = new MultipartReader(boundary, HttpContext.Request.Body);

                var section = await reader.ReadNextSectionAsync();

                if (section != null)
                {
                    var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);

                    if (hasContentDispositionHeader)
                    {
                        var fileName = Path.GetFileName(contentDisposition.FileName.Value ?? contentDisposition.Name.Value.Replace("\"", string.Empty));

                        var targetFilePath = folderUrl + "/" + fileName;

                        using (var targetStream = storageProvider.OpenWrite(targetFilePath))
                        {
                            await section.Body.CopyToAsync(targetStream);
                        }

                        var contentFile = AbstractTypeFactory <ContentFile> .TryCreateInstance();

                        contentFile.Name = fileName;
                        contentFile.Url  = storageProvider.GetAbsoluteUrl(targetFilePath);
                        retVal.Add(contentFile);
                    }
                }
            }

            ContentCacheRegion.ExpireContent(($"content-{storeId}"));

            return(Ok(retVal.ToArray()));
        }
Exemplo n.º 27
0
        public async Task <IActionResult> Save()
        {
            string  targetFilePath = null;
            BookDTO dto            = null;

            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Requisição precisa ser 'multipart'."));
            }

            var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), MaxBoundaryLengthLimit);
            var reader   = new MultipartReader(boundary, HttpContext.Request.Body);
            var section  = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                ContentDispositionHeaderValue contentDisposition;
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);
                var key = HeaderUtilities.RemoveQuotes(contentDisposition.Name);
                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        if (key.ToString() == "file")
                        {
                            var imageValidator        = new ImageValidator();
                            var fileName              = HeaderUtilities.RemoveQuotes(contentDisposition.FileName).ToString();
                            var imageValidationResult = imageValidator.Validate(fileName);

                            if (imageValidationResult.HasError)
                            {
                                return(BadRequest(imageValidationResult.ErrorMessage));
                            }

                            targetFilePath = Path.GetTempFileName();
                            using (var targetStream = System.IO.File.Create(targetFilePath))
                            {
                                await section.Body.CopyToAsync(targetStream);
                            }
                        }
                    }
                    else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                    {
                        if (key.ToString() == "value")
                        {
                            using (var streamReader = new StreamReader(section.Body))
                            {
                                var json = await streamReader.ReadToEndAsync();

                                dto = JsonConvert.DeserializeObject <BookDTO>(json);
                            }
                        }
                    }
                }
                section = await reader.ReadNextSectionAsync();
            }
            var validationContext = new ValidationContext(bookFacade, authorService, publisherService);
            var bookValidator     = new BookValidator(validationContext);
            var validationResult  = bookValidator.Validate(dto);

            if (validationResult.HasError)
            {
                return(BadRequest(validationResult.ErrorMessage));
            }

            var bookFound = await bookFacade.FindById(dto.Id);

            if (bookFound == null && dto.Id != 0)
            {
                return(NotFound($"Nenhum livro encontrado com o ID: {dto.Id}."));
            }

            try
            {
                var book = dto.FromBookViewItem(new DefaultValidationResultDataLookUp(validationResult));

                book = await bookFacade.Save(book, targetFilePath);

                return(Ok(await book.ToBookViewItem(serviceDataLookUp)));
            }
            catch (BookSaveException ex)
            {
                try
                {
                    System.IO.File.Delete(targetFilePath);
                }
                catch
                {
                    //Ignore
                }
                return(BadRequest(ex.Message));
            }
            catch (ImageSaveException ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Exemplo n.º 28
0
        public async Task <IActionResult> Upload()
        {
            Debug.WriteLine("~~~~~~~~~~~~~~Post Request Received~~~~~~~~~~~~~~~~~");
            string[] _permittedExtensions = { ".jpg", ".iso", ".txt", ".msi", ".zip" };
            Int64    _fileSizeLimit       = Int64.MaxValue;
            string   _targetFilePath      = pathData.GetBasePath().Path;

            _targetFilePath = Path.Combine(_targetFilePath, "uploaded");

            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                ModelState.AddModelError("File",
                                         $"The request couldn't be processed (Error 1).");
                // Log error

                return(BadRequest(ModelState));
            }

            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType),
                int.MaxValue);
            var reader  = new Microsoft.AspNetCore.WebUtilities.MultipartReader(boundary, HttpContext.Request.Body);
            var section = await reader.ReadNextSectionAsync();

            Debug.WriteLine("~~~~~~~~~~~~~~ 1 ~~~~~~~~~~~~~~~~~");
            while (section != null)
            {
                var hasContentDispositionHeader =
                    ContentDispositionHeaderValue.TryParse(
                        section.ContentDisposition, out var contentDisposition);
                Debug.WriteLine("~~~~~~~~~~~~~~ 2 ~~~~~~~~~~~~~~~~~");
                if (hasContentDispositionHeader)
                {
                    if (!MultipartRequestHelper
                        .HasFileContentDisposition(contentDisposition))
                    {
                        ModelState.AddModelError("File",
                                                 $"The request couldn't be processed (Error 2).");
                        // Log error

                        return(BadRequest(ModelState));
                    }
                    else
                    {
                        Debug.WriteLine("~~~~~~~~~~~~~~ 3 ~~~~~~~~~~~~~~~~~");
                        var trustedFileNameForDisplay = WebUtility.HtmlEncode(
                            contentDisposition.FileName.Value);
                        var trustedFileNameForFileStorage = trustedFileNameForDisplay;


                        var streamedFileContent = await FileHelpers.ProcessStreamedFile(
                            section, contentDisposition, ModelState,
                            _permittedExtensions, _fileSizeLimit);

                        if (!ModelState.IsValid)
                        {
                            return(BadRequest(ModelState));
                        }
                        Debug.WriteLine("~~~~~~~~~~~~~~ 4 ~~~~~~~~~~~~~~~~~");
                        using (var targetStream = System.IO.File.Create(
                                   Path.Combine(_targetFilePath, trustedFileNameForFileStorage)))
                        {
                            Debug.WriteLine("~~~~~~~~~~~~~~ 5 ~~~~~~~~~~~~~~~~~");
                            await targetStream.WriteAsync(streamedFileContent);

                            Debug.WriteLine("~~~~~~~~~~~~~~ 6 ~~~~~~~~~~~~~~~~~");
                            Debug.WriteLine(
                                $"Uploaded file {trustedFileNameForDisplay} saved to " +
                                $"'{_targetFilePath}' as {trustedFileNameForFileStorage}");
                            Fyle fyle = new Fyle()
                            {
                                Name = trustedFileNameForDisplay, Folder = "uploaded"
                            };
                            fileData.AddFile(fyle);
                        }
                    }
                }

                // Drain any remaining section body that hasn't been consumed and
                // read the headers for the next section.
                section = await reader.ReadNextSectionAsync();
            }
            fileData.Commit();
            return(Created(nameof(FileController), null));
        }
        public async Task <IActionResult> Upload(UploadViewModel viewModel)
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                ModelState.AddModelError("File",
                                         $"The request couldn't be processed (Error 1).");
                // Log error

                return(BadRequest(ModelState));
            }

            IFormFile file     = Request.Form.Files[0];
            string    fileName = file.FileName;

            byte[] streamedFileContent = new byte[0];
            streamedFileContent = await FileHelper.ProcessStreamedFile(file, ModelState, _permittedExtensions, _fileSizeLimit);

            if (streamedFileContent.Length == 0)
            {
                return(BadRequest(ModelState));
            }

            if (ModelState.IsValid)
            {
                List <TransactionImport> imports = new List <TransactionImport>();
                FileStatus fileStatus            = _transactionFileService.Upload(file, imports);

                var transactionFile = new TransactionFile()
                {
                    Content    = streamedFileContent,
                    FileName   = fileName,
                    Note       = viewModel.Note,
                    Size       = streamedFileContent.Length,
                    UploadDate = DateTimeOffset.UtcNow,
                    FileStatus = fileStatus
                };

                await _transactionFileService.Create(_context, transactionFile);

                imports = imports.OrderBy(x => x.Id).Select(x => { x.TransactionFileId = transactionFile.Id; return(x); }).ToList();

                try
                {
                    _context.TransactionImport.AddRange(imports);
                    await _context.SaveChangesAsync();
                }
                catch (Exception e)
                {
                }

                if (fileStatus == FileStatus.Success)
                {
                    List <Transaction> transactions = new List <Transaction>();
                    transactions = imports.Where(x => x.ImportStatus == ImportStatus.Success).Select(x => new Transaction
                    {
                        TransactionIdentificator = x.TransactionId,
                        Amount              = (decimal)x.Amount,
                        CurrencyCode        = x.CurrencyCode,
                        TransactionDate     = (DateTimeOffset)x.TransactionDate,
                        Status              = x.Status,
                        TransactionImportId = x.Id,
                    }).ToList();

                    try
                    {
                        foreach (Transaction transaction in transactions)
                        {
                            await _transactionService.Create(_context, transaction);
                        }
                    }
                    catch (Exception e)
                    {
                    }
                }
            }

            return(Created(nameof(TransactionFileController), null));
        }
        public async Task <IActionResult> Upload()
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
            }

            var filePath = Directory.GetCurrentDirectory() + @"\UploadedFiles";

            Directory.CreateDirectory(filePath);
            var targetFilePath = filePath + @"\" + "test.csv";

            // Used to accumulate all the form url encoded key value pairs in the request.
            var formAccumulator = new KeyValueAccumulator();

            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType),
                DefaultFormOptions.MultipartBoundaryLengthLimit);
            var reader = new MultipartReader(boundary, HttpContext.Request.Body);

            var section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);

                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        //targetFilePath =Path.GetTempFileName();
                        using (var targetStream = System.IO.File.Create(targetFilePath))
                        {
                            await section.Body.CopyToAsync(targetStream);

                            _logger.LogInformation($"Copied the uploaded file '{targetFilePath}'");
                        }
                    }
                    else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                    {
                        // Content-Disposition: form-data; name="key" value
                        // Do not limit the key name length here because the
                        // multipart headers length limit is already in effect.
                        var key      = HeaderUtilities.RemoveQuotes(contentDisposition.Name);
                        var encoding = GetEncoding(section);
                        using (var streamReader = new StreamReader(
                                   section.Body,
                                   encoding,
                                   detectEncodingFromByteOrderMarks: true,
                                   bufferSize: 1024,
                                   leaveOpen: true))
                        {
                            // The value length limit is enforced by MultipartBodyLengthLimit
                            var value = await streamReader.ReadToEndAsync();

                            if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
                            {
                                value = String.Empty;
                            }

                            formAccumulator.Append(key.ToString(), value);

                            if (formAccumulator.ValueCount > DefaultFormOptions.ValueCountLimit)
                            {
                                throw new InvalidDataException($"Form key count limit {DefaultFormOptions.ValueCountLimit} exceeded.");
                            }
                        }
                    }
                }

                // Drains any remaining section body that has not been consumed and
                // reads the headers for the next section.
                section = await reader.ReadNextSectionAsync();
            }

            // Bind form data to a model
            var upload            = new Upload();
            var formValueProvider = new FormValueProvider(
                BindingSource.Form,
                new FormCollection(formAccumulator.GetResults()),
                CultureInfo.CurrentCulture);

            var bindingSuccessful = await TryUpdateModelAsync(upload, prefix : "", valueProvider : formValueProvider);

            if (!bindingSuccessful)
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
            }

            var uploadedData = new UploadedData()
            {
                FilePath = targetFilePath
            };

            _processor.Process(uploadedData);

            return(Ok(new { filePath = uploadedData.FilePath }));
        }