예제 #1
0
        private IVideoData GetVideoDataFromCache(IVideoDataGetter videoDataGetter, string encodedLink)
        {
            string serializedValue = _diskCache.Get(GetMainKey(encodedLink), Encoding.UTF8);

            if (string.IsNullOrEmpty(serializedValue))
            {
                return(null);
            }

            string[] rows     = serializedValue.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            var      mainData = _serializer.Deserialize <Dictionary <string, string> >(rows[0]);

            IVideoData result = videoDataGetter.CreateFromString(rows[1]);

            if (result != null)
            {
                result.Title    = mainData["title"];
                result.Vid      = mainData["vid"];
                result.HtmlCode = mainData["htmlCode"];
                string imageKey = mainData["imagePath"];
                result.ThumnailImage = _diskCache.Get(imageKey);
            }
            return(result);
        }
예제 #2
0
        public byte[] Generate(long languageId, UserLanguages userLanguages)
        {
            var allMaterialsQuery = new AllMaterialsQuery(languageId, userLanguages, _forbiddenSectionsChecker);
            Dictionary <SectionId, List <Tuple <long, string, DateTime> > > tupleBySections =
                allMaterialsQuery.GetDataBySections();

            string keyFromContent = GetKeyFromContent(tupleBySections);
            string zipKey         = "AllMaterials" + "_" + keyFromContent + "_" + userLanguages.From.ShortName + "_"
                                    + userLanguages.To.ShortName + ".zip";

            byte[] result = _cache.Get(zipKey);
            if (result != null)
            {
                return(result);
            }

            foreach (var pairBySection in tupleBySections)
            {
                SectionId sectionId  = pairBySection.Key;
                string    folderName = allMaterialsQuery.GetHeader(sectionId);

                foreach (var tuple in pairBySection.Value)
                {
                    DocumentationGenerator generator = GetGenerator(allMaterialsQuery, sectionId, tuple.Item1,
                                                                    tuple.Item2);
                    if (generator == null)
                    {
                        //TODO: логировать и отсылать мне на почту
                        continue;
                    }

                    Stream fileContent = generator.Generate();
                    if (fileContent == null)
                    {
                        //TODO: логировать и отсылать мне на почту
                        continue;
                    }

                    string entryName = folderName + "/" + generator.FileName;
                    _zipCompressor.AddFileToArchive(entryName, fileContent);
                }
            }

            result = _zipCompressor.GetArchive();
            WriteToCache(zipKey, result);
            return(result);
        }
        public byte[] Generate(LanguageShortName from, LanguageShortName to, HashSet <long> visualDictionariesIds)
        {
            var           languagesQuery       = new LanguagesQuery(from, to);
            UserLanguages userLanguages        = languagesQuery.GetLanguagesByShortNames(from, to);
            var           representationsQuery = new RepresentationsQuery(userLanguages.From.Id);

            List <RepresentationForUser> allRepresentations = representationsQuery.GetBought(userLanguages,
                                                                                             visualDictionariesIds);

            PdfGenerator commonPdfGenerator = allRepresentations.Count > 1
                                                  ? new PdfGenerator(_fontPath, _domain, "Все визуальные словари")
                                                  : null;

            var zipKey = GetZipKey(allRepresentations, @from, to);
            var result = _cache.Get(zipKey);

            if (result != null)
            {
                return(result);
            }

            var painter          = new Painter();
            var partImageCreator = new PartImageCreator(painter);

            foreach (RepresentationForUser representation in allRepresentations)
            {
                var tableData = new TableData(3, true);
                tableData.AddHeader(string.Empty, "Слово", "Перевод");

                string title = representation.Title;

                /*if (pictureName != "Лицо" /*&& pictureName != "Человек"#1#) {
                 *  continue;
                 * }*/

                var   byteStream  = new MemoryStream(representation.Image);
                Image image       = Image.FromStream(byteStream);
                var   wordsWriter = new WordsWriter(painter, image.Width, image.Height);

                foreach (RepresentationAreaForUser area in representation.Areas)
                {
                    Point leftCorner  = area.LeftUpperCorner;
                    Point rightCorner = area.RightBottomCorner;

                    string partKey = representation.Id + "_part_" + area.Id + "_"
                                     + representation.SortInfo.LastModified.Ticks + "_" + from + "_" + to + ".jpeg";
                    byte[] partImageBytes = _cache.Get(partKey);
                    if (partImageBytes == null)
                    {
                        PartImageData partImageData = partImageCreator.CutImage(leftCorner, rightCorner, image);

                        partImageBytes = ImageToBytes(partImageData.Bitmap);
                        WriteToCache(partKey, partImageBytes);
                    }
                    tableData.AddRow(new[] {
                        TableDataCell.CreateImage(partImageBytes), TableDataCell.CreateText(area.Source.Text),
                        TableDataCell.CreateText(area.Translation.Text)
                    });

                    wordsWriter.AddRectangle(leftCorner, rightCorner, area);
                }

                string fullKey = representation.Id + "_" + representation.SortInfo.LastModified.Ticks + "_" + from + "_" + to + ".jpeg";

                byte[] imageResult = _cache.Get(fullKey);
                if (imageResult == null)
                {
                    Image imageWithSign = wordsWriter.GetImageWithSigns(image);
                    imageResult = ImageToBytes(imageWithSign);

                    WriteToCache(fullKey, imageResult);
                }

                var    pdfFileName = title + ".pdf";
                string pdfKey      = representation.Id + "_" + representation.SortInfo.LastModified.Ticks + "_" + from + "_" + to + "_" + pdfFileName;
                byte[] pdfContent  = _cache.Get(pdfKey);

                if (pdfContent == null)
                {
                    var pdfGenerator = new PdfGenerator(_fontPath, _domain, string.Format(
                                                            "Визуальный словарь на тему {0}",
                                                            title));
                    WriteDataToPdf(pdfGenerator, title, imageResult, tableData);
                    pdfContent = pdfGenerator.GetAsBytes();
                    WriteToCache(pdfKey, pdfContent);
                }
                _zipCompressor.AddFileToArchive(pdfFileName, pdfContent);

                if (commonPdfGenerator != null)
                {
                    WriteDataToPdf(commonPdfGenerator, title, imageResult, tableData);
                    commonPdfGenerator.NewPage();
                }
            }
            if (commonPdfGenerator != null)
            {
                WritePdfToArchive(commonPdfGenerator, "Всё в одном файле.pdf");
            }

            result = _zipCompressor.GetArchive();
            WriteToCache(zipKey, result);
            return(result);
        }
예제 #4
0
 public override Present Realize(string refCredit)
 {
     return(cache.Get(future, refCredit));
 }