Skip to content

Commit

Permalink
fix: change sendfile to sendbytestream [#79] (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyford authored Jan 9, 2025
1 parent ed332f9 commit 5bfd9ba
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 35 deletions.
12 changes: 9 additions & 3 deletions packages/clerk_auth/lib/src/clerk_api/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,16 @@ class Api with Logging {
try {
final queryParams = _queryParams(HttpMethod.post, withSession: true);
final uri = _uri('/me/profile_image', queryParams);

final length = await file.length();
final headers = _headers(HttpMethod.post);

final resp = await _client.sendFile(HttpMethod.post, uri, file, headers);
final stream = http.ByteStream(file.openRead());
final resp = await _client.sendByteStream(
HttpMethod.post,
uri,
stream,
length,
headers,
);
return _processResponse(resp);
} catch (error, stacktrace) {
logSevere('Error during fetch', error, stacktrace);
Expand Down
31 changes: 10 additions & 21 deletions packages/clerk_auth/lib/src/clerk_auth/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:io';

import 'package:clerk_auth/clerk_auth.dart';
import 'package:http/http.dart';
import 'package:http_parser/http_parser.dart';

/// Enum detailing [HttpMethod]s used by the Clerk API
enum HttpMethod {
Expand Down Expand Up @@ -49,10 +48,11 @@ abstract class HttpClient {

/// Upload a [File] to the back end, and receive a [Response]
///
Future<Response> sendFile(
Future<Response> sendByteStream(
HttpMethod method,
Uri uri,
File file,
ByteStream byteStream,
int length,
Map<String, String> headers,
);
}
Expand All @@ -63,8 +63,6 @@ class DefaultHttpClient implements HttpClient {
/// Constructor
const DefaultHttpClient();

static const _imageExts = ['png', 'webp'];

@override
Future<Response> send(
HttpMethod method,
Expand All @@ -87,34 +85,25 @@ class DefaultHttpClient implements HttpClient {
}

@override
Future<Response> sendFile(
Future<Response> sendByteStream(
HttpMethod method,
Uri uri,
File file,
ByteStream byteStream,
int length,
Map<String, String> headers,
) async {
final request = MultipartRequest(method.toString(), uri);
request.headers.addAll(headers);

final multipartFile = await MultipartFile.fromPath(
final multipartFile = MultipartFile(
'file',
file.path,
contentType: _contentTypeFor(file),
byteStream,
length,
filename: byteStream.hashCode.toString(),
);
request.files.add(multipartFile);

final streamedResponse = await request.send();
return Response.fromStream(streamedResponse);
}

MediaType _contentTypeFor(File file) {
return switch (file.path.split('.').last.toLowerCase()) {
'jpg' => MediaType('image', 'jpeg'),
String ext when _imageExts.contains(ext) => MediaType('image', ext),
String ext => throw AuthError(
message: 'Unknown media type for upload: ###',
substitution: ext,
),
};
}
}
1 change: 0 additions & 1 deletion packages/clerk_auth/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ environment:
dependencies:
collection: ^1.17.1
http: ^1.1.0
http_parser: ^4.0.2
json_annotation: ^4.9.0
logging: ^1.2.0

Expand Down
7 changes: 4 additions & 3 deletions packages/clerk_auth/test/test_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:io';
import 'package:clerk_auth/src/clerk_auth/http_client.dart';
import 'package:clerk_auth/src/utils/logging.dart';
import 'package:dart_dotenv/dart_dotenv.dart';
import 'package:http/http.dart' show Response;
import 'package:http/http.dart' show ByteStream, Response;

class TestEnv {
TestEnv._(this._map);
Expand Down Expand Up @@ -99,10 +99,11 @@ class TestHttpClient implements HttpClient {
map.entries.map((me) => '${me.key}=${me.value}').join('&');

@override
Future<Response> sendFile(
Future<Response> sendByteStream(
HttpMethod method,
Uri uri,
File file,
ByteStream stream,
int length,
Map<String, String> headers,
) {
// TODO: add tests for sendFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class _ClerkSignInPanelState extends State<ClerkSignInPanel> {
children: [
if (env.hasPasswordStrategy)
Padding(
padding: horizontalPadding32 + verticalPadding8,
padding: verticalPadding8,
child: ClerkTextFormField(
label: translator.translate('Password'),
obscureText: true,
Expand All @@ -141,15 +141,12 @@ class _ClerkSignInPanelState extends State<ClerkSignInPanel> {
),
),
if (env.hasOtherStrategies) ...[
if (env.hasPasswordStrategy)
const Padding(
padding: horizontalPadding32,
child: OrDivider(),
),
if (env.hasPasswordStrategy) //
const OrDivider(),
for (final strategy in env.otherStrategies)
if (StrategyButton.supports(strategy))
Padding(
padding: topPadding4 + horizontalPadding32,
padding: topPadding4,
child: StrategyButton(
key: ValueKey<clerk.Strategy>(strategy),
strategy: strategy,
Expand Down

0 comments on commit 5bfd9ba

Please sign in to comment.