Feat/komga api (#2071)

* Implement Komga API endpoints with OPDS authentication

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* Add database migration and documentation for Komga API

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* Address code review comments - improve performance and maintainability

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* chore: update with main develop

* chore: log cleanup

* chore: fixed switch with missing types

* chore: missing case

* Merge pull request #4 from farfromrefug/copilot/fix-500-error-on-books-api

Fix NPE in Komga books API when pageCount is null and add unpaged parameter

* Add collections endpoint and page download with PNG conversion support

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* Address code review feedback for better resource management and error messages

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* Fix convert parameter to match specification (convert=png)

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* chore: renamed migration

* chore: migration

* chore: migration fix

* chore: should work now

* chore: settings

* chore: working with mihon

* Initial plan

* Add clean query parameter for Komga API endpoints

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* Address code review comments - remove unused imports and add @Primary annotation

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* Add demo test to illustrate clean mode effectiveness

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* Support both ?clean and ?clean=true syntax

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* Filter out empty arrays in clean mode

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* chore: missing field

* chore: missing field

* chore: fix error with missing number

* fix: added groupUnknown API parameters to sort by "Unknown Series" (true by default)

* Initial plan

* Convert groupUnknown from query parameter to Booklore setting

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* Remove unused groupUnknown variables from service and mapper

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* chore: fix seriesTitle in ungrouped unknowns

* Initial plan

* Optimize Komga API performance for series listing

- Optimize getAllSeries to only convert series on current page to DTOs
- Optimize getBooksBySeries to fetch books only once (not twice)
- Add database query methods for future optimizations
- Update tests to work with new optimizations
- All existing tests pass

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* Clean up unused database query methods in BookRepository

Remove unused optimization queries that don't align with application-level series grouping logic

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* Optimize getAllSeries to query distinct series names from database

- Add database queries to fetch distinct series names directly (no need to load all books)
- Add queries to fetch books only for specific series (when building DTOs)
- Support both groupUnknown=true and groupUnknown=false modes
- Add test to verify optimization works and books aren't loaded unnecessarily
- Performance improvement: For 1000+ books grouped into 100+ series, now only queries series names (~100 rows) instead of loading all books (~1000+ rows), then loads books only for the current page (~20-50 books per series on page)

Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>

* chore: migration fix from merge

* chore: address comments

* fix: handle getBookPage for PDF/CBX

* chore: rename migration

* chore: komga specific series queries fix

* chore: komga tests fix

* chore: front end komga tests

---------

Co-authored-by: Aditya Chandel <8075870+adityachandelgit@users.noreply.github.com>
Co-authored-by: ACX <8075870+acx10@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
This commit is contained in:
farfromrefuge
2026-01-19 08:44:53 -07:00
committed by GitHub
co-authored by farfromrefug Aditya Chandel ACX copilot-swe-agent[bot]
parent 4253322d2c
commit 69c3c88375
43 changed files with 3071 additions and 5 deletions
@@ -40,6 +40,93 @@
</div>
</div>
<div class="preferences-section">
<div class="section-header">
<h3 class="section-title">
<i class="pi pi-power-off"></i>
Komga API
</h3>
</div>
<div class="settings-card">
<div class="setting-item">
<div class="setting-info">
<div class="setting-label-row">
<label class="setting-label">Komga API Enabled</label>
<p-toggleswitch
[(ngModel)]="komgaApiEnabled"
(onChange)="toggleKomgaApi()">
</p-toggleswitch>
</div>
<p class="setting-description">
<i class="pi pi-info-circle"></i>
Enable the Komga-compatible API to use Komga clients (Tachiyomi, Komelia, etc.) with Booklore. Uses the same OPDS user accounts for authentication.
</p>
</div>
</div>
@if (komgaApiEnabled) {
<div class="setting-item">
<div class="setting-info">
<div class="setting-label-row">
<label class="setting-label">Group Unknown Series</label>
<p-toggleswitch
[(ngModel)]="komgaGroupUnknown"
(onChange)="toggleKomgaGroupUnknown()">
</p-toggleswitch>
</div>
<p class="setting-description">
<i class="pi pi-info-circle"></i>
When enabled, books without a series will be grouped under "Unknown Series". When disabled, each book without a series will appear as its own individual series.
</p>
</div>
</div>
}
</div>
</div>
@if (komgaApiEnabled) {
<div class="preferences-section">
<div class="section-header">
<h3 class="section-title">
<i class="pi pi-link"></i>
Komga API Endpoint
</h3>
</div>
<div class="settings-card">
<div class="setting-item">
<div class="setting-info">
<div class="setting-label-row">
<label class="setting-label">Komga Base URL</label>
<div class="input-group">
<input
id="komga-endpoint-url"
class="endpoint-input"
fluid
type="text"
pInputText
[value]="komgaEndpoint"
readonly/>
<p-button
icon="pi pi-copy"
severity="info"
outlined
size="small"
(onClick)="copyKomgaEndpoint()">
</p-button>
</div>
</div>
<p class="setting-description">
<i class="pi pi-info-circle"></i>
Use this URL as the server address in Komga-compatible clients. The API is available at <code>/komga/api/v1/*</code> and uses OPDS user credentials.
</p>
</div>
</div>
</div>
</div>
}
@if (opdsEnabled) {
<div class="preferences-section">
<div class="section-header">
@@ -310,4 +397,4 @@
</div>
</div>
}
</div>
</div>
@@ -42,7 +42,10 @@ import {Select} from 'primeng/select';
export class OpdsSettings implements OnInit, OnDestroy {
opdsEndpoint = `${API_CONFIG.BASE_URL}/api/v1/opds`;
komgaEndpoint = `${API_CONFIG.BASE_URL}/komga`;
opdsEnabled = false;
komgaApiEnabled = false;
komgaGroupUnknown = true;
private opdsService = inject(OpdsService);
private confirmationService = inject(ConfirmationService);
@@ -102,7 +105,9 @@ export class OpdsSettings implements OnInit, OnDestroy {
)
.subscribe(settings => {
this.opdsEnabled = settings.opdsServerEnabled ?? false;
if (this.opdsEnabled) {
this.komgaApiEnabled = settings.komgaApiEnabled ?? false;
this.komgaGroupUnknown = settings.komgaGroupUnknown ?? true;
if (this.opdsEnabled || this.komgaApiEnabled) {
this.loadUsers();
} else {
this.loading = false;
@@ -182,13 +187,42 @@ export class OpdsSettings implements OnInit, OnDestroy {
toggleOpdsServer(): void {
this.saveSetting(AppSettingKey.OPDS_SERVER_ENABLED, this.opdsEnabled);
if (this.opdsEnabled) {
if (this.opdsEnabled || this.komgaApiEnabled) {
this.loadUsers();
} else {
this.users = [];
}
}
toggleKomgaApi(): void {
this.saveKomgaSetting(AppSettingKey.KOMGA_API_ENABLED, this.komgaApiEnabled);
if (this.opdsEnabled || this.komgaApiEnabled) {
this.loadUsers();
} else {
this.users = [];
}
}
copyKomgaEndpoint(): void {
navigator.clipboard.writeText(this.komgaEndpoint).then(() => {
this.showMessage('success', 'Copied', 'Komga API endpoint copied to clipboard');
});
}
toggleKomgaGroupUnknown(): void {
this.appSettingsService.saveSettings([{key: AppSettingKey.KOMGA_GROUP_UNKNOWN, newValue: this.komgaGroupUnknown}]).subscribe({
next: () => {
const successMessage = (this.komgaGroupUnknown === true)
? 'Books without series will be grouped under "Unknown Series".'
: 'Books without series will appear as individual series.';
this.showMessage('success', 'Settings Saved', successMessage);
},
error: () => {
this.showMessage('error', 'Error', 'There was an error saving the settings.');
}
});
}
private saveSetting(key: string, value: unknown): void {
this.appSettingsService.saveSettings([{key, newValue: value}]).subscribe({
next: () => {
@@ -203,6 +237,20 @@ export class OpdsSettings implements OnInit, OnDestroy {
});
}
private saveKomgaSetting(key: string, value: unknown): void {
this.appSettingsService.saveSettings([{key, newValue: value}]).subscribe({
next: () => {
const successMessage = (value === true)
? 'Komga API Enabled.'
: 'Komga API Disabled.';
this.showMessage('success', 'Settings Saved', successMessage);
},
error: () => {
this.showMessage('error', 'Error', 'There was an error saving the settings.');
}
});
}
private resetCreateUserDialog(): void {
this.showCreateUserDialog = false;
this.newUser = {username: '', password: '', sortOrder: 'RECENT'};
@@ -254,4 +302,4 @@ export class OpdsSettings implements OnInit, OnDestroy {
this.destroy$.next();
this.destroy$.complete();
}
}
}
@@ -145,6 +145,8 @@ export interface AppSettings {
libraryMetadataRefreshOptions: MetadataRefreshOptions[];
uploadPattern: string;
opdsServerEnabled: boolean;
komgaApiEnabled: boolean;
komgaGroupUnknown: boolean;
remoteAuthEnabled: boolean;
oidcEnabled: boolean;
oidcProviderDetails: OidcProviderDetails;
@@ -187,6 +189,8 @@ export enum AppSettingKey {
LIBRARY_METADATA_REFRESH_OPTIONS = 'LIBRARY_METADATA_REFRESH_OPTIONS',
UPLOAD_FILE_PATTERN = 'UPLOAD_FILE_PATTERN',
OPDS_SERVER_ENABLED = 'OPDS_SERVER_ENABLED',
KOMGA_API_ENABLED = 'KOMGA_API_ENABLED',
KOMGA_GROUP_UNKNOWN = 'KOMGA_GROUP_UNKNOWN',
OIDC_ENABLED = 'OIDC_ENABLED',
OIDC_PROVIDER_DETAILS = 'OIDC_PROVIDER_DETAILS',
OIDC_AUTO_PROVISION_DETAILS = 'OIDC_AUTO_PROVISION_DETAILS',
@@ -21,6 +21,8 @@ describe('AppSettingsService', () => {
libraryMetadataRefreshOptions: [],
uploadPattern: '',
opdsServerEnabled: false,
komgaApiEnabled: false,
komgaGroupUnknown: false,
remoteAuthEnabled: false,
oidcEnabled: true,
oidcProviderDetails: {
@@ -391,6 +393,8 @@ describe('AppSettingsService - API Contract Tests', () => {
libraryMetadataRefreshOptions: [],
uploadPattern: '',
opdsServerEnabled: false,
komgaApiEnabled: false,
komgaGroupUnknown: false,
remoteAuthEnabled: false,
oidcEnabled: true,
oidcProviderDetails: {