mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-14 16:25:16 +00:00
feat: add caching support with track metadata
This commit is contained in:
parent
499ecfba26
commit
7fbb4bce78
@ -1,4 +1,5 @@
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:metadata_god/metadata_god.dart';
|
||||
import 'package:path/path.dart';
|
||||
@ -37,6 +38,33 @@ extension TrackExtensions on Track {
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
Metadata toMetadata({
|
||||
required int fileLength,
|
||||
Uint8List? imageBytes,
|
||||
}) {
|
||||
return Metadata(
|
||||
title: name,
|
||||
artist: artists?.map((a) => a.name).join(", "),
|
||||
album: album?.name,
|
||||
albumArtist: artists?.map((a) => a.name).join(", "),
|
||||
year: album?.releaseDate != null
|
||||
? int.tryParse(album!.releaseDate!.split("-").first) ?? 1969
|
||||
: 1969,
|
||||
trackNumber: trackNumber,
|
||||
discNumber: discNumber,
|
||||
durationMs: durationMs?.toDouble() ?? 0.0,
|
||||
fileSize: BigInt.from(fileLength),
|
||||
trackTotal: album?.tracks?.length ?? 0,
|
||||
picture: imageBytes != null
|
||||
? Picture(
|
||||
data: imageBytes,
|
||||
// Spotify images are always JPEGs
|
||||
mimeType: 'image/jpeg',
|
||||
)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
extension TrackSimpleExtensions on TrackSimple {
|
||||
|
71
lib/models/parser/range_headers.dart
Normal file
71
lib/models/parser/range_headers.dart
Normal file
@ -0,0 +1,71 @@
|
||||
class ContentRangeHeader {
|
||||
final int start;
|
||||
final int end;
|
||||
final int total;
|
||||
|
||||
ContentRangeHeader(this.start, this.end, this.total);
|
||||
|
||||
factory ContentRangeHeader.parse(String value) {
|
||||
if (value.isEmpty) {
|
||||
throw FormatException('Invalid Content-Range header: $value');
|
||||
}
|
||||
|
||||
final parts = value.split(' ');
|
||||
if (parts.length != 2) {
|
||||
throw FormatException('Invalid Content-Range header: $value');
|
||||
}
|
||||
|
||||
final rangeParts = parts[1].split('/');
|
||||
if (rangeParts.length != 2) {
|
||||
throw FormatException('Invalid Content-Range header: $value');
|
||||
}
|
||||
|
||||
final range = rangeParts[0].split('-');
|
||||
if (range.length != 2) {
|
||||
throw FormatException('Invalid Content-Range header: $value');
|
||||
}
|
||||
|
||||
return ContentRangeHeader(
|
||||
int.parse(range[0]),
|
||||
int.parse(range[1]),
|
||||
int.parse(rangeParts[1]),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'bytes $start-$end/$total';
|
||||
}
|
||||
}
|
||||
|
||||
class RangeHeader {
|
||||
final int start;
|
||||
final int? end;
|
||||
|
||||
RangeHeader(this.start, this.end);
|
||||
|
||||
factory RangeHeader.parse(String value) {
|
||||
if (value.isEmpty) {
|
||||
return RangeHeader(0, null);
|
||||
}
|
||||
|
||||
final parts = value.split('=');
|
||||
if (parts.length != 2) {
|
||||
throw FormatException('Invalid Range header: $value');
|
||||
}
|
||||
|
||||
final ranges = parts[1].split('-');
|
||||
|
||||
return RangeHeader(
|
||||
int.parse(ranges[0]),
|
||||
ranges.elementAtOrNull(1) != null && ranges[1].isNotEmpty
|
||||
? int.parse(ranges[1])
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'bytes=$start-${end ?? ""}';
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:spotube/extensions/track.dart';
|
||||
import 'package:spotube/services/logger/logger.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:metadata_god/metadata_god.dart';
|
||||
import 'package:path/path.dart';
|
||||
@ -16,6 +16,7 @@ import 'package:spotube/services/download_manager/download_manager.dart';
|
||||
import 'package:spotube/services/sourced_track/enums.dart';
|
||||
import 'package:spotube/services/sourced_track/sourced_track.dart';
|
||||
import 'package:spotube/utils/primitive_utils.dart';
|
||||
import 'package:spotube/utils/service_utils.dart';
|
||||
|
||||
class DownloadManagerProvider extends ChangeNotifier {
|
||||
DownloadManagerProvider({required this.ref})
|
||||
@ -53,33 +54,16 @@ class DownloadManagerProvider extends ChangeNotifier {
|
||||
await oldFile.delete();
|
||||
}
|
||||
|
||||
final imageBytes = await downloadImage(
|
||||
final imageBytes = await ServiceUtils.downloadImage(
|
||||
(track.album?.images).asUrlString(
|
||||
placeholder: ImagePlaceholder.albumArt,
|
||||
index: 1,
|
||||
),
|
||||
);
|
||||
|
||||
final metadata = Metadata(
|
||||
title: track.name,
|
||||
artist: track.artists?.map((a) => a.name).join(", "),
|
||||
album: track.album?.name,
|
||||
albumArtist: track.artists?.map((a) => a.name).join(", "),
|
||||
year: track.album?.releaseDate != null
|
||||
? int.tryParse(track.album!.releaseDate!.split("-").first) ?? 1969
|
||||
: 1969,
|
||||
trackNumber: track.trackNumber,
|
||||
discNumber: track.discNumber,
|
||||
durationMs: track.durationMs?.toDouble() ?? 0.0,
|
||||
fileSize: BigInt.from(await file.length()),
|
||||
trackTotal: track.album?.tracks?.length ?? 0,
|
||||
picture: imageBytes != null
|
||||
? Picture(
|
||||
data: imageBytes,
|
||||
// Spotify images are always JPEGs
|
||||
mimeType: 'image/jpeg',
|
||||
)
|
||||
: null,
|
||||
final metadata = track.toMetadata(
|
||||
fileLength: await file.length(),
|
||||
imageBytes: imageBytes,
|
||||
);
|
||||
|
||||
await MetadataGod.writeMetadata(
|
||||
@ -116,29 +100,6 @@ class DownloadManagerProvider extends ChangeNotifier {
|
||||
final Set<Track> $backHistory;
|
||||
final DownloadManager dl;
|
||||
|
||||
/// Spotify Images are always JPEGs
|
||||
Future<Uint8List?> downloadImage(
|
||||
String imageUrl,
|
||||
) async {
|
||||
try {
|
||||
final fileStream = DefaultCacheManager().getImageFile(imageUrl);
|
||||
|
||||
final bytes = List<int>.empty(growable: true);
|
||||
|
||||
await for (final data in fileStream) {
|
||||
if (data is FileInfo) {
|
||||
bytes.addAll(data.file.readAsBytesSync());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return Uint8List.fromList(bytes);
|
||||
} catch (e, stackTrace) {
|
||||
AppLogger.reportError(e, stackTrace);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
String getTrackFileUrl(Track track) {
|
||||
final name =
|
||||
"${track.name} - ${track.artists?.asString() ?? ""}.${downloadCodec.name}";
|
||||
|
@ -1,14 +1,28 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart' hide Response;
|
||||
import 'package:dio/dio.dart' as dio_lib;
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:metadata_god/metadata_god.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:shelf/shelf.dart';
|
||||
import 'package:spotube/extensions/artist_simple.dart';
|
||||
import 'package:spotube/extensions/image.dart';
|
||||
import 'package:spotube/extensions/track.dart';
|
||||
import 'package:spotube/models/parser/range_headers.dart';
|
||||
import 'package:spotube/provider/audio_player/audio_player.dart';
|
||||
import 'package:spotube/provider/audio_player/state.dart';
|
||||
|
||||
import 'package:spotube/provider/server/active_sourced_track.dart';
|
||||
import 'package:spotube/provider/server/sourced_track.dart';
|
||||
import 'package:spotube/provider/user_preferences/user_preferences_provider.dart';
|
||||
import 'package:spotube/services/audio_player/audio_player.dart';
|
||||
import 'package:spotube/services/logger/logger.dart';
|
||||
import 'package:spotube/services/sourced_track/enums.dart';
|
||||
import 'package:spotube/services/sourced_track/sourced_track.dart';
|
||||
import 'package:path_provider/path_provider.dart' as paths;
|
||||
import 'package:spotube/utils/service_utils.dart';
|
||||
|
||||
class ServerPlaybackRoutes {
|
||||
final Ref ref;
|
||||
@ -18,19 +32,140 @@ class ServerPlaybackRoutes {
|
||||
|
||||
ServerPlaybackRoutes(this.ref) : dio = Dio();
|
||||
|
||||
/// @get('/stream/<trackId>')
|
||||
Future<Response> getStreamTrackId(Request request, String trackId) async {
|
||||
final options = Options(
|
||||
Future<({dio_lib.Response<Uint8List> response, Uint8List? bytes})>
|
||||
streamTrack(
|
||||
SourcedTrack track,
|
||||
Map<String, dynamic> headers,
|
||||
) async {
|
||||
final trackCacheFile = File(
|
||||
join(
|
||||
await paths.getApplicationCacheDirectory().then((value) => value.path),
|
||||
'cached_tracks',
|
||||
'${track.name} - ${track.artists?.asString()} (${track.sourceInfo.id}).${track.codec.name}',
|
||||
),
|
||||
);
|
||||
final trackPartialCacheFile = File("${trackCacheFile.path}.part");
|
||||
|
||||
var options = Options(
|
||||
headers: {
|
||||
...request.headers,
|
||||
...headers,
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
|
||||
"Cache-Control": "max-age=0",
|
||||
"Connection": "keep-alive",
|
||||
"host": Uri.parse(track.url).host,
|
||||
},
|
||||
responseType: ResponseType.stream,
|
||||
responseType: ResponseType.bytes,
|
||||
validateStatus: (status) => status! < 400,
|
||||
);
|
||||
|
||||
final headersRes = await Future<dio_lib.Response?>.value(
|
||||
dio.head(
|
||||
track.url,
|
||||
options: options,
|
||||
),
|
||||
).catchError((_) async => null);
|
||||
|
||||
final contentLength = headersRes?.headers.value("content-length");
|
||||
|
||||
if (await trackCacheFile.exists()) {
|
||||
final bytes = await trackCacheFile.readAsBytes();
|
||||
final cachedFileLength = bytes.length;
|
||||
|
||||
return (
|
||||
response: dio_lib.Response<Uint8List>(
|
||||
statusCode: 200,
|
||||
headers: Headers.fromMap({
|
||||
"content-type": ["audio/${track.codec.name}"],
|
||||
"content-length": ["$cachedFileLength"],
|
||||
"accept-ranges": ["bytes"],
|
||||
"content-range": ["bytes 0-$cachedFileLength/$cachedFileLength"],
|
||||
}),
|
||||
requestOptions: RequestOptions(path: track.url),
|
||||
),
|
||||
bytes: bytes,
|
||||
);
|
||||
}
|
||||
|
||||
/// Forcing partial content range as mpv sometimes greedily wants
|
||||
/// everything at one go. Slows down overall streaming.
|
||||
final range = RangeHeader.parse(headers["range"] ?? "");
|
||||
final contentPartialLength = int.tryParse(contentLength ?? "");
|
||||
if ((range.end == null) &&
|
||||
contentPartialLength != null &&
|
||||
range.start == 0) {
|
||||
options = options.copyWith(
|
||||
headers: {
|
||||
...?options.headers,
|
||||
"range": "$range${(contentPartialLength * 0.3).ceil()}",
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
final res =
|
||||
await dio.get<Uint8List>(track.url, options: options).catchError(
|
||||
(e, stack) async {
|
||||
final sourcedTrack = await ref
|
||||
.read(sourcedTrackProvider(SpotubeMedia(track)).notifier)
|
||||
.switchToAlternativeSources();
|
||||
|
||||
ref.read(activeSourcedTrackProvider.notifier).update(sourcedTrack);
|
||||
|
||||
return await dio.get<Uint8List>(sourcedTrack!.url, options: options);
|
||||
},
|
||||
);
|
||||
|
||||
final bytes = res.data;
|
||||
|
||||
if (bytes == null) {
|
||||
return (response: res, bytes: null);
|
||||
}
|
||||
|
||||
final contentRange =
|
||||
ContentRangeHeader.parse(res.headers.value("content-range") ?? "");
|
||||
|
||||
if (!await trackPartialCacheFile.exists()) {
|
||||
await trackPartialCacheFile.create(recursive: true);
|
||||
}
|
||||
|
||||
// Write the stream to the file based on the range
|
||||
final partialCacheFile =
|
||||
await trackPartialCacheFile.open(mode: FileMode.writeOnlyAppend);
|
||||
int fileLength = 0;
|
||||
try {
|
||||
await partialCacheFile.setPosition(contentRange.start);
|
||||
await partialCacheFile.writeFrom(bytes);
|
||||
fileLength = await partialCacheFile.length();
|
||||
} finally {
|
||||
await partialCacheFile.close();
|
||||
}
|
||||
|
||||
if (fileLength == contentRange.total) {
|
||||
await trackPartialCacheFile.rename(trackCacheFile.path);
|
||||
}
|
||||
|
||||
if (contentRange.total == fileLength && track.codec != SourceCodecs.weba) {
|
||||
final imageBytes = await ServiceUtils.downloadImage(
|
||||
(track.album?.images).asUrlString(
|
||||
placeholder: ImagePlaceholder.albumArt,
|
||||
index: 1,
|
||||
),
|
||||
);
|
||||
|
||||
await MetadataGod.writeMetadata(
|
||||
file: trackCacheFile.path,
|
||||
metadata: track.toMetadata(
|
||||
fileLength: fileLength,
|
||||
imageBytes: imageBytes,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return (bytes: bytes, response: res);
|
||||
}
|
||||
|
||||
/// @get('/stream/<trackId>')
|
||||
Future<Response> getStreamTrackId(Request request, String trackId) async {
|
||||
try {
|
||||
final track =
|
||||
playlist.tracks.firstWhere((element) => element.id == trackId);
|
||||
@ -41,48 +176,13 @@ class ServerPlaybackRoutes {
|
||||
: await ref.read(sourcedTrackProvider(SpotubeMedia(track)).future);
|
||||
|
||||
ref.read(activeSourcedTrackProvider.notifier).update(sourcedTrack);
|
||||
final res = await dio
|
||||
.get(
|
||||
sourcedTrack!.url,
|
||||
options: options.copyWith(
|
||||
headers: {
|
||||
...options.headers!,
|
||||
"host": Uri.parse(sourcedTrack.url).host,
|
||||
},
|
||||
),
|
||||
)
|
||||
.catchError((e, stack) async {
|
||||
final sourcedTrack = await ref
|
||||
.read(sourcedTrackProvider(SpotubeMedia(track)).notifier)
|
||||
.switchToAlternativeSources();
|
||||
|
||||
ref.read(activeSourcedTrackProvider.notifier).update(sourcedTrack);
|
||||
|
||||
return await dio.get(
|
||||
sourcedTrack!.url,
|
||||
options: options.copyWith(
|
||||
headers: {
|
||||
...options.headers!,
|
||||
"host": Uri.parse(sourcedTrack.url).host,
|
||||
},
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
final audioStream =
|
||||
(res.data?.stream as Stream<Uint8List>?)?.asBroadcastStream();
|
||||
|
||||
audioStream!.listen(
|
||||
(event) {},
|
||||
cancelOnError: true,
|
||||
);
|
||||
final (bytes: audioBytes, response: res) =
|
||||
await streamTrack(sourcedTrack!, request.headers);
|
||||
|
||||
return Response(
|
||||
res.statusCode!,
|
||||
body: audioStream,
|
||||
context: {
|
||||
"shelf.io.buffer_output": false,
|
||||
},
|
||||
body: audioBytes,
|
||||
headers: res.headers.map,
|
||||
);
|
||||
} catch (e, stack) {
|
||||
|
@ -1,4 +1,7 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:html/dom.dart' hide Text;
|
||||
import 'package:spotify/spotify.dart';
|
||||
@ -8,6 +11,7 @@ import 'package:spotube/modules/root/update_dialog.dart';
|
||||
import 'package:spotube/models/lyrics.dart';
|
||||
import 'package:spotube/provider/database/database.dart';
|
||||
import 'package:spotube/services/dio/dio.dart';
|
||||
import 'package:spotube/services/logger/logger.dart';
|
||||
import 'package:spotube/services/sourced_track/sourced_track.dart';
|
||||
|
||||
import 'package:spotube/utils/primitive_utils.dart';
|
||||
@ -449,4 +453,27 @@ abstract class ServiceUtils {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Spotify Images are always JPEGs
|
||||
static Future<Uint8List?> downloadImage(
|
||||
String imageUrl,
|
||||
) async {
|
||||
try {
|
||||
final fileStream = DefaultCacheManager().getImageFile(imageUrl);
|
||||
|
||||
final bytes = List<int>.empty(growable: true);
|
||||
|
||||
await for (final data in fileStream) {
|
||||
if (data is FileInfo) {
|
||||
bytes.addAll(data.file.readAsBytesSync());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return Uint8List.fromList(bytes);
|
||||
} catch (e, stackTrace) {
|
||||
AppLogger.reportError(e, stackTrace);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user