From 2d0674206551f96a036a33aef00b06f15c2ff973 Mon Sep 17 00:00:00 2001
From: xuty <xty50337@hotmail.com>
Date: Sat, 14 Aug 2021 00:31:54 +0000
Subject: [PATCH] change port field to final

---
 lib/src/minio.dart        | 49 +++++++++++++++++++++++++++------------
 lib/src/minio_client.dart |  8 +++----
 2 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/lib/src/minio.dart b/lib/src/minio.dart
index cae7473..e2afb86 100644
--- a/lib/src/minio.dart
+++ b/lib/src/minio.dart
@@ -1,13 +1,13 @@
 import 'dart:async';
 import 'dart:convert';
 
-import 'package:http/http.dart';
 import 'package:minio/models.dart';
 import 'package:minio/src/minio_client.dart';
 import 'package:minio/src/minio_errors.dart';
 import 'package:minio/src/minio_helpers.dart';
 import 'package:minio/src/minio_poller.dart';
 import 'package:minio/src/minio_sign.dart';
+import 'package:minio/src/minio_stream.dart';
 import 'package:minio/src/minio_uploader.dart';
 import 'package:minio/src/utils.dart';
 import 'package:xml/xml.dart' as xml;
@@ -19,16 +19,27 @@ import 'minio_helpers.dart';
 class Minio {
   /// Initializes a new client object.
   Minio({
-    this.endPoint,
-    this.port,
-    this.useSSL = true,
+    required this.endPoint,
     required this.accessKey,
     required this.secretKey,
+    int? port,
+    this.useSSL = true,
     this.sessionToken,
     this.region,
     this.enableTrace = false,
-  })  : assert(isValidEndpoint(endPoint)),
-        assert(port == null || isValidPort(port)) {
+  }) : port = port ?? implyPort(useSSL) {
+    if (!isValidEndpoint(endPoint)) {
+      throw MinioInvalidEndpointError(
+        'End point $endPoint is not a valid domain or ip address',
+      );
+    }
+
+    if (!isValidPort(this.port)) {
+      throw MinioInvalidPortError(
+        'Invalid port number ${this.port}',
+      );
+    }
+
     _client = MinioClient(this);
   }
 
@@ -42,10 +53,14 @@ class Minio {
   final maxObjectSize = 5 * 1024 * 1024 * 1024 * 1024;
 
   /// endPoint is a host name or an IP address.
-  final String? endPoint;
+  ///
+  /// For example:
+  /// - play.min.io
+  /// - 1.2.3.4
+  final String endPoint;
 
   /// TCP/IP port number. This input is optional. Default value set to 80 for HTTP and 443 for HTTPs.
-  final int? port;
+  final int port;
 
   /// If set to true, https is used instead of http. Default is true.
   final bool useSSL;
@@ -296,14 +311,12 @@ class Minio {
   }
 
   /// get a readable stream of the object content.
-  Future<ByteStream> getObject(String bucket, String object) {
-    MinioInvalidBucketNameError.check(bucket);
-    MinioInvalidObjectNameError.check(object);
+  Future<MinioByteStream> getObject(String bucket, String object) {
     return getPartialObject(bucket, object, null, null);
   }
 
   /// get a readable stream of the partial object content.
-  Future<ByteStream> getPartialObject(
+  Future<MinioByteStream> getPartialObject(
     String bucket,
     String object, [
     int? offset,
@@ -339,7 +352,11 @@ class Minio {
     );
 
     await validateStreamed(resp, expect: expectedStatus);
-    return resp.stream;
+
+    return MinioByteStream.fromStream(
+      stream: resp.stream,
+      contentLength: resp.contentLength,
+    );
   }
 
   /// Initiate a new multipart upload.
@@ -782,7 +799,7 @@ class Minio {
         .getBaseRequest('POST', postPolicy.formData['bucket'], null, region,
             null, null, null)
         .url;
-    var portStr = (port == 80 || port == 443 || port == null) ? '' : ':$port';
+    var portStr = (port == 80 || port == 443) ? '' : ':$port';
     var urlStr = '${url.scheme}://${url.host}$portStr${url.path}';
     return PostPolicyResult(postURL: urlStr, formData: postPolicy.formData);
   }
@@ -824,7 +841,9 @@ class Minio {
     MinioInvalidBucketNameError.check(bucket);
     MinioInvalidObjectNameError.check(object);
 
-    assert(expires == null || expires >= 0);
+    if (expires != null && expires < 0) {
+      throw MinioInvalidArgumentError('invalid expire time value: $expires');
+    }
 
     expires ??= expires = 24 * 60 * 60 * 7; // 7 days in seconds
     reqParams ??= {};
diff --git a/lib/src/minio_client.dart b/lib/src/minio_client.dart
index 0c3acaa..4287ce5 100644
--- a/lib/src/minio_client.dart
+++ b/lib/src/minio_client.dart
@@ -47,15 +47,15 @@ class MinioClient {
   MinioClient(this.minio) {
     anonymous = minio.accessKey.isEmpty && minio.secretKey.isEmpty;
     enableSHA256 = !anonymous && !minio.useSSL;
-    port = minio.port ?? implyPort(minio.useSSL);
+    port = minio.port;
   }
 
   final Minio minio;
-  final String userAgent = 'MinIO (Unknown; Unknown) minio-dart/0.1.9';
+  final String userAgent = 'MinIO (Unknown; Unknown) minio-dart/2.0.0';
 
   late bool enableSHA256;
   late bool anonymous;
-  int? port;
+  late final int port;
 
   Future<StreamedResponse> _request({
     required String method,
@@ -171,7 +171,7 @@ class MinioClient {
     String? resource,
     Map<String, dynamic>? queries,
   ) {
-    var host = minio.endPoint!.toLowerCase();
+    var host = minio.endPoint.toLowerCase();
     var path = '/';
 
     if (isAmazonEndpoint(host)) {
-- 
GitLab