Skip to content

Commit

Permalink
fix: download related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Nov 5, 2024
1 parent e4080fa commit 8024f5b
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 198 deletions.
4 changes: 2 additions & 2 deletions lib/controller/thumbnail_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class _YTThumbnailDownloadManager with PortsProvider<SendPort> {

if (forceRequest == false && _downloadCompleters[mapKey] != null) {
final res = await _downloadCompleters[mapKey]!.future;
_requestsCountForId.update(id, (value) => value - 1);
_requestsCountForId.update(id, (value) => value - 1, ifAbsent: () => 0);
if (res != null || _shouldRetry[id] != true) {
return res;
}
Expand All @@ -304,7 +304,7 @@ class _YTThumbnailDownloadManager with PortsProvider<SendPort> {
await sendPort(p);
final res = await _downloadCompleters[mapKey]?.future;

_requestsCountForId.update(id, (value) => value - 1);
_requestsCountForId.update(id, (value) => value - 1, ifAbsent: () => 0);
return res;
}

Expand Down
47 changes: 36 additions & 11 deletions lib/youtube/class/download_task_base.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
abstract class DownloadTask<T> {
const DownloadTask();

@override
String toString();
}

class DownloadTaskFilename extends DownloadTask {
class DownloadTaskFilename {
String filename;

String get key => hashCode.toString();
late String key;
late int _uniqueKey;

DownloadTaskFilename.create({
required String initialFilename,
}) : filename = initialFilename;
}) : filename = initialFilename {
_uniqueKey = hashCode ^ DateTime.now().microsecondsSinceEpoch.hashCode;
key = _uniqueKey.toString();
}

DownloadTaskFilename._({
required this.filename,
required String? key,
}) {
this._uniqueKey = (key == null ? null : int.tryParse(key)) ?? (hashCode ^ DateTime.now().microsecondsSinceEpoch);
this.key = _uniqueKey.toString();
}

@override
String toString() => filename;

Map<String, dynamic> toMap() {
return <String, dynamic>{
'filename': filename,
'key': key,
};
}

factory DownloadTaskFilename.fromMap(dynamic value) {
if (value is Map) {
return DownloadTaskFilename._(
filename: value['filename'] as String? ?? 'UNKNOWN_FILENAME',
key: value['key'] as String?,
);
}
// -- old string only
return DownloadTaskFilename._(
filename: value as String,
key: null,
);
}
}

class DownloadTaskVideoId {
Expand Down
5 changes: 3 additions & 2 deletions lib/youtube/class/youtube_item_download_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ class YoutubeItemDownloadConfig {
try {
streamInfoItem = StreamInfoItem.fromMap(map['streamInfoItem']);
} catch (_) {}

return YoutubeItemDownloadConfig(
id: DownloadTaskVideoId(videoId: map['id'] ?? 'UNKNOWN_ID'),
filename: DownloadTaskFilename.create(initialFilename: map['filename'] ?? 'UNKNOWN_FILENAME'),
filename: DownloadTaskFilename.fromMap(map['filename']),
groupName: DownloadTaskGroupName(groupName: map['groupName'] ?? ''),
fileDate: DateTime.fromMillisecondsSinceEpoch(map['fileDate'] ?? 0),
ffmpegTags: (map['ffmpegTags'] as Map<String, dynamic>?)?.cast() ?? {},
Expand All @@ -89,7 +90,7 @@ class YoutubeItemDownloadConfig {
return {
'id': id.videoId,
'groupName': groupName.groupName,
'filename': filename.filename,
'filename': filename.toMap(),
'ffmpegTags': ffmpegTags,
'fileDate': fileDate?.millisecondsSinceEpoch,
'videoStream': videoStream?.toMap(),
Expand Down
Loading

0 comments on commit 8024f5b

Please sign in to comment.