<commit_before>﻿using SubtitleCommunitySystem.Data;
using SubtitleCommunitySystem.Model;
using SubtitleCommunitySystem.Web.Areas.Administration.Models;
using SubtitleCommunitySystem.Web.Controllers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SubtitleCommunitySystem.Web.Areas.Administration.Controllers
{
    public class MoviesController : AdminController
    {
        public MoviesController(IApplicationData data)
            : base(data)
        {

        }

        public ActionResult Index()
        {
            var movies = this.Data.Movies.All().OrderBy(m => m.Name).Select(m => new MovieOutputModel() 
            {  
                BannerUrl = m.BannerUrl,
                Description = m.Description,
                Id = m.Id,
                MainPosterUrl = m.MainPosterUrl,
                Name = m.Name
            });

            return View(movies);
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Movie movie, HttpPostedFileBase poster, HttpPostedFileBase banner)
        {
            if (!ModelState.IsValid)
            {
                return View(movie);
            }
            try
            {
                if (poster != null)
                {
                    movie.MainPosterUrl = UploadFile(poster, "poster", movie);
                }

                if (banner != null)
                {
                    movie.BannerUrl = UploadFile(banner, "banner", movie);
                }
            }
            catch (ArgumentException ex)
            {
                return Content(ex.Message);
            }

            this.Data.Movies.Add(movie);

            this.Data.SaveChanges();

            return RedirectToAction("Index");
        }

        private string UploadFile(HttpPostedFileBase file, string fileName, Movie movie)
        {
            var extention = file.FileName.Substring(file.FileName.LastIndexOf('.'));
            if (!FileConstants.AllowedPictureExtentions.Contains(extention))
            {
                throw new ArgumentException("Incorrect file extention type.");
            }

            var directoryName = "~/Files/" + movie.Name;
            if (string.IsNullOrWhiteSpace(movie.Directory))
            {
                if (Directory.Exists(Server.MapPath("~/Files/" + movie.Name)))
                {
                    var i = 0;
                    while (Directory.Exists(Server.MapPath("~/Files/" + movie.Name + "." + i)))
                    {
                        i++;
                    }

                    directoryName = "~/Files/" + movie.Name + "." + i;
                }
            }
            else
            {
                directoryName = movie.Directory;
            }
            

            movie.Directory = directoryName;
            var mappedDirectoryName = Server.MapPath(directoryName);
            Directory.CreateDirectory(mappedDirectoryName);

            file.SaveAs(mappedDirectoryName + "/" + fileName + extention);

            return directoryName.Substring(1) + "/" + fileName + extention;
        }
    }
}<commit_msg>Anti forgery token<commit_after>﻿using SubtitleCommunitySystem.Data;
using SubtitleCommunitySystem.Model;
using SubtitleCommunitySystem.Web.Areas.Administration.Models;
using SubtitleCommunitySystem.Web.Controllers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SubtitleCommunitySystem.Web.Areas.Administration.Controllers
{
    public class MoviesController : AdminController
    {
        public MoviesController(IApplicationData data)
            : base(data)
        {

        }

        public ActionResult Index()
        {
            var movies = this.Data.Movies.All().OrderBy(m => m.Name).Select(m => new MovieOutputModel() 
            {  
                BannerUrl = m.BannerUrl,
                Description = m.Description,
                Id = m.Id,
                MainPosterUrl = m.MainPosterUrl,
                Name = m.Name
            });

            return View(movies);
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Movie movie, HttpPostedFileBase poster, HttpPostedFileBase banner)
        {
            if (!ModelState.IsValid)
            {
                return View(movie);
            }
            try
            {
                if (poster != null)
                {
                    movie.MainPosterUrl = UploadFile(poster, "poster", movie);
                }

                if (banner != null)
                {
                    movie.BannerUrl = UploadFile(banner, "banner", movie);
                }
            }
            catch (ArgumentException ex)
            {
                return Content(ex.Message);
            }

            this.Data.Movies.Add(movie);

            this.Data.SaveChanges();

            return RedirectToAction("Index");
        }

        private string UploadFile(HttpPostedFileBase file, string fileName, Movie movie)
        {
            var extention = file.FileName.Substring(file.FileName.LastIndexOf('.'));
            if (!FileConstants.AllowedPictureExtentions.Contains(extention))
            {
                throw new ArgumentException("Incorrect file extention type.");
            }

            var directoryName = "~/Files/" + movie.Name;
            if (string.IsNullOrWhiteSpace(movie.Directory))
            {
                if (Directory.Exists(Server.MapPath("~/Files/" + movie.Name)))
                {
                    var i = 0;
                    while (Directory.Exists(Server.MapPath("~/Files/" + movie.Name + "." + i)))
                    {
                        i++;
                    }

                    directoryName = "~/Files/" + movie.Name + "." + i;
                }
            }
            else
            {
                directoryName = movie.Directory;
            }
            

            movie.Directory = directoryName;
            var mappedDirectoryName = Server.MapPath(directoryName);
            Directory.CreateDirectory(mappedDirectoryName);

            file.SaveAs(mappedDirectoryName + "/" + fileName + extention);

            return directoryName.Substring(1) + "/" + fileName + extention;
        }
    }
}
{'fraction_non_alphanumeric': 0.09322158506102043, 'fraction_numerical': 0.0005881488016468167, 'mean_word_length': 1.3382605706428325, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5331025', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1630, 'n_words': 303}