Implement OPDS

This commit is contained in:
adityachandelgit
2025-04-28 16:16:36 -06:00
committed by Aditya Chandel
parent 71bc927d54
commit c4f8bf8322
26 changed files with 879 additions and 21 deletions
@@ -6,4 +6,5 @@ export interface AppSettings {
metadataRefreshOptions: MetadataRefreshOptions;
coverResolution: string;
uploadPattern: string;
opdsServerEnabled: boolean;
}
@@ -1,3 +1,11 @@
<p class="text-xl pb-2 pt-8">
Uploaded Files Pattern
<i class="pi pi-info-circle text-sky-600"
pTooltip="Define a naming pattern for future uploads using available metadata. Existing files will remain unchanged."
tooltipPosition="right"
style="cursor: pointer;">
</i>
</p>
<div class="flex flex-col space-y-6 p-4 m-4 custom-border">
<div>
<h2 class="mb-3 text-lg">Enter Custom File Naming Pattern:</h2>
@@ -6,12 +6,13 @@ import {AppSettingsService} from '../core/service/app-settings.service';
import {Observable} from 'rxjs';
import {AppSettings} from '../core/model/app-settings.model';
import {filter, take} from 'rxjs/operators';
import {Tooltip} from 'primeng/tooltip';
@Component({
selector: 'app-file-upload-pattern',
templateUrl: './file-upload-pattern.component.html',
standalone: true,
imports: [FormsModule, Button],
imports: [FormsModule, Button, Tooltip],
styleUrls: ['./file-upload-pattern.component.scss']
})
export class FileUploadPatternComponent implements OnInit {
@@ -0,0 +1,158 @@
<p class="text-xl pb-2 pt-6 flex items-center gap-2">
OPDS Server Settings
<i
class="pi pi-info-circle text-sky-600"
pTooltip="OPDS allows your book collection to be accessed by compatible reading apps through your private server."
tooltipPosition="right"
style="cursor: pointer;">
</i>
</p>
<div class="flex flex-col space-y-6 p-4 m-4 custom-border">
<div class="flex items-center gap-4">
<p class="text-lg py-1">OPDS Server Enabled:</p>
<p-toggleButton
[(ngModel)]="opdsEnabled"
onLabel="OPDS ON"
offLabel="OPDS OFF"
(onChange)="toggleOpdsServer()"
style="width: 150px;">
</p-toggleButton>
</div>
<div *ngIf="opdsEnabled" class="mt-6">
<p-button
label="Create New User"
icon="pi pi-plus"
outlined="true"
severity="primary"
(onClick)="openCreateUserDialog()">
</p-button>
<div class="mt-3">
<p-table [value]="users" [responsiveLayout]="'scroll'">
<ng-template pTemplate="header">
<tr>
<th>Username</th>
<th>Password</th>
<th>Reset Password</th>
<th>Delete</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-user>
<tr>
<td>{{ user.username }}</td>
<td class="flex items-center gap-2">
<p-password
[(ngModel)]="user.password"
[disabled]="true">
</p-password>
<i
class="pi pi-info-circle text-sky-600"
pTooltip="The password is hidden for security reasons and is only visible once during creation. You can reset the password if needed."
tooltipPosition="right"
style="cursor: pointer;">
</i>
</td>
<td>
<p-button
icon="pi pi-key"
severity="warn"
size="small"
outlined="true"
(onClick)="openResetPasswordDialog(user)">
</p-button>
</td>
<td>
<p-button
icon="pi pi-trash"
severity="danger"
size="small"
outlined="true"
(onClick)="deleteUser(user.id)">
</p-button>
</td>
</tr>
</ng-template>
</p-table>
</div>
</div>
</div>
<p-dialog
header="Create New OPDS User"
[(visible)]="createUserDialogVisible"
[modal]="true"
[style]="{ width: '400px' }">
<div>
<div>
<label for="username">Username</label>
<input id="username" type="text" pInputText [(ngModel)]="newUser.username" class="w-full"/>
</div>
<div class="mt-4">
<div class="flex items-center gap-2">
<label for="password">Password</label>
<i
class="pi pi-info-circle text-yellow-500"
pTooltip="Password can only be seen now. It won’t be retrievable later. Please save it securely."
tooltipPosition="right"
style="cursor: pointer;">
</i>
</div>
<input id="password" pInputText [(ngModel)]="newUser.password" class="w-full"/>
</div>
<div class="flex justify-end mt-6 gap-4">
<p-button
label="Cancel"
icon="pi pi-times"
severity="secondary"
(onClick)="cancelCreateUser()">
</p-button>
<p-button
label="Save"
icon="pi pi-check"
severity="success"
(onClick)="saveNewUser()">
</p-button>
</div>
</div>
</p-dialog>
<p-dialog
header="Reset User Password"
[(visible)]="resetPasswordDialogVisible"
[modal]="true"
[style]="{ width: '400px' }">
<div>
<p class="mb-6 mt-2">Reset password for: <b>{{ selectedUser?.username }}</b></p>
<div>
<div class="flex items-center">
<label for="newPassword">New Password</label>
<i
class="pi pi-info-circle text-yellow-500 ml-2"
pTooltip="Password can only be seen now. It won’t be retrievable later. Please save it securely."
tooltipPosition="right"
style="cursor: pointer;">
</i>
</div>
<input id="newPassword" pInputText [(ngModel)]="newPassword" class="w-full"/>
</div>
<div class="flex justify-end mt-4 gap-4">
<p-button
label="Cancel"
icon="pi pi-times"
severity="secondary"
(onClick)="cancelResetPassword()">
</p-button>
<p-button
label="Save"
icon="pi pi-check"
severity="success"
(onClick)="confirmResetPassword()">
</p-button>
</div>
</div>
</p-dialog>
@@ -0,0 +1,4 @@
.custom-border {
border: 1px solid var(--border-color);
border-radius: var(--card-border);
}
@@ -0,0 +1,177 @@
import { Component, inject, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { NgIf } from '@angular/common';
import { TableModule } from 'primeng/table';
import { Button } from 'primeng/button';
import { InputText } from 'primeng/inputtext';
import { Dialog } from 'primeng/dialog';
import { Tooltip } from 'primeng/tooltip';
import { ToggleButton } from 'primeng/togglebutton';
import { MessageService } from 'primeng/api';
import { filter, take } from 'rxjs/operators';
import { OpdsUser, OpdsUserService } from '../opds-user.service';
import { AppSettingsService } from '../core/service/app-settings.service';
import { AppSettings } from '../core/model/app-settings.model';
import { Password } from 'primeng/password';
@Component({
selector: 'app-opds-settings',
standalone: true,
imports: [
FormsModule,
NgIf,
TableModule,
Button,
InputText,
Dialog,
Tooltip,
ToggleButton,
Password
],
templateUrl: './opds-settings.component.html',
styleUrl: './opds-settings.component.scss'
})
export class OpdsSettingsComponent implements OnInit {
opdsEnabled = false;
users: OpdsUser[] = [];
selectedUser: OpdsUser | null = null;
createUserDialogVisible = false;
resetPasswordDialogVisible = false;
newUser = { username: '', password: '' };
newPassword = '';
private readonly appSettings$ = inject(AppSettingsService).appSettings$;
private readonly opdsUserService = inject(OpdsUserService);
private readonly messageService = inject(MessageService);
private readonly appSettingsService = inject(AppSettingsService);
ngOnInit(): void {
this.appSettings$
.pipe(
filter((settings): settings is AppSettings => settings != null),
take(1)
)
.subscribe(settings => {
this.opdsEnabled = settings.opdsServerEnabled ?? false;
if (this.opdsEnabled) {
this.loadUsers();
}
});
}
toggleOpdsServer(): void {
this.saveSetting('opds_server_enabled', this.opdsEnabled);
if (this.opdsEnabled) {
this.loadUsers();
} else {
this.users = [];
}
}
loadUsers(): void {
this.opdsUserService.getUsers().subscribe({
next: (users) => this.users = users,
error: () => this.showError('Load Users Failed', 'Unable to fetch users.')
});
}
openCreateUserDialog(): void {
this.newUser = { username: '', password: '' };
this.createUserDialogVisible = true;
}
cancelCreateUser(): void {
this.createUserDialogVisible = false;
}
saveNewUser(): void {
const { username, password } = this.newUser;
if (username.trim() && password.trim()) {
this.opdsUserService.createUser(this.newUser).subscribe({
next: () => {
this.createUserDialogVisible = false;
this.loadUsers();
this.showSuccess('User Created', 'New OPDS user created successfully.');
},
error: (err) => {
const errorMessage = err?.error?.message || 'Unable to create user.';
this.showError('Create User Failed', errorMessage);
}
});
} else {
this.showWarning('Invalid Input', 'Username and Password are required.');
}
}
openResetPasswordDialog(user: OpdsUser): void {
this.selectedUser = user;
this.newPassword = '';
this.resetPasswordDialogVisible = true;
}
cancelResetPassword(): void {
this.resetPasswordDialogVisible = false;
this.selectedUser = null;
this.newPassword = '';
}
confirmResetPassword(): void {
if (this.selectedUser && this.newPassword.trim()) {
this.opdsUserService.resetPassword(this.selectedUser.id, this.newPassword).subscribe({
next: () => {
this.resetPasswordDialogVisible = false;
this.showSuccess('Password Reset', 'User password reset successfully.');
},
error: (err) => {
const errorMessage = err?.error?.message || 'Unable to reset password.';
this.showError('Reset Password Failed', errorMessage);
}
});
} else {
this.showWarning('Invalid Input', 'New password is required.');
}
}
deleteUser(userId: number): void {
this.opdsUserService.deleteUser(userId).subscribe({
next: () => {
this.loadUsers();
this.showSuccess('User Deleted', 'User deleted successfully.');
},
error: () => {
this.showError('Delete User Failed', 'Unable to delete user.');
}
});
}
private saveSetting(key: string, value: unknown): void {
this.appSettingsService.saveSettings([{ key, newValue: value }]).subscribe({
next: () => {
let successMessage = 'Settings saved successfully.';
if (key === 'opds_server_enabled') {
successMessage = (value === true)
? 'OPDS Server Enabled.'
: 'OPDS Server Disabled.';
}
this.showSuccess('Settings Saved', successMessage);
},
error: () => {
this.showError('Error', 'There was an error saving the settings.');
}
});
}
private showSuccess(summary: string, detail: string): void {
this.messageService.add({ severity: 'success', summary, detail });
}
private showError(summary: string, detail: string): void {
this.messageService.add({ severity: 'error', summary, detail });
}
private showWarning(summary: string, detail: string): void {
this.messageService.add({ severity: 'warn', summary, detail });
}
}
+42
View File
@@ -0,0 +1,42 @@
import {inject, Injectable} from '@angular/core';
import {API_CONFIG} from './config/api-config';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
export interface OpdsUser {
id: number;
username: string;
password: string;
}
@Injectable({
providedIn: 'root'
})
export class OpdsUserService {
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/opds-users`;
private http = inject(HttpClient);
constructor() {
}
createUser(userData: Omit<OpdsUser, 'id'>): Observable<void> {
return this.http.post<void>(this.url, userData);
}
getUsers(): Observable<OpdsUser[]> {
return this.http.get<OpdsUser[]>(this.url);
}
deleteUser(userId: number): Observable<void> {
return this.http.delete<void>(`${this.url}/${userId}`);
}
resetPassword(resetPasswordUserId: number, newPassword: string): Observable<void> {
const resetPayload = {
password: newPassword
};
return this.http.put<void>(`${this.url}/${resetPasswordUserId}/reset-password`, resetPayload);
}
}
@@ -71,14 +71,10 @@
<p-divider></p-divider>
<p class="text-xl pb-2 pt-8">
Uploaded Files Pattern
<i class="pi pi-info-circle text-sky-600"
pTooltip="Define a naming pattern for future uploads using available metadata. Existing files will remain unchanged."
tooltipPosition="right"
style="cursor: pointer;">
</i>
</p>
<app-opds-settings></app-opds-settings>
<p-divider></p-divider>
<app-file-upload-pattern></app-file-upload-pattern>
<p-divider></p-divider>
@@ -17,6 +17,7 @@ import {MetadataRefreshOptions} from '../../metadata/model/request/metadata-refr
import {MetadataAdvancedFetchOptionsComponent} from '../../metadata/metadata-options-dialog/metadata-advanced-fetch-options/metadata-advanced-fetch-options.component';
import {filter, take} from 'rxjs/operators';
import {FileUploadPatternComponent} from '../../file-upload-pattern/file-upload-pattern.component';
import {OpdsSettingsComponent} from '../../opds-settings/opds-settings.component';
@Component({
selector: 'app-global-preferences',
@@ -30,7 +31,8 @@ import {FileUploadPatternComponent} from '../../file-upload-pattern/file-upload-
ToggleSwitch,
FormsModule,
MetadataAdvancedFetchOptionsComponent,
FileUploadPatternComponent
FileUploadPatternComponent,
OpdsSettingsComponent
],
templateUrl: './global-preferences.component.html',
styleUrl: './global-preferences.component.scss'