Exemple #1
0
        public async Task AddAsyncInterests(ProjectImageData model, ModelStateDictionary modelState, string userName)
        {
            var user = await _userManager.FindByNameAsync(userName);

            var trustedFileNameForDisplay     = string.Empty;
            var streamedFileImageContent      = new byte[0];
            var untrustedFileNameForStorage   = string.Empty;
            var trustedFilePathStorage        = string.Empty;
            var trustedFileNameForFileStorage = string.Empty;

            var test = string.Empty;

            streamedFileImageContent =
                await FileHelpers.ProcessFormFile <ProjectFormData>(
                    model.File, modelState, _permittedExtentions,
                    _fileSizeLimit);

            if (!modelState.IsValid)
            {
                test = "ModelState is Invalid";
            }

            untrustedFileNameForStorage = model.File.FileName;
            // Don't trust the file name sent by the client. To display
            // the file name, HTML-encode the value.
            trustedFileNameForDisplay = WebUtility.HtmlEncode(
                model.File.FileName);

            // Bind form data to the model
            var keyPathFolder     = FilePathUrlHelper.GenerateKeyPath(user.Id);
            var generateKeyFolder = GenerateSecurity.Encode(user.Id);

            // Path Location & Directory Check
            trustedFilePathStorage = String.Format("{0}\\{1}\\{2}\\{3}",
                                                   _targetFolderPath,
                                                   keyPathFolder,
                                                   generateKeyFolder,
                                                   Path.GetRandomFileName());

            Directory.CreateDirectory(trustedFilePathStorage);

            using (var fileStream = new FileStream(Path.Combine(trustedFilePathStorage, untrustedFileNameForStorage),
                                                   FileMode.Create, FileAccess.Write))
            {
                await model.File.CopyToAsync(fileStream);

                fileStream.Close();
            }

            ClamProjectInterestsImageDisplay result = new ClamProjectInterestsImageDisplay()
            {
                Title         = Path.GetFileNameWithoutExtension(model.File.FileName),
                ImageLocation = Path.Combine(trustedFilePathStorage, untrustedFileNameForStorage),
                Status        = bool.Parse(model.Status),
                UserId        = user.Id,
                LastModified  = DateTime.Now,
                DateCreated   = DateTime.Now
            };

            await _context.AddAsync(result);

            await _context.SaveChangesAsync();
        }