Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PE-4923: Introduces methods for getting file name and extension #19

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/src/utils/path_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ String getFileExtension({
}
}

String getFileTypeFromMime({required String contentType}) {
return contentType.substring(contentType.lastIndexOf('/') + 1);
}

String getBasenameWithoutExtension({required String filePath}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matibat , can you add the withoutExtension parameter and implement both options: with and without extension?

Copy link
Contributor Author

@matibat matibat Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to introduce a new separate method when needed. 🙏

return path.basenameWithoutExtension(filePath);
}

Future<String> _getDefaultIOSDir() async {
final iosDirectory = await path_provider.getApplicationDocumentsDirectory();
final iosDownloadsDirectory = Directory('${iosDirectory.path}/Downloads/');
Expand Down
60 changes: 57 additions & 3 deletions test/src/utils/path_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:ardrive_io/src/utils/path_utils.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('test method getFolderNameFromPath', () {
group('getFolderNameFromPath method', () {
test('should return correct folder name', () {
expect(getBasenameFromPath('some-folder'), 'some-folder');
expect(getBasenameFromPath('/some-folder'), 'some-folder');
Expand All @@ -17,7 +17,7 @@ void main() {
throwsA(const TypeMatcher<EntityPathException>()));
});
});
group('test method getDirname', () {
group('getDirname method', () {
test('should return correct dirname', () {
expect(getDirname('/some-folder/file'), '/some-folder');
expect(getDirname('/some-folder/path/file.png'), '/some-folder/path');
Expand All @@ -32,7 +32,7 @@ void main() {
});
});

group('test method getFileExtension', () {
group('getFileExtension method', () {
test('should return the extension from contentType and return with the .',
() {
/// .pdf
Expand Down Expand Up @@ -112,4 +112,58 @@ void main() {
expect(ext, 'conf');
});
});

group('getFileTypeFromMime method', () {
test('should return the file type from mimetype', () {
final type = getFileTypeFromMime(contentType: 'application/pdf');
expect(type, 'pdf');
});

test('should return the file type for a different mimetype', () {
final type = getFileTypeFromMime(contentType: 'image/jpeg');
expect(type, 'jpeg');
});

test('should return null for an empty string', () {
final type = getFileTypeFromMime(contentType: '');
expect(type, '');
});
});

group('getBasenameWithoutExtension method', () {
test('should return the basename without extension', () {
final basename = getBasenameWithoutExtension(
filePath: '/hola/que/tal/file.pdf',
);
expect(basename, 'file');
});

test('should return the basename without extension', () {
final basename = getBasenameWithoutExtension(
filePath: '/hola/que/tal/file.pdf.txt',
);
expect(basename, 'file.pdf');
});

test('should return the basename for a different file path', () {
final basename = getBasenameWithoutExtension(
filePath: '/another/path/to/different.file',
);
expect(basename, 'different');
});

test('should return the basename for a file path without extension', () {
final basename = getBasenameWithoutExtension(
filePath: '/path/to/file',
);
expect(basename, 'file');
});

test('should return an empty string for an empty string', () {
final basename = getBasenameWithoutExtension(
filePath: '',
);
expect(basename, '');
});
});
}