Ejemplo n.º 1
0
        public async Task <ActionResult> Post(DataForServer dataForServer)
        {
            try
            {
                var inputContext = _inputContextService.CreateFromHttpContext(dataForServer.AccessToken, dataForServer.Language, dataForServer.Version, HttpContext);

                _logger.LogInformation("Received Request: {Type}, ID: {RequestId}, Has token: {HasToken}, Language: {Language}", dataForServer.Type, inputContext.RequestId, inputContext.AccessToken.HasValue, dataForServer.Language);

                var request = _requestDeserializer.Deserialize(dataForServer.Request, dataForServer.Type);

                _logger.LogInformation("Successfully deserialized the request");

                var result = await _mvcProcessor.Process(request, inputContext);

                _logger.LogInformation("Processed the request");

                return(new JsonResult(result));
            }
            catch (RequestException e)
            {
                return(_mvcRequestExceptionHandlerService.HandleRequestException(e, this));
            }
            catch (Exception e)
            {
                return(_mvcRequestExceptionHandlerService.HandleException(e, this));
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Post()
        {
            _logger.LogInformation("Received a file upload request");

            // Chack if there are some files
            if (HttpContext.Request.Form.Files.Count == 0)
            {
                _logger.LogWarning("The received file upload didnt contain any files!");
                return(BadRequest(_textService.Error_NoFilesReceived));
            }

            // Check if there is exactly one file
            if (HttpContext.Request.Form.Files.Count > 1)
            {
                _logger.LogWarning("The received file upload contains too many files (FilesCount)!", HttpContext.Request.Form.Files.Count);
                return(BadRequest(_textService.Error_TooManyFiles));
            }

            // Check if the form contains the field 'Data'
            if (!HttpContext.Request.Form.ContainsKey("Data"))
            {
                _logger.LogWarning("Tried to upload a file without filling in the data for server");
                return(BadRequest(_textService.Error_DataFieldIsMissing));
            }

            try
            {
                // Get the file
                var file = HttpContext.Request.Form.Files.First();

                // Prepare the file to be served to a server action
                _uploadFileService.RegisterFile(file);

                // Get the data for server
                var serializedDataForServer = HttpContext.Request.Form["Data"].First();

                // Deserialize the data for server
                _logger.LogInformation("Deserializing the data for server");
                var dataForServer = JsonConvert.DeserializeObject <DataForServer>(serializedDataForServer);

                // Check if serialization succeeded
                if (dataForServer == null)
                {
                    _logger.LogWarning("Could not deserialize the data for server");
                    return(BadRequest(_textService.Error_CouldNotDeserializeRequest));
                }

                // Deserialize the request
                _logger.LogInformation("Deserializing the request");
                var request = _requestDeserializer.Deserialize(dataForServer.Request, dataForServer.Type);

                // Get the input context
                var inputContext = _inputContextService.CreateFromHttpContext(dataForServer.AccessToken, dataForServer.Language, ClientConstants.AppVersionString, HttpContext);

                // Process the request
                _logger.LogInformation("Processing the upload request");
                var response = await _mvcProcessor.Process(request, inputContext);

                _logger.LogInformation("Successfully processed the upload request");

                return(new JsonResult(response));
            }
            catch (RequestException e)
            {
                return(_mvcRequestExceptionHandlerService.HandleRequestException(e, this));
            }
            catch (Exception e)
            {
                return(_mvcRequestExceptionHandlerService.HandleException(e, this));
            }
        }