Exemple #1
0
        public async Task UpdateTVShow(Guid id, SectionTVShowSubCategory model)
        {
            var trustedFileNameForDisplay   = string.Empty;
            var trustedFilePathForStorage   = string.Empty;
            var streamedFilePhysicalContent = new byte[0];

            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("Genre",
                                         $"The request couldn't be processed (Error 1).");
                // Log error
            }

            if (model.FormFile != null)
            {
                streamedFilePhysicalContent =
                    await FileHelpers.ProcessFormFile <StreamFileUploadDatabase>(
                        model.FormFile, ModelState, _permittedExtentions,
                        _fileSizeLimit);
            }

            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("Genre",
                                         $"The request couldn't be processed (Error 1).");
                // Log error
            }

            var result = await _context.ClamSectionTVShowSubCategories.FindAsync(id);

            if (Directory.Exists(FilePathUrlHelper.GetFileDirectoryPath(result.ItemPath)))
            {
                System.IO.File.Delete(result.ItemPath);
            }

            trustedFilePathForStorage = String.Format("{0}\\{1}\\{2}",
                                                      _targetImagePath,
                                                      Guid.NewGuid().ToString(),
                                                      Path.GetRandomFileName());
            trustedFileNameForDisplay = String.Format("{0}_{1}",
                                                      Guid.NewGuid(),
                                                      WebUtility.HtmlEncode(model.FormFile.FileName));

            Directory.CreateDirectory(trustedFilePathForStorage);
            using (var targetStream = System.IO.File.Create(
                       Path.Combine(trustedFilePathForStorage, trustedFileNameForDisplay)))
            {
                await targetStream.WriteAsync(streamedFilePhysicalContent);
            }


            var entity = _context.Set <ClamSectionTVShowSubCategory>().Find(id);

            _context.Entry(entity).Entity.TVShowTitle             = model.TVShowTitle;
            _context.Entry(entity).Entity.TVShowSeasonNumberTotal = model.TVShowSeasonNumberTotal;
            _context.Entry(entity).Entity.ItemPath     = Path.Combine(trustedFilePathForStorage, trustedFileNameForDisplay);
            _context.Entry(entity).Entity.LastModified = DateTime.Now;
            _context.Entry(entity).State = EntityState.Modified;
            _context.Update(entity);
            Task.WaitAll(_context.SaveChangesAsync());
        }