From 68cc740251570d9ef64590d1b3f76d2574ae612e Mon Sep 17 00:00:00 2001 From: NaiJi Date: Sat, 4 Feb 2023 11:23:51 +0400 Subject: [PATCH] fix: Improve path sanitizing by replacing dash --- lib/funkwhale.dart | 11 +++++++---- lib/parser.dart | 6 ++---- lib/string_utils.dart | 6 ++++++ 3 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 lib/string_utils.dart diff --git a/lib/funkwhale.dart b/lib/funkwhale.dart index 4a416b9..c0d1fa6 100644 --- a/lib/funkwhale.dart +++ b/lib/funkwhale.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:dio/dio.dart'; import 'package:funkblubber/funkentity.dart'; import 'package:funkblubber/console.dart' as console; +import 'package:funkblubber/string_utils.dart' as utils; Future download({ required final FunkObject object, @@ -40,7 +41,7 @@ Future _downloadArtist( 'refresh=false', ); - final String pathAppend = response.data['name']; + final String pathAppend = utils.sanitizePath(response.data['name']); bool folderCreated = true; try { await Directory('$path/$pathAppend').create(); @@ -87,7 +88,9 @@ Future _downloadAlbum( '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; try { await Directory('$path/$pathAppend').create(); @@ -102,7 +105,7 @@ Future _downloadAlbum( final List> 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 result = _downloadTrackObject( FunkObject( @@ -128,7 +131,7 @@ Future _downloadTrack( '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']; return _downloadTrackObject( FunkObject( diff --git a/lib/parser.dart b/lib/parser.dart index 33e106f..3df777c 100644 --- a/lib/parser.dart +++ b/lib/parser.dart @@ -1,5 +1,6 @@ import 'package:funkblubber/funkentity.dart'; import 'package:funkblubber/console.dart' as console; +import 'package:funkblubber/string_utils.dart' as utils; enum Action { download, @@ -203,9 +204,6 @@ StageResult _onDomainStage( return StageResult(result: result, stage: currentStage); } -String _cutTrailingDash(final String path) => - path.endsWith('/') ? path.substring(0, path.length - 1) : path; - StageResult _onPathStage( final String arg, final ParseResult previousResult, @@ -216,7 +214,7 @@ StageResult _onPathStage( currentStage = ParsingStage.nothing; result = ParseResult( - localPath: _cutTrailingDash(arg), + localPath: utils.cutTrailingDash(arg), action: previousResult.action, success: true, object: previousResult.object, diff --git a/lib/string_utils.dart b/lib/string_utils.dart new file mode 100644 index 0000000..ee02096 --- /dev/null +++ b/lib/string_utils.dart @@ -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('/', '\\');