示例#1
0
        public ActionResult PostAudio([FromForm] IFormFile soundFile, [FromForm] int sekaniWordId, [FromForm] string notes)
        {
            if (soundFile.Length > 505000)
            {
                return(BadRequest("The file exceeds maximum allowed size."));
            }

            byte[] soundBytes = new byte[0];
            if (soundFile.Length > 0)
            {
                using (var ms = new MemoryStream())
                {
                    soundFile.CopyTo(ms);
                    soundBytes = ms.ToArray();
                }
            }

            SekaniWordAudio swa = new SekaniWordAudio()
            {
                SekaniWordId = sekaniWordId,
                Content      = soundBytes,
                Notes        = notes,
                Format       = "NA",
                UpdateTime   = DateTime.Now
            };

            this._unitOfWork.SekaniWordAudios.Add(swa);
            _unitOfWork.Complete();
            return(Ok());
        }
        public void ImportData()
        {
            var createdFiles = Directory.GetFiles(_path, "*.*", SearchOption.AllDirectories);

            var xmlFile = createdFiles.SingleOrDefault(x => x.Contains(@"\AppData.xml"));

            if (xmlFile == null)
            {
                throw new Exception("Xml file not found! please Upload xml file and try again!");
            }

            XmlSerializer serializer = new XmlSerializer(typeof(Dictionary));

            using (FileStream fileStream = new FileStream(xmlFile, FileMode.Open))
            {
                var result = (Dictionary)serializer.Deserialize(fileStream);

                var allFiles = Directory.GetFiles(_path, "*.*", SearchOption.AllDirectories);

                AddCategories(result);
                var sekaniRootList = _unitOfWork.SekaniRoots.GetAll().ToList().Select(a => a.Id);
                var persistRoots   = result.Roots.Where(a => !sekaniRootList.Contains(a.Id));
                var categoryList   = _unitOfWork.SekaniCategories.GetAll().ToList();
                AddForms(result);
                var sekaniFormList  = _unitOfWork.SekaniForms.GetAll().ToList();
                var sekaniLevelList = _unitOfWork.SekaniLevels.GetAll().ToList();
                AddTopics(result);
                var sekaniTopicsList = _unitOfWork.Topics.GetAll().ToList();
                AddEnglishWords(result);
                var englishWordList = _unitOfWork.EnglishWords.GetAll().ToList();

                List <SekaniRoot> sekaniRoots = new List <SekaniRoot>();
                Parallel.ForEach(persistRoots, root =>
                {
                    var sekaniRoot = new SekaniRoot
                    {
                        Id               = root.Id,
                        IsNull           = root.IsNull,
                        RootWord         = root.Rootword,
                        UpdateTime       = DateTime.Now,
                        SekaniCategoryId = categoryList.Find(x => x.Title == root.Category).Id,
                        SekaniFormId     = sekaniFormList.Find(x => x.Title == root.Form).Id,
                        SekaniLevelId    = sekaniLevelList.Find(x => x.Title == root.Level).Id
                    };



                    if (!string.IsNullOrEmpty(root.Image))
                    {
                        Regex regex = new Regex(@"^[\w,\s-]+\.[\w]{3}$");
                        Match match = regex.Match(root.Image);
                        if (match.Success)
                        {
                            var img = allFiles.SingleOrDefault(a => a.Contains("\\" + root.Image));
                            if (img != null)
                            {
                                var theFile = System.IO.File.ReadAllBytes(img);
                                sekaniRoot.SekaniRootImages.Add(new SekaniRootImage()
                                {
                                    Notes      = root.Image,
                                    Format     = root.Image.Split(".")[1],
                                    UpdateTime = DateTime.Now,
                                    Content    = theFile
                                });
                            }
                        }
                    }

                    root.EnglishWords.EnglishWord.Distinct().ToList().ForEach(ew =>
                    {
                        var word = englishWordList.Find(x => x.Word == ew);
                        if (word != null)
                        {
                            sekaniRoot.SekaniRootsEnglishWords.Add(new SekaniRootEnglishWord {
                                EnglishWordId = word.Id, UpdateTime = DateTime.Now
                            });
                        }
                    });

                    root.Topics.Title.ForEach(t =>
                    {
                        var topic = sekaniTopicsList.Find(x => x.Title == t);

                        sekaniRoot.SekaniRootsTopics.Add(new SekaniRootTopic {
                            TopicId = topic.Id, UpdateTime = DateTime.Now
                        });
                    });

                    root.Inflectedforms.Elements.ForEach(sekani =>
                    {
                        var sw = new SekaniWord
                        {
                            SekaniRootId = root.Id,
                            Word         = sekani.Inflectedform,
                            UpdateTime   = DateTime.Now
                        };
                        sekani.Attributes?.AttributeElement.ForEach(attr =>
                        {
                            sw.SekaniWordAttributes.Add(new SekaniWordAttribute {
                                Key = attr.Key, Value = attr.Value, UpdateTime = DateTime.Now
                            });
                        });
                        sekani.Examples.ForEach(ex =>
                        {
                            var example = new SekaniWordExample
                            {
                                English    = ex.ExampleElement.English,
                                Sekani     = ex.ExampleElement.Sekani,
                                UpdateTime = DateTime.Now
                            };

                            if (!string.IsNullOrEmpty(ex.ExampleElement.Audio))
                            {
                                Regex regex = new Regex(@"^[\w,\s-]+\.[\w]{3}$");
                                Match match = regex.Match(ex.ExampleElement.Audio);
                                if (match.Success)
                                {
                                    var aud = allFiles.SingleOrDefault(a => a.Contains("\\" + ex.ExampleElement.Audio));
                                    if (aud != null)
                                    {
                                        var theFile = System.IO.File.ReadAllBytes(aud);
                                        example.SekaniWordExampleAudios.Add(new SekaniWordExampleAudio
                                        {
                                            Format     = aud.Split(".")[1],
                                            Notes      = ex.ExampleElement.Audio,
                                            Content    = theFile,
                                            UpdateTime = DateTime.Now
                                        });
                                    }
                                }
                            }
                            sw.SekaniWordExamples.Add(example);
                        });
                        sekani.Audios?.ForEach(au =>
                        {
                            if (!string.IsNullOrEmpty(au))
                            {
                                Regex regex = new Regex(@"^[\w,\s-]+\.[\w]{3}$");
                                Match match = regex.Match(au);
                                if (match.Success)
                                {
                                    var aud = allFiles.SingleOrDefault(a => a.Contains("\\" + au));
                                    if (aud != null)
                                    {
                                        var theFile = System.IO.File.ReadAllBytes(aud);
                                        var audio   = new SekaniWordAudio
                                        {
                                            Format     = au.Split(".")[1],
                                            Content    = theFile,
                                            UpdateTime = DateTime.Now,
                                            Notes      = au
                                        };
                                        sw.SekaniWordAudios.Add(audio);
                                    }
                                }
                            }
                        });
                        sekaniRoot.SekaniWords.Add(sw);
                    });
                    lock (sekaniRoot)
                    {
                        sekaniRoots.Add(sekaniRoot);
                    }
                });

                _unitOfWork.SekaniRoots.AddRange(sekaniRoots);
                _unitOfWork.Complete();
            }
        }