mangayomi-mirror/lib/utils/extensions/string_extensions.dart
Mehakdeep Singh bbc7fa04f5 fix(downloads): unstall anime downloads on sources that return URLs with query strings or HTTP 206
Two related bugs left anime downloads stuck at 0% with no error visible to
the user. Manga downloads from clean HTTPS sources were unaffected.

1) lib/utils/extensions/string_extensions.dart

   isMediaVideo() did a plain endsWith on the full URL string, so URLs
   shaped like https://host/play/{id}/video.mp4?for={token} (used by
   AnimeGG and several other sources) failed the filter because of the
   trailing `?for=...` query string. With both m3u8Urls and nonM3u8Urls
   empty in downloadChapter, the surrounding
   Future.doWhile(() => isOk == true) poll never sets isOk and waits
   forever -- MDownloader is never constructed.

   Fix: match against Uri.tryParse(this)?.path instead of the full
   string, and use a leading "." in the suffix so e.g. "flashmp4" cannot
   accidentally match.

2) lib/services/download_manager/download_isolate_pool.dart

   Once the URL passes the filter, _downloadFile (anime branch) opens a
   streaming request. When the source extension sets Range: bytes=0-,
   the server correctly responds with HTTP 206 Partial Content. The
   previous "if (response.statusCode != 200)" check rejected that,
   retried 3x, and threw. The throw was masked by an outer catch(_) in
   downloadChapter, so the user only saw a forever-spinner.

   Fix: accept any 2xx (>= 200 && < 300). Same fix applied to
   _downloadSegment for HLS segment fetches.

Repro
- Source: AnimeGG (en) -- install via Mangayomi extensions
- Pick any episode (tested with Toriko Ep 147, Gintama Ep 39, Grand Blue
  Ep 12)
- Tap the download icon

Before: an empty
".../AnimeGG (EN)/<series>/<episode>.mp4"-named folder is created, the
download icon stays in the spinner state, no error toast.

After: the .mp4 is written to disk at the size declared in Content-Length
(65,026,283 bytes for Toriko Ep 147), plays in the system video player.

Tested on macOS 26.3 / Apple Silicon with AnimeGG (multiple episodes,
multiple series, including a 180 MB 720p) and a manga control
(Asura Scans, 9 pages) on the same build to confirm no regression on the
manga path.
2026-05-09 19:10:35 -07:00

110 lines
2.8 KiB
Dart

import 'dart:ffi';
import 'package:ffi/ffi.dart';
extension StringExtensions on String {
String substringAfter(String pattern) {
final startIndex = indexOf(pattern);
if (startIndex == -1) return substring(0);
final start = startIndex + pattern.length;
return substring(start);
}
String substringAfterLast(String pattern) {
return split(pattern).last;
}
String substringBefore(String pattern) {
final endIndex = indexOf(pattern);
if (endIndex == -1) return substring(0);
return substring(0, endIndex);
}
String substringBeforeLast(String pattern) {
final endIndex = lastIndexOf(pattern);
if (endIndex == -1) return substring(0);
return substring(0, endIndex);
}
String substringBetween(String left, String right) {
int startIndex = 0;
int index = indexOf(left, startIndex);
if (index == -1) return "";
int leftIndex = index + left.length;
int rightIndex = indexOf(right, leftIndex);
if (rightIndex == -1) return "";
startIndex = rightIndex + right.length;
return substring(leftIndex, rightIndex);
}
String replaceForbiddenCharacters(String source) {
return replaceAll(
RegExp(r'[\\/:*?"<>|\0]|(^CON$|^PRN$|^AUX$|^NUL$|^COM[1-9]$|^LPT[1-9]$)'),
source,
);
}
String get getUrlWithoutDomain {
final uri = Uri.parse(replaceAll(' ', '%20'));
String out = uri.path;
if (uri.query.isNotEmpty) {
out += '?${uri.query}';
}
if (uri.fragment.isNotEmpty) {
out += '#${uri.fragment}';
}
return out;
}
bool isMediaVideo() {
// Match against the URL path only — query strings (e.g. AnimeGG's
// `?for=...`) and fragments must not defeat the suffix check. Use a
// leading `.` so e.g. `flashmp4` doesn't accidentally match.
final lower = (Uri.tryParse(this)?.path ?? this).toLowerCase();
return const [
"3gp",
"avi",
"mpg",
"mpeg",
"webm",
"ogg",
"flv",
"m4v",
"mvp",
"mp4",
"wmv",
"mkv",
"mov",
].any((extension) => lower.endsWith(".$extension"));
}
}
extension DefaultValueExtension on String? {
String? trimmedOrDefault(String? defaultValue) {
if (this?.trim().isNotEmpty ?? false) {
return this!.trim();
}
return defaultValue;
}
}
extension NativeStringExtensions on List<String> {
Pointer<Pointer<Int8>> strListToPointer() {
final strings = this;
List<Pointer<Int8>> int8PointerList = strings
.map((str) => str.toNativeUtf8().cast<Int8>())
.toList();
final Pointer<Pointer<Int8>> pointerPointer = malloc.allocate(
int8PointerList.length,
);
strings.asMap().forEach((index, utf) {
pointerPointer[index] = int8PointerList[index];
});
return pointerPointer;
}
}