Skip to content

Commit

Permalink
refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
ArunPrakashG committed Sep 8, 2024
1 parent 2793eb0 commit 76ed8c6
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions lib/src/requests/create/create_media.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,43 +188,49 @@ final class CreateMediaRequest extends IRequest {
/// setting up the necessary headers and body, and returning a WordpressRequest object.
@override
Future<WordpressRequest> build(Uri baseUrl) async {
late MultipartFile multipartFile;
late String mimeType;
MultipartFile? multipartFile;
String? mimeType;

switch (mediaFile) {
case final File file:
if (!await file.exists()) {
throw FileDoesntExistException(
'The file at path "${file.path}" doesn\'t exist.',
);
}

final mediaType = getMIMETypeFromExtension(
extension(fileName).replaceFirst('.', ''),
if (mediaFile is File) {
final file = mediaFile as File;
if (!await file.exists()) {
throw FileDoesntExistException(
'The file at path "${file.path}" doesn\'t exist.',
);
}

multipartFile = await MultipartFile.fromFile(
file.path,
filename: fileName,
contentType: DioMediaType.parse(mediaType),
);
mimeType = mediaType;
break;
case final Uint8List bytes:
final mediaType = getMIMETypeFromExtension(
extension(fileName).replaceFirst('.', ''),
);
multipartFile = MultipartFile.fromBytes(
bytes,
filename: fileName,
contentType: DioMediaType.parse(mediaType),
);
mimeType = mediaType;
break;
default:
final mediaType = getMIMETypeFromExtension(
extension(fileName).replaceFirst('.', ''),
);

multipartFile = await MultipartFile.fromFile(
file.path,
filename: fileName,
contentType: DioMediaType.parse(mediaType),
);
mimeType = mediaType;
} else if (mediaFile is Uint8List) {
final bytes = mediaFile as Uint8List;

if (!fileName.contains('.')) {
throw ArgumentError(
'Invalid mediaFile type. Expected File or Uint8List.',
'The file name must contain a file extension.',
);
}

final mediaType = getMIMETypeFromExtension(
extension(fileName).replaceFirst('.', ''),
);
multipartFile = MultipartFile.fromBytes(
bytes,
filename: fileName,
contentType: DioMediaType.parse(mediaType),
);
mimeType = mediaType;
} else {
throw ArgumentError(
'Invalid content type for the file. Please use the `fromBytes` factory method to upload files with custom content types.',
);
}

final body = <String, dynamic>{}
Expand All @@ -243,7 +249,7 @@ final class CreateMediaRequest extends IRequest {
final headers = <String, dynamic>{}
..addIfNotNull(
'Content-Disposition',
'attachment; filename="$fileName"',
'attachment; filename="${multipartFile.filename}"',
)
..addIfNotNull('Content-Type', mimeType)
..addAllIfNotNull(this.headers);
Expand Down

0 comments on commit 76ed8c6

Please sign in to comment.