public async Task <IActionResult> OnGetDownloadSqliteDbAsync(int id)
        {
            using (HttpClient secureClient = new HttpClient())
            {
                LastResult = "";
                if (false == await PreparePoet())
                {
                    return(BadRequest(LastResult));
                }

                await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response);

                var content = await secureClient.GetStreamAsync($"{APIRoot.Url}/api/ganjoor/sqlite/export/{id}");

                var contentType = "Application/octet-stream";
                var fileName    = $"{GPersianTextSync.Farglisize(Poet.Name)}.gdb";
                return(File(content, contentType, fileName));
            }
        }
Exemple #2
0
        private async Task <string> _ImportSQLiteCatChildren(RMuseumDbContext context, IDbConnection sqlite, int poetId, int sqliteParentCatId, GanjoorCat parentCat, string parentFullTitle, LongRunningJobProgressServiceEF jobProgressServiceEF, RLongRunningJobStatus job, int parentPagId)
        {
            try
            {
                string catHtmlText = "";
                foreach (var cat in await sqlite.QueryAsync($"SELECT * FROM cat WHERE parent_id = {sqliteParentCatId} ORDER BY id"))
                {
                    await jobProgressServiceEF.UpdateJob(job.Id, 0, $"Importing - {cat.text}");

                    var poetCatId = 1 + await context.GanjoorCategories.MaxAsync(c => c.Id);

                    string catTitle = cat.text;

                    string url = GPersianTextSync.Farglisize(catTitle);
                    if (catTitle.IndexOf('|') != -1)
                    {
                        string[] catParts = catTitle.Split('|', StringSplitOptions.RemoveEmptyEntries);
                        if (catParts.Length == 2)
                        {
                            catTitle = catParts[0].Trim();
                            url      = catParts[1].Trim();
                        }
                    }
                    else
                    {
                        switch (catTitle)
                        {
                        case "دیوان اشعار":
                            url = "divan";
                            break;

                        case "قصاید":
                        case "قصائد":
                        case "قصیده":
                        case "قصیده ها":
                            url = "ghaside";
                            break;

                        case "غزلیات":
                        case "غزل":
                        case "غزل ها":
                            url = "ghazal";
                            break;

                        case "قطعات":
                        case "مقطعات":
                        case "قطعه":
                            url = "ghete";
                            break;

                        case "مثنویات":
                        case "مثنوی":
                        case "مثنوی ها":
                            url = "masnavi";
                            break;

                        case "ترکیبات":
                        case "ترکیب بند":
                            url = "tarkib";
                            break;

                        case "ترجیعات":
                        case "ترجیع بند":
                            url = "tarjee";
                            break;

                        case "مسمطات":
                        case "مسمط":
                            url = "mosammat";
                            break;

                        case "مخمسات":
                        case "مخمس":
                            url = "mokhammas";
                            break;

                        case "رباعیات":
                        case "رباعی":
                        case "رباعی ها":
                            url = "robaee";
                            break;

                        case "ملمعات":
                        case "ملمع":
                            url = "molamma";
                            break;

                        case "هجویات":
                        case "هجو":
                            url = "hajv";
                            break;

                        case "هزلیات":
                        case "هزل":
                            url = "hazl";
                            break;

                        case "مراثی":
                        case "مرثیه":
                        case "رثا":
                        case "مرثیه ها":
                            url = "marsie";
                            break;

                        case "مفردات":
                            url = "mofradat";
                            break;

                        case "ملحقات":
                            url = "molhaghat";
                            break;

                        case "اشعار عربی":
                            url = "arabi";
                            break;

                        case "ماده تاریخ‌ها":
                        case "ماده تاریخها":
                        case "ماده تاریخ":
                            url = "tarikh";
                            break;

                        case "معمیات":
                            url = "moammiyat";
                            break;

                        case "چیستان":
                            url = "chistan";
                            break;

                        case "لغز":
                        case "لغزها":
                            url = "loghaz";
                            break;
                        }
                    }

                    GanjoorCat dbCat = new GanjoorCat()
                    {
                        Id                   = poetCatId,
                        PoetId               = poetId,
                        Title                = catTitle,
                        UrlSlug              = url,
                        FullUrl              = $"{parentCat.FullUrl}/{url}",
                        ParentId             = parentCat.Id,
                        TableOfContentsStyle = GanjoorTOC.Analyse,
                        Published            = true,
                    };
                    context.GanjoorCategories.Add(dbCat);

                    var maxPageId = await context.GanjoorPages.MaxAsync(p => p.Id);

                    if (await context.GanjoorPoems.MaxAsync(p => p.Id) > maxPageId)
                    {
                        maxPageId = await context.GanjoorPoems.MaxAsync(p => p.Id);
                    }

                    var catPageId = 1 + maxPageId;
                    while (await context.GanjoorPoems.Where(p => p.Id == catPageId).AnyAsync())
                    {
                        catPageId++;
                    }

                    GanjoorPage dbPageCat = new GanjoorPage()
                    {
                        Id = catPageId,
                        GanjoorPageType = GanjoorPageType.CatPage,
                        Published       = false,
                        PageOrder       = -1,
                        Title           = dbCat.Title,
                        FullTitle       = $"{parentFullTitle} » {dbCat.Title}",
                        UrlSlug         = dbCat.UrlSlug,
                        FullUrl         = dbCat.FullUrl,
                        HtmlText        = "",
                        PoetId          = poetId,
                        CatId           = poetCatId,
                        PostDate        = DateTime.Now,
                        ParentId        = parentPagId
                    };

                    context.GanjoorPages.Add(dbPageCat);

                    await context.SaveChangesAsync();

                    catHtmlText += $"<p><a href=\"{dbCat.FullUrl}\">{dbCat.Title}</a></p>{Environment.NewLine}";

                    var resChild = await _ImportSQLiteCatChildren(context, sqlite, poetId, (int)cat.id, dbCat, $"{parentFullTitle} » {dbCat.Title}", jobProgressServiceEF, job, dbPageCat.Id);

                    if (!string.IsNullOrEmpty(resChild))
                    {
                        return(resChild);
                    }
                }
                var maxPoemId = await context.GanjoorPoems.MaxAsync(p => p.Id);

                if (await context.GanjoorPages.MaxAsync(p => p.Id) > maxPoemId)
                {
                    maxPoemId = await context.GanjoorPages.MaxAsync(p => p.Id);
                }
                var poemId = 1 + maxPoemId;

                int poemNumber = 0;
                foreach (var poem in await sqlite.QueryAsync($"SELECT * FROM poem WHERE cat_id = {sqliteParentCatId} ORDER BY id"))
                {
                    poemNumber++;
                    await jobProgressServiceEF.UpdateJob(job.Id, poemNumber, "", false);

                    string title   = poem.title;
                    string urlSlug = $"sh{poemNumber}";
                    if (title.IndexOf('|') != -1)
                    {
                        string[] titleParts = title.Split('|', StringSplitOptions.RemoveEmptyEntries);
                        if (titleParts.Length == 2)
                        {
                            title   = titleParts[0].Trim();
                            urlSlug = titleParts[1].Trim();
                        }
                    }


                    GanjoorPoem dbPoem = new GanjoorPoem()
                    {
                        Id        = poemId,
                        CatId     = parentCat.Id,
                        Title     = title,
                        UrlSlug   = urlSlug,
                        FullTitle = $"{parentFullTitle} » {title}",
                        FullUrl   = $"{parentCat.FullUrl}/{urlSlug}",
                        Published = true,
                    };

                    List <GanjoorVerse> poemVerses = new List <GanjoorVerse>();
                    foreach (var verse in await sqlite.QueryAsync($"SELECT * FROM verse WHERE poem_id = {poem.id} ORDER BY vorder"))
                    {
                        int          vOrder   = int.Parse(verse.vorder.ToString());
                        int          position = int.Parse(verse.position.ToString());
                        string       text     = verse.text;
                        GanjoorVerse dbVerse  = new GanjoorVerse()
                        {
                            PoemId        = poemId,
                            VOrder        = vOrder,
                            VersePosition = (VersePosition)position,
                            Text          = text.Replace("ـ", "").Replace("  ", " ").ApplyCorrectYeKe().Trim()
                        };
                        poemVerses.Add(dbVerse);
                    }

                    if (poemVerses.Count == 0)
                    {
                        poemNumber--;
                        continue;
                    }

                    dbPoem.PlainText = PreparePlainText(poemVerses);
                    dbPoem.HtmlText  = PrepareHtmlText(poemVerses);

                    context.GanjoorPoems.Add(dbPoem);
                    await context.SaveChangesAsync();

                    foreach (var dbVerse in poemVerses)
                    {
                        context.GanjoorVerses.Add(dbVerse);
                        await context.SaveChangesAsync();//id set should be in order
                    }

                    await _FillPoemCoupletIndices(context, poemId);

                    try
                    {
                        var poemRhymeLettersRes = LanguageUtils.FindRhyme(poemVerses);
                        if (!string.IsNullOrEmpty(poemRhymeLettersRes.Rhyme))
                        {
                            dbPoem.RhymeLetters = poemRhymeLettersRes.Rhyme;
                            context.GanjoorPoems.Update(dbPoem);
                        }
                    }
                    catch
                    {
                    }



                    GanjoorPage dbPoemPage = new GanjoorPage()
                    {
                        Id = poemId,
                        GanjoorPageType = GanjoorPageType.PoemPage,
                        Published       = false,
                        PageOrder       = -1,
                        Title           = dbPoem.Title,
                        FullTitle       = dbPoem.FullTitle,
                        UrlSlug         = dbPoem.UrlSlug,
                        FullUrl         = dbPoem.FullUrl,
                        HtmlText        = dbPoem.HtmlText,
                        PoetId          = poetId,
                        CatId           = parentCat.Id,
                        PoemId          = poemId,
                        PostDate        = DateTime.Now,
                        ParentId        = parentPagId
                    };

                    context.GanjoorPages.Add(dbPoemPage);
                    await context.SaveChangesAsync();

                    catHtmlText += $"<p><a href=\"{dbPoemPage.FullUrl}\">{dbPoemPage.Title}</a></p>{Environment.NewLine}";

                    poemId++;
                }

                if (!string.IsNullOrEmpty(catHtmlText))
                {
                    var parentCatPage = await context.GanjoorPages.Where(p => p.FullUrl == parentCat.FullUrl).SingleAsync();

                    parentCatPage.HtmlText += catHtmlText;
                    context.GanjoorPages.Update(parentCatPage);
                }

                await context.SaveChangesAsync();
            }
            catch (Exception exp)
            {
                return(exp.ToString());
            }
            return("");
        }