Skip to content

Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.

Fixed offset bug in GetCachedPagePath for if you've read just one page. Fixed a bad refactor for getting files.

Joseph Milazzoc75feb03e16f
diff --git a/API/Controllers/LibraryController.cs b/API/Controllers/LibraryController.cs
index d69e98b6e..acf0ce24c 100644
--- a/API/Controllers/LibraryController.cs
+++ b/API/Controllers/LibraryController.cs
@@ -158,15 +158,10 @@ namespace API.Controllers
}
[HttpGet("series")]
- public async Task<ActionResult<IEnumerable<Series>>> GetSeriesForLibrary(int libraryId, bool forUser = false)
+ public async Task<ActionResult<IEnumerable<Series>>> GetSeriesForLibrary(int libraryId)
{
- int userId = 0;
- if (forUser)
- {
- var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
- userId = user.Id;
- }
- return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, userId));
+ var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
+ return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, user.Id));
}
[Authorize(Policy = "RequireAdminRole")]
diff --git a/API/Data/DataContext.cs b/API/Data/DataContext.cs
index 97a1c32b8..09a53f0cd 100644
--- a/API/Data/DataContext.cs
+++ b/API/Data/DataContext.cs
@@ -40,10 +40,10 @@ namespace API.Data
.HasForeignKey(ur => ur.RoleId)
.IsRequired();
// AppUsers have Libraries, not other way around
- builder.Entity<Library>()
- .HasMany(p => p.AppUsers)
- .WithMany(p => p.Libraries)
- .UsingEntity(j => j.ToTable("AppUserLibrary"));
+ // builder.Entity<Library>()
+ // .HasMany(p => p.AppUsers)
+ // .WithMany(p => p.Libraries)
+ // .UsingEntity(j => j.ToTable("AppUserLibrary"));
}
void OnEntityTracked(object sender, EntityTrackedEventArgs e)
diff --git a/API/Data/LibraryRepository.cs b/API/Data/LibraryRepository.cs
index 54fc64fea..ef21ffd55 100644
--- a/API/Data/LibraryRepository.cs
+++ b/API/Data/LibraryRepository.cs
@@ -1,4 +1,6 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using API.DTOs;
@@ -33,13 +35,15 @@ namespace API.Data
public async Task<IEnumerable<LibraryDto>> GetLibraryDtosForUsernameAsync(string userName)
{
- // TODO: Speed this query up
- return await _context.Library
+ Stopwatch sw = Stopwatch.StartNew();
+ var libs = await _context.Library
.Include(l => l.AppUsers)
.Where(library => library.AppUsers.Any(x => x.UserName == userName))
.ProjectTo<LibraryDto>(_mapper.ConfigurationProvider)
.AsNoTracking()
.ToListAsync();
+ Console.WriteLine("Processed GetLibraryDtosForUsernameAsync in {0} milliseconds", sw.ElapsedMilliseconds);
+ return libs;
}
public async Task<IEnumerable<Library>> GetLibrariesAsync()
diff --git a/API/Services/CacheService.cs b/API/Services/CacheService.cs
index 3572d7567..0c69abae8 100644
--- a/API/Services/CacheService.cs
+++ b/API/Services/CacheService.cs
@@ -113,7 +113,7 @@ namespace API.Services
var array = files.ToArray();
Array.Sort(array, _numericComparer); // TODO: Find a way to apply numericComparer to IList.
- return array.ElementAt((page + 1) - pagesSoFar);
+ return array.ElementAt(page - pagesSoFar);
}
pagesSoFar += mangaFile.NumberOfPages;
diff --git a/API/Services/DirectoryService.cs b/API/Services/DirectoryService.cs
index e49e36c99..25c6349c9 100644
--- a/API/Services/DirectoryService.cs
+++ b/API/Services/DirectoryService.cs
@@ -40,8 +40,8 @@ namespace API.Services
/// <param name="searchPatternExpression">Regex version of search pattern (ie \.mp3|\.mp4). Defaults to * meaning all files.</param>
/// <param name="searchOption">SearchOption to use, defaults to TopDirectoryOnly</param>
/// <returns>List of file paths</returns>
- public static IEnumerable<string> GetFiles(string path,
- string searchPatternExpression = "*",
+ public static IEnumerable<string> GetFilesWithCertainExtensions(string path,
+ string searchPatternExpression = "",
SearchOption searchOption = SearchOption.TopDirectoryOnly)
{
if (!Directory.Exists(path)) return ImmutableList<string>.Empty;
@@ -50,6 +50,14 @@ namespace API.Services
.Where(file =>
reSearchPattern.IsMatch(Path.GetExtension(file)));
}
+
+ public static IList<string> GetFiles(string path)
+ {
+ if (!Directory.Exists(path)) return ImmutableList<string>.Empty;
+ return Directory.GetFiles(path);
+ }
+
+
public IEnumerable<string> ListDirectory(string rootPath)
{
@@ -384,7 +392,7 @@ namespace API.Services
}
try {
- files = DirectoryService.GetFiles(currentDir, Parser.Parser.MangaFileExtensions)
+ files = DirectoryService.GetFilesWithCertainExtensions(currentDir, Parser.Parser.MangaFileExtensions)
.ToArray();
}
catch (UnauthorizedAccessException e) {