Пример #1
0
        public async Task <IActionResult> Upload()
        {
            var user = await base.GetAuthenticatedUserAsync();

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

            // Validate & process multi-part request
            // -------------------

            var result = await _multiPartRequestHandler.ProcessAsync(Request);

            // Return any errors parsing the multi-part request
            if (!result.Succeeded)
            {
                foreach (var error in result.Errors)
                {
                    return(BadRequest(error));
                }
            }

            // Build output
            // -------------------

            var output = new List <UploadedFile>();

            // Store media
            var media = await _mediaStore.CreateAsync(new Models.Media
            {
                Name          = result.Response.Name,
                ContentType   = result.Response.ContentType,
                ContentLength = result.Response.ContentLength,
                ContentBlob   = result.Response.ContentBytes,
                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));
        }
Пример #2
0
        public async Task <IActionResult> Post()
        {
            // Ensure we have permission
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.PostQuestionFiles))
            {
                return(Unauthorized());
            }

            // Get authenticated user
            var user = await base.GetAuthenticatedUserAsync();

            // We need to be authenticated
            if (user == null)
            {
                return(base.UnauthorizedException());
            }

            // Get current feature
            var feature = await _featureFacade.GetFeatureByIdAsync(ModuleId);

            // Ensure the feature exists
            if (feature == null)
            {
                throw new Exception($"A feature named \"{ModuleId}\" could not be found!");
            }

            // Validate temporary global unique identifier
            var guid = GetFileContentGuid();

            if (string.IsNullOrEmpty(guid))
            {
                return(BadRequest($"The \"{GuidKey}\" query string parameter is empty!"));
            }

            // Validate & process multipart request
            // -------------------

            var result = await _multiPartRequestHandler.ProcessAsync(Request);

            // Return any errors parsing the multipart request
            if (!result.Succeeded)
            {
                foreach (var error in result.Errors)
                {
                    return(BadRequest(error));
                }
            }

            // Build file
            // -------------------

            var md5     = result.Response.ContentBytes?.ToMD5().ToHex() ?? string.Empty;
            var newFile = new File
            {
                FeatureId       = feature.Id,
                Name            = result.Response.Name,
                Extension       = result.Response.Extension,
                ContentType     = result.Response.ContentType,
                ContentLength   = result.Response.ContentLength,
                ContentBlob     = result.Response.ContentBytes,
                ContentGuid     = guid,
                ContentCheckSum = md5,
                CreatedUserId   = user.Id,
                CreatedDate     = DateTimeOffset.Now
            };

            // Validate file
            // -------------------

            var output = new List <UploadResult>();

            var validationResult = await _fileValidator.ValidateAsync(newFile);

            if (validationResult.Succeeded)
            {
                // Create file
                var fileResult = await _fileManager.CreateAsync(newFile);

                // Build friendly result
                if (fileResult.Succeeded)
                {
                    output.Add(new UploadResult()
                    {
                        Id            = fileResult.Response.Id,
                        Name          = fileResult.Response.Name,
                        ContentType   = fileResult.Response.ContentType,
                        ContentLength = fileResult.Response.ContentLength,
                        FriendlySize  = fileResult.Response.ContentLength.ToFriendlyFileSize()
                    });
                }
                else
                {
                    foreach (var error in fileResult.Errors)
                    {
                        output.Add(new UploadResult()
                        {
                            Name  = result.Response.Name,
                            Error = error.Description
                        });
                    }
                }
            }
            else
            {
                foreach (var error in validationResult.Errors)
                {
                    output.Add(new UploadResult()
                    {
                        Name  = result.Response.Name,
                        Error = error.Description
                    });
                }
            }

            return(base.Result(output));
        }