示例#1
0
        public Feed SearchBySeries(string series, [FromUri] int page = 1)
        {
            if (page < 1)
            {
                page = 1;
            }
            series = Uri.UnescapeDataString(series);
            var searcher = new LuceneIndexStorage();
            int total;
            var books = searcher.SearchExact(out total, "series_exact", series, page);

            var feed = new Feed();

            feed.Id    = $"tag:root:series:{series}";
            feed.Title = T._("Books in the series {0}", series);
            feed.Total = total;

            AddNavigation($"{Prefix}/series/{Uri.EscapeDataString(series)}", feed, page, total, searcher);

            foreach (var book in books)
            {
                feed.Entries.Add(GetBook(book));
            }

            return(feed);
        }
示例#2
0
        public Feed GetByGenres(string genre)
        {
            genre = Uri.UnescapeDataString(genre ?? "");

            var feed = new Feed();

            feed.Id    = "tag:root:genres" + (genre == "" ? "" : $":{genre}");
            feed.Title = genre == "" ? T._("Books by genres") : T._("Books in the genre of {0}", GenreExtensions.GetDisplayName(genre));
            AddNavigation($"{Prefix}/genres/{Uri.EscapeDataString(genre)}", feed);

            var searcher = new LuceneIndexStorage();
            var genres   = searcher.GetAllGenres(genre);

            foreach (var k in genres)
            {
                var tag   = k.Value.Item2 ? "genre" : "genres";
                var url   = k.Value.Item1;
                var entry = new FeedEntry();
                entry.Id    = $"tag:root:{tag}:{url}";
                entry.Title = k.Key;
                entry.Links.Add(new FeedLink {
                    Type = FeedLinkType.AtomAcquisition, Href = $"{Prefix}/{tag}/{Uri.EscapeDataString(url)}"
                });
                entry.Content = new FeedEntryContent {
                    Text = T._("Books in the genre of {0}", k.Key)
                };
                feed.Entries.Add(entry);
            }

            return(feed);
        }
示例#3
0
        public Feed SearchByGenre(string genre, [FromUri] int page = 1)
        {
            if (page < 1)
            {
                page = 1;
            }
            genre = Uri.UnescapeDataString(genre);
            var searcher = new LuceneIndexStorage();
            int total;
            var books = searcher.SearchExact(out total, "genre", genre, page);

            var feed = new Feed();

            feed.Id    = "tag:root:genre:" + genre;
            feed.Title = T._("Books in the genre of {0}", GenreExtensions.GetDisplayName(genre));
            feed.Total = total;
            AddNavigation($"{Prefix}/genre/{Uri.EscapeDataString(genre)}", feed, page, total, searcher);

            foreach (var book in books)
            {
                feed.Entries.Add(GetBook(book));
            }

            return(feed);
        }
示例#4
0
        public Feed SearchByAuthor(string author, [FromUri] int page = 1)
        {
            if (page < 1)
            {
                page = 1;
            }
            author = Uri.UnescapeDataString(author);
            var searcher = new LuceneIndexStorage();
            int total;
            var books = searcher.SearchExact(out total, "author_exact", author, page);

            var feed = new Feed();

            feed.Id    = $"tag:root:author:{author}";
            feed.Title = T._("Books by {0}", author);
            feed.Total = total;
            AddNavigation($"{Prefix}/author/{Uri.EscapeDataString(author)}", feed, page, total, searcher);

            foreach (var book in books)
            {
                feed.Entries.Add(GetBook(book));
            }

            return(feed);
        }
示例#5
0
        public Feed SearchInField([FromUri] string q, [FromUri] string field, [FromUri] int page = 1)
        {
            if (page < 1)
            {
                page = 1;
            }
            var         searcher = new LuceneIndexStorage();
            int         total    = 0;
            List <Book> books    = null;

            if (predefinedSearchFields.Any(f => f.Field == field) || PluginProvider.Instance.IndexFields.Any(f => f.Field == field))
            {
                books = searcher.Search(out total, q, field, page);
            }

            var feed = new Feed();

            feed.Id    = $"tag:root:search:{field}:{q}";
            feed.Title = T._("Search results: {0}", q);
            feed.Total = total;
            AddNavigation($"{Prefix}/search?field={field}&q={q}", feed, page, total, searcher);

            if (books != null)
            {
                foreach (var book in books)
                {
                    feed.Entries.Add(GetBook(book));
                }
            }

            return(feed);
        }
示例#6
0
        public Feed Search([FromUri] string everywhere, [FromUri] int page = 1)
        {
            if (page < 1)
            {
                page = 1;
            }
            var searcher = new LuceneIndexStorage();
            int total;
            var query = string.Format(@"title:""{0}"" OR author:""{0}"" OR series:""{0}""", searcher.Escape(everywhere));
            var books = searcher.Search(out total, query, "title", page);

            var feed = new Feed();

            feed.Id    = $"tag:root:search:everywhere:{everywhere}";
            feed.Title = T._("Search results: {0}", everywhere);
            feed.Total = total;
            AddNavigation($"{Prefix}/search?everywhere={everywhere}", feed, page, total, searcher);

            foreach (var book in books)
            {
                feed.Entries.Add(GetBook(book));
            }

            return(feed);
        }
示例#7
0
        public HttpResponseMessage GetCover(Guid id)
        {
            var searcher = new LuceneIndexStorage();
            int total;
            var books = searcher.SearchExact(out total, "guid", id.ToString(), take: 1);

            if (total != 1)
            {
                logger.Debug("File {0} not found", id);
                throw new KeyNotFoundException("Key Not Found: " + id);
            }

            logger.Debug("File {0} found in {1}ms", id, searcher.Time);
            var book = books[0];

            if (book.Cover == null)
            {
                logger.Warn("No cover found for file {0}", id);
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            var result = new HttpResponseMessage(HttpStatusCode.OK);

            result.Content = new ByteArrayContent(book.Cover.Data);
            result.Content.Headers.ContentType = new MediaTypeHeaderValue(book.Cover.ContentType);
            return(result);
        }
示例#8
0
        public void Update(Book book)
        {
            if (book.UpdatedFromFile)
            {
                return;
            }
            logger.Debug("Book being updated, id:{0}", book.Id);

            var parser = PluginProvider.Instance.GetFileFormatReader(book.Ext);

            if (parser == null)
            {
                return;
            }

            bool replace = false;

            try
            {
                using (var stream = FileUtils.GetBookFile(book))
                {
                    replace = parser.Read(book, stream);
                }
                logger.Debug("Book updated successfully, id:{0}", book.Id);
            }
            catch (Exception e)
            {
                logger.Debug("Book update failed, id:{0}. {1}", book.Id, e.Message);
            }

            if (importer == null)
            {
                importer = new LuceneIndexStorage();
                importer.Open(Settings.Instance.DatabaseIndex);
            }
            if (replace)
            {
                book.UpdatedFromFile = true;
                importer.Replace(book);
            }
        }
示例#9
0
        public int Run(BaseOptions options)
        {
            Settings.Load(options.Config);
            var opts = (RmOptions)options;

            var library = Guid.Parse(opts.Lib);

            if (!Settings.Instance.Libraries.ContainsKey(library))
            {
                Console.Error.WriteLine("Library {0} not found.", library);
                return(1);
            }

            var storage = new LuceneIndexStorage();
            var total   = storage.RemoveLibrary(Settings.Instance.DatabaseIndex, library.ToString());

            Settings.Instance.Libraries.Remove(library);
            Settings.Save();
            Console.WriteLine("Library {0} removed ({1} books).", library, total);

            return(0);
        }
示例#10
0
        public HttpResponseMessage GetFile(Guid id, string ext)
        {
            var searcher = new LuceneIndexStorage();
            int total;
            var books = searcher.SearchExact(out total, "guid", id.ToString(), take: 1);

            if (total != 1)
            {
                logger.Debug("File {0} not found", id);
                throw new KeyNotFoundException("Key Not Found: " + id);
            }

            logger.Debug("File {0} found in {1}ms", id, searcher.Time);

            var book      = books[0];
            var converter = Settings.Instance.Converters
                            .FirstOrDefault(x => x.From.Equals(book.Ext, StringComparison.InvariantCultureIgnoreCase) &&
                                            x.To.Equals(ext, StringComparison.InvariantCultureIgnoreCase));

            if (!ext.Equals(book.Ext, StringComparison.InvariantCultureIgnoreCase) && converter == null)
            {
                logger.Warn("No converter found for '{0}'->'{1}'", book.Ext, ext);
                return(new HttpResponseMessage(HttpStatusCode.NotFound));
            }

            var content  = GetFileInFormat(book, converter);
            var result   = new HttpResponseMessage(HttpStatusCode.OK);
            var mimeType = MimeHelper.GetMimeType(ext);

            result.Content = new StreamContent(content);
            result.Content.Headers.ContentType                 = new MediaTypeHeaderValue(mimeType);
            result.Content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentDisposition.FileName = Util.GetBookSafeName(book, ext);

            logger.Info("Book {0} served to {1}", id, Request.GetOwinContext().Request.RemoteIpAddress);
            return(result);
        }
示例#11
0
        public Feed SearchAdvanced([FromUri] string advanced, [FromUri] int page = 1)
        {
            if (page < 1)
            {
                page = 1;
            }
            var searcher = new LuceneIndexStorage();
            int total;
            var books = searcher.Search(out total, advanced, "title", page);

            var feed = new Feed();

            feed.Id    = $"tag:root:search:advanced:{advanced}";
            feed.Title = T._("Search results: {0}", advanced);
            feed.Total = total;
            AddNavigation($"{Prefix}/search?advanced={advanced}", feed, page, total, searcher);

            foreach (var book in books)
            {
                feed.Entries.Add(GetBook(book));
            }

            return(feed);
        }
示例#12
0
        private void AddNavigation(string uri, Feed feed, int page = 0, int total = 0, LuceneIndexStorage searcher = null)
        {
            var favicon = Path.Combine(Util.Normalize(Settings.Instance.Web), "favicon.ico");

            if (File.Exists(favicon))
            {
                feed.Icon = "/favicon.ico";
            }

            feed.Links.Add(SearchLink);
            feed.Links.Add(StartLink);
            feed.Links.Add(new FeedLink {
                Rel = FeedLinkRel.Self, Type = FeedLinkType.AtomNavigation, Href = ChangePage(uri, page)
            });
            if (page > 1)
            {
                feed.Links.Add(new FeedLink
                {
                    Rel  = FeedLinkRel.Prev,
                    Type = FeedLinkType.AtomNavigation,
                    Href = ChangePage(uri, page - 1)
                });
            }
            if (page * Settings.Instance.Pagination < total)
            {
                feed.Links.Add(new FeedLink
                {
                    Rel  = FeedLinkRel.Next,
                    Type = FeedLinkType.AtomNavigation,
                    Href = ChangePage(uri, page + 1)
                });
            }
#if DEBUG
            if (searcher != null)
            {
                feed.Links.Add(new FeedLink {
                    Rel = FeedLinkRel.Debug, Type = "query", Title = searcher.Query
                });
                feed.Links.Add(new FeedLink {
                    Rel = FeedLinkRel.Debug, Type = "time", Title = searcher.Time + "ms"
                });
            }
#endif
        }