worldhopper/lib/models/opds_server.dart

79 lines
2.2 KiB
Dart

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:worldhopper/services/url_helper.dart';
part 'opds_server.freezed.dart';
part 'opds_server.g.dart';
/// Represents an OPDS server configuration
@freezed
class OPDSServer with _$OPDSServer {
const factory OPDSServer({
/// Unique identifier for the server
required String id,
/// Display name for the server
required String name,
/// Root URL of the OPDS server
required String url,
/// Optional username for HTTP Basic Auth
String? username,
/// Optional password for HTTP Basic Auth (stored encrypted in database)
String? password,
/// Timestamp when the server was added
required DateTime createdAt,
/// Timestamp of the last successful sync
DateTime? lastSyncedAt,
}) = _OPDSServer;
/// Create OPDSServer from JSON
factory OPDSServer.fromJson(Map<String, dynamic> json) =>
_$OPDSServerFromJson(json);
/// Create OPDSServer from database row
factory OPDSServer.fromDatabase(Map<String, dynamic> row) {
return OPDSServer(
id: row['id'] as String,
name: row['name'] as String,
url: row['url'] as String,
username: row['username'] as String?,
password: row['password'] as String?,
createdAt: DateTime.fromMillisecondsSinceEpoch(row['created_at'] as int),
lastSyncedAt: row['last_synced_at'] != null
? DateTime.fromMillisecondsSinceEpoch(row['last_synced_at'] as int)
: null,
);
}
}
extension OPDSServerX on OPDSServer {
/// Convert OPDSServer to database row
Map<String, dynamic> toDatabase() {
return {
'id': id,
'name': name,
'url': url,
'username': username,
'password': password,
'created_at': createdAt.millisecondsSinceEpoch,
'last_synced_at': lastSyncedAt?.millisecondsSinceEpoch,
};
}
/// Check if server requires authentication
bool get requiresAuth => username != null && password != null;
/// Resolve a relative URL against this server's base URL
String resolveUrl(String relativeUrl) {
return UrlHelper.resolveUrl(url, relativeUrl);
}
/// Make a request URL by resolving it against the server base
String makeRequest(String path) {
return UrlHelper.resolveUrl(url, path);
}
}