Exemplo n.º 1
0
        public FileDataDTO GetFileDataDTO()
        {
            FileDataDTO dto = new FileDataDTO();

            dto.ContentType = this.ContentType;
            dto.Data        = this.Data;
            dto.Id          = this.Id;
            dto.Length      = this.Length;
            dto.Name        = this.Name;
            return(dto);
        }
        public async Task <IActionResult> UploadAsync([FromForm] FileDataDTO fileData)
        {
            try
            {
                int maxSize = Convert.ToInt32(config["AppDetails:AllowedFileSizeInMb"]) * 1000000;

                if (fileData.File.Length > maxSize)
                {
                    return(BadRequest("File is too big"));
                }

                string[] alloweFileTypes = config["AppDetails:AllowedFileTypes"].Split(",");

                string extension = Path.GetExtension(fileData.File.FileName);

                if (!alloweFileTypes.Contains(extension))
                {
                    return(BadRequest("File extension is not allowed"));
                }

                string path = await fileService.SaveOnDriveAsync(fileData.File);

                if (path == null)
                {
                    // Such file already exists
                    return(BadRequest("File with such name already exists"));
                }

                var fileInstance = await fileRepository.AddAsync(new FileInstance()
                {
                    Name            = fileData.File.FileName,
                    SizeInMb        = (double)fileData.File.Length / 1000000,
                    Path            = path,
                    UploadDate      = DateTime.Now,
                    UserWhoUploaded = fileData.UserWhoUploaded
                });

                return(Ok(fileInstance));
            }
            catch (Exception e)
            {
                // place to add logging
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
Exemplo n.º 3
0
        public async Task <BaseHttpActionResult> UpdateRuleStyle()
        {
            var ruleJson = HttpContext.Current.Request.Form["rule"];
            var baseUrl  = HttpContext.Current.Request.Form["base_url"];
            var entityId = HttpContext.Current.Request.Form["entity_id"];

            if (string.IsNullOrEmpty(entityId))
            {
                entityId = null;
            }
            if (string.IsNullOrEmpty(ruleJson))
            {
                return(ErrorHttpActionResult.GenerateBadRequest("rule form data is required"));
            }
            RuleDTO rule = JsonConvert.DeserializeObject <RuleDTO>(ruleJson);

            // Get the uploaded files from the Files collection , add data to the fileinfo
            IList <FileDataDTO> fileDtos = new List <FileDataDTO>();

            foreach (var cssProperty in HttpContext.Current.Request.Files.AllKeys)
            {
                var httpPostedFile = HttpContext.Current.Request.Files[cssProperty];

                int    length      = httpPostedFile.ContentLength;
                string contentType = httpPostedFile.ContentType;
                string name        = Path.GetFileName(httpPostedFile.FileName);

                var fileData = new FileDataDTO(cssProperty);
                fileData.Data        = new byte[length];
                fileData.Name        = name;
                fileData.Length      = length;
                fileData.ContentType = contentType;
                httpPostedFile.InputStream.Read(fileData.Data, 0, length);
                fileDtos.Add(fileData);
            }

            var updateRule = await _repo.UpdateRuleStyle(rule, fileDtos, UserId, entityId);

            return(new SuccessHttpActionResult(updateRule.Format(baseUrl)));
        }