fix: Improve path sanitizing by replacing dash

This commit is contained in:
NaiJi ✨ 2023-02-04 11:23:51 +04:00
parent 87117f2e9e
commit 68cc740251
3 changed files with 15 additions and 8 deletions

View File

@ -3,6 +3,7 @@ import 'dart:io';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:funkblubber/funkentity.dart'; import 'package:funkblubber/funkentity.dart';
import 'package:funkblubber/console.dart' as console; import 'package:funkblubber/console.dart' as console;
import 'package:funkblubber/string_utils.dart' as utils;
Future<bool> download({ Future<bool> download({
required final FunkObject object, required final FunkObject object,
@ -40,7 +41,7 @@ Future<bool> _downloadArtist(
'refresh=false', 'refresh=false',
); );
final String pathAppend = response.data['name']; final String pathAppend = utils.sanitizePath(response.data['name']);
bool folderCreated = true; bool folderCreated = true;
try { try {
await Directory('$path/$pathAppend').create(); await Directory('$path/$pathAppend').create();
@ -87,7 +88,9 @@ Future<bool> _downloadAlbum(
'page=1&page_size=16&scope=all', 'page=1&page_size=16&scope=all',
); );
final String pathAppend = response.data['results'][0]['album']['title']; final String pathAppend = utils.sanitizePath(
response.data['results'][0]['album']['title'],
);
bool folderCreated = true; bool folderCreated = true;
try { try {
await Directory('$path/$pathAppend').create(); await Directory('$path/$pathAppend').create();
@ -102,7 +105,7 @@ Future<bool> _downloadAlbum(
final List<Future<bool>> results = []; final List<Future<bool>> results = [];
for (final songResponse in response.data['results']) { for (final songResponse in response.data['results']) {
final String songTitle = songResponse['title']; final String songTitle = utils.sanitizePath(songResponse['title']);
final String ext = songResponse['uploads'][0]['extension']; final String ext = songResponse['uploads'][0]['extension'];
final result = _downloadTrackObject( final result = _downloadTrackObject(
FunkObject( FunkObject(
@ -128,7 +131,7 @@ Future<bool> _downloadTrack(
'https://${object.domain}/api/v1/tracks/${object.id}/?refresh=false', 'https://${object.domain}/api/v1/tracks/${object.id}/?refresh=false',
); );
final String songTitle = response.data['title']; final String songTitle = utils.sanitizePath(response.data['title']);
final String ext = response.data['uploads'][0]['extension']; final String ext = response.data['uploads'][0]['extension'];
return _downloadTrackObject( return _downloadTrackObject(
FunkObject( FunkObject(

View File

@ -1,5 +1,6 @@
import 'package:funkblubber/funkentity.dart'; import 'package:funkblubber/funkentity.dart';
import 'package:funkblubber/console.dart' as console; import 'package:funkblubber/console.dart' as console;
import 'package:funkblubber/string_utils.dart' as utils;
enum Action { enum Action {
download, download,
@ -203,9 +204,6 @@ StageResult _onDomainStage(
return StageResult(result: result, stage: currentStage); return StageResult(result: result, stage: currentStage);
} }
String _cutTrailingDash(final String path) =>
path.endsWith('/') ? path.substring(0, path.length - 1) : path;
StageResult _onPathStage( StageResult _onPathStage(
final String arg, final String arg,
final ParseResult previousResult, final ParseResult previousResult,
@ -216,7 +214,7 @@ StageResult _onPathStage(
currentStage = ParsingStage.nothing; currentStage = ParsingStage.nothing;
result = ParseResult( result = ParseResult(
localPath: _cutTrailingDash(arg), localPath: utils.cutTrailingDash(arg),
action: previousResult.action, action: previousResult.action,
success: true, success: true,
object: previousResult.object, object: previousResult.object,

6
lib/string_utils.dart Normal file
View File

@ -0,0 +1,6 @@
String cutTrailingDash(final String path) =>
path.endsWith('/') && path.length > 1
? path.substring(0, path.length - 1)
: path;
String sanitizePath(final String rawPath) => rawPath.replaceAll('/', '\\');