public void InitialWeArticleClassName(ref WeArticle weArticle, Guid ClassId)
        {
            var reader       = new StreamReader(WeArticleClassCsvPath());
            var csv          = new CsvReader(reader);
            var _Records     = csv.GetRecords <WeArticleFirstClass>().ToList();
            var _SecondClass = _Records.Where(p => p.Id == ClassId).FirstOrDefault();

            weArticle.SecondClassName = _SecondClass.Name;
            weArticle.FirstClassName  = _Records.Where(p => p.Code == _SecondClass.Code.Substring(0, 3)).Select(p => p.Name).FirstOrDefault();
        }
Esempio n. 2
0
        // GET: WeEduHub/WeArticle/Create
        public IActionResult Create(Guid?SetId)
        {
            if (SetId == null)
            {
                return(NotFound());
            }

            var _WeArticle = new WeArticle
            {
                SetId                   = SetId ?? Guid.Empty,
                ClassId                 = _weEduHubArticleClassHelper.GetAllSecondClasses().Select(p => p.Id).FirstOrDefault(),
                IsCommentBeAllowed      = false,
                IsNegativeSignBeAllowed = false,
                IsPositiveSignBeAllowed = false
            };

            return(View(_WeArticle));
        }
Esempio n. 3
0
        public async Task <IActionResult> Edit(Guid id, [Bind("Id,SetId,Title,WeArticleFormFile,ClassId,CustomClass,Complex,Comment,IsCommentBeAllowed,IsNegativeSignBeAllowed,IsPositiveSignBeAllowed")] WeArticle weArticle)
        {
            if (id != weArticle.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var _UserId = _userManager.GetUserAsync(User).GetAwaiter().GetResult().Id;

                    var _WeArticle = await _context.WeArticle.Where(p => p.Id == weArticle.Id && p.AuthorUserId == _UserId).FirstOrDefaultAsync();

                    if (weArticle.WeArticleFormFile != null)
                    {
                        var _FormFileBytes              = _funFilesHelper.GetFormFileBytes(weArticle.WeArticleFormFile);
                        var _FormFileSHA256HexString    = _funFilesHelper.SHA256BytesToHexString(SHA256.Create().ComputeHash(_FormFileBytes));
                        var _WeEduHubFilesDirectoryInfo = new DirectoryInfo(_weEduHubHelper.WeEduHubFilesDirectoryPath(_webHostEnv));

                        if (
                            _WeEduHubFilesDirectoryInfo.GetFiles().ToList().Any(p => p.Name == _FormFileSHA256HexString) &&
                            !await _context.WeArticleFile.AnyAsync(p => p.SHA256HexString == _FormFileSHA256HexString && p.UserId == _UserId)
                            )
                        {
                            return(BadRequest());
                        }

                        if (!IsFileLengthValid(weArticle.WeArticleFormFile))
                        {
                            return(BadRequest());
                        }

                        var _WeArticleFile = new WeArticleFile {
                            DOUploading     = DateTimeOffset.Now,
                            Id              = Guid.NewGuid(),
                            Name            = System.IO.Path.GetFileName(weArticle.WeArticleFormFile.FileName),
                            UserId          = _UserId,
                            SHA256HexString = _FormFileSHA256HexString
                        };

                        await System.IO.File.WriteAllBytesAsync(
                            _weEduHubHelper.WeEduHubFilesDirectoryPath(_webHostEnv) + "/" + _FormFileSHA256HexString,
                            _FormFileBytes
                            );

                        await _context.WeArticleFile.AddAsync(_WeArticleFile);

                        _WeArticle.WeArticleContentFileId = _WeArticleFile.Id;
                    }


                    _WeArticle.Title                   = weArticle.Title;
                    _WeArticle.Complex                 = weArticle.Complex;
                    _WeArticle.ClassId                 = weArticle.ClassId;
                    _WeArticle.CustomClass             = weArticle.CustomClass;
                    _WeArticle.Comment                 = weArticle.Comment;
                    _WeArticle.IsCommentBeAllowed      = weArticle.IsCommentBeAllowed;
                    _WeArticle.IsPositiveSignBeAllowed = weArticle.IsPositiveSignBeAllowed;
                    _WeArticle.IsNegativeSignBeAllowed = weArticle.IsNegativeSignBeAllowed;

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!WeArticleExists(weArticle.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(Content(Url.Action(nameof(Index), new{ weArticle.SetId }))); //  RedirectToAction(nameof(Index), new{weArticle.SetId});
            }
            return(View(weArticle));
        }
Esempio n. 4
0
        public async Task <IActionResult> Create([Bind("SetId,Title,WeArticleFormFile,ClassId,CustomClass,Complex,Comment,IsCommentBeAllowed,IsNegativeSignBeAllowed,IsPositiveSignBeAllowed")] WeArticle weArticle)
        {
            if (ModelState.IsValid)
            {
                var _UserId = _userManager.GetUserAsync(User).GetAwaiter().GetResult().Id;

                if (
                    !await _context.WeArticleSet.AnyAsync(p => p.UserId == _UserId && p.Id == weArticle.SetId) ||
                    weArticle.WeArticleFormFile == null
                    )
                {
                    return(NotFound());
                }

                if (
                    !weArticle.WeArticleFormFile.ContentType.StartsWith("video/") &&
                    !weArticle.WeArticleFormFile.FileName.EndsWith(".pdf")
                    )
                {
                    return(BadRequest());
                }

                if (!IsFileLengthValid(weArticle.WeArticleFormFile))
                {
                    return(BadRequest());
                }


                var _FormFileBytes           = _funFilesHelper.GetFormFileBytes(weArticle.WeArticleFormFile);
                var _FormFileSHA256HexString = _funFilesHelper.SHA256BytesToHexString(
                    SHA256.Create().ComputeHash(_FormFileBytes)
                    );

                var WeEduHubFilesDirectoryInfo = new DirectoryInfo(_weEduHubHelper.WeEduHubFilesDirectoryPath(_webHostEnv));

                if (WeEduHubFilesDirectoryInfo.GetFiles().ToList().Any(p => p.Name == _FormFileSHA256HexString))
                {
                    Console.WriteLine(_FormFileSHA256HexString);
                    return(NotFound());
                }


                weArticle.Id           = Guid.NewGuid();
                weArticle.AuthorUserId = _UserId;

                var _WeArticleFile = new WeArticleFile {
                    DOUploading     = DateTimeOffset.Now,
                    Id              = Guid.NewGuid(),
                    Name            = System.IO.Path.GetFileName(weArticle.WeArticleFormFile.FileName),
                    UserId          = _UserId,
                    SHA256HexString = _FormFileSHA256HexString
                };

                weArticle.WeArticleContentFileId = _WeArticleFile.Id;

                weArticle.DOPublishing = DateTimeOffset.Now;

                await _context.WeArticle.AddAsync(weArticle);

                await _context.WeArticleFile.AddAsync(_WeArticleFile);

                await System.IO.File.WriteAllBytesAsync(
                    _weEduHubHelper.WeEduHubFilesDirectoryPath(_webHostEnv) + "/" + _FormFileSHA256HexString,
                    _FormFileBytes
                    );

                await _context.SaveChangesAsync();

                return(Content(Url.Action(nameof(Index), new{ weArticle.SetId }))); //  RedirectToAction(nameof(Index),new{weArticle.SetId});
            }
            return(View(weArticle));
        }