コード例 #1
0
        public async Task <IActionResult> AddProject(IFormFile formFile)
        {
            Request.Form.TryGetValue("project", out StringValues res);
            ProjectDTO project = JsonConvert.DeserializeObject <ProjectDTO>(res);

            if (Request.Form.Files.Count != 0)
            {
                IFormFile file = Request.Form.Files[0];
                byte[]    byteArr;
                using (var ms = new MemoryStream())
                {
                    file.CopyTo(ms);
                    await file.CopyToAsync(ms);

                    byteArr = ms.ToArray();
                }

                project.ImageUrl = await fileStorageProvider.UploadFileAsync(byteArr, FileType.Photo, Path.GetExtension(file.FileName));
            }

            var langIds = project.ProjectLanguageses.Select(l => l.Id);
            var entity  = await service.PostAsync(project);

            entity = await service.AddLanguagesToProject(entity.Id, langIds.ToArray());

            return(entity == null?StatusCode(409) as IActionResult
                   : Created($"{Request?.Scheme}://{Request?.Host}{Request?.Path}{entity.Id}",
                             entity));
        }
コード例 #2
0
        public async Task <IActionResult> AddCropedPhoto(IFormFile formFile)
        {
            var currentUser = mapper.Map <UserProfileDTO>(await _currentUser.GetCurrentUserProfile());

            if (currentUser == null)
            {
                return(Unauthorized());
            }

            if (Request.Form.Files.Count != 0)
            {
                IFormFile photo = Request.Form.Files[0];
                byte[]    byteArr;
                using (var ms = new MemoryStream())
                {
                    photo.CopyTo(ms);
                    await photo.CopyToAsync(ms);

                    byteArr = ms.ToArray();
                }

                currentUser.AvatarUrl = await fileStorageProvider.UploadFileAsync(byteArr, FileType.Photo, Path.GetExtension(photo.FileName));

                var result = await service.PutUserBool(currentUser);

                return(currentUser == null
                    ? StatusCode(400) as IActionResult
                    : Ok(currentUser));
            }

            return(BadRequest());
        }
コード例 #3
0
        private static async Task <string> GenerateThumbinal(IFileStorageProvider storageProvider, int width, int height, IImageFormat imageFormat, Image image)
        {
            string thumbinalPath = string.Empty;
            double ratio         = width * 1.0 / image.Width <= height * 1.0 / image.Height ?
                                   width * 1.0 / image.Width : height * 1.0 / image.Height;

            var encoder = image.GetConfiguration().ImageFormatsManager.FindEncoder(imageFormat);

            if (encoder is null)
            {
                var sb = new StringBuilder();
                sb.AppendLine($"No encoder was found for image format '{imageFormat.Name}'. Registered encoders include:");
                throw new NotSupportedException(sb.ToString());
            }

            using (MemoryStream ms = new MemoryStream())
            {
                image.Mutate(x => x
                             .Resize((int)(image.Width * ratio), (int)(image.Height * ratio)));

                image.Save(ms, encoder);
                thumbinalPath = await storageProvider.UploadFileAsync(ms.ToArray());
            }

            return(thumbinalPath);
        }
コード例 #4
0
        private async Task <FileStorageInfo> SaveFileContent(Stream stream,
                                                             string uniqueFileName,
                                                             string format,
                                                             bool createThumbinal,
                                                             string fileFriendlyName)
        {
            if (defaultProvider == null)
            {
                throw new ArgumentException(nameof(defaultProvider) + " is null");
            }

            string fileFullName = await defaultProvider.UploadFileAsync(stream);

            string thumbinalFileName = string.Empty;

            if (createThumbinal)
            {
                using (var fileStream = defaultProvider.OpenFileStream(fileFullName))
                {
                    thumbinalFileName = await SimpleFileThumbinalGenerator.CreateThumbnail(defaultProvider,
                                                                                           format,
                                                                                           thumbinalConfig.Width,
                                                                                           thumbinalConfig.Height,
                                                                                           fileStream);
                }
            }
            string fileCrc      = string.Empty;
            long   streamLength = 0;

            using (var fileStream = defaultProvider.OpenFileStream(fileFullName))
            {
                fileCrc      = CalculateCRC(fileStream);
                streamLength = stream.CanSeek ? stream.Length
                : fileStream.Length;
            }

            return(StoreFileInfo(streamLength,
                                 uniqueFileName,
                                 fileCrc,
                                 fileFullName,
                                 thumbinalFileName,
                                 fileFriendlyName));
        }
コード例 #5
0
        public async Task <IActionResult> AddComplexString(IFormFile formFile)
        {
            Request.Form.TryGetValue("str", out StringValues res);
            ComplexStringDTO complexString = JsonConvert.DeserializeObject <ComplexStringDTO>(res);

            if (Request.Form.Files.Count != 0)
            {
                IFormFile file = Request.Form.Files[0];
                byte[]    byteArr;
                using (var ms = new MemoryStream())
                {
                    await file.CopyToAsync(ms);

                    byteArr = ms.ToArray();
                }

                complexString.PictureLink = await fileStorageProvider.UploadFileAsync(byteArr, FileType.Photo, Path.GetExtension(file.FileName));
            }
            var entity = await dataProvider.AddComplexString(complexString);

            return(entity == null?StatusCode(409) as IActionResult
                   : Ok(entity));
        }