@@ -28,9 +28,13 @@ import com.imagekit.api.core.http.Headers
28
28
import com.imagekit.api.core.http.QueryParams
29
29
import com.imagekit.api.core.toImmutable
30
30
import com.imagekit.api.errors.ImageKitInvalidDataException
31
+ import java.io.InputStream
32
+ import java.nio.file.Path
31
33
import java.util.Collections
32
34
import java.util.Objects
33
35
import java.util.Optional
36
+ import kotlin.io.path.inputStream
37
+ import kotlin.io.path.name
34
38
import kotlin.jvm.optionals.getOrNull
35
39
36
40
/* *
@@ -74,7 +78,7 @@ private constructor(
74
78
* @throws ImageKitInvalidDataException if the JSON field has an unexpected type or is
75
79
* unexpectedly missing or null (e.g. if the server responded with an unexpected value).
76
80
*/
77
- fun file (): String = body.file()
81
+ fun file (): InputStream = body.file()
78
82
79
83
/* *
80
84
* The name with which the file has to be uploaded. The file name can contain:
@@ -323,7 +327,7 @@ private constructor(
323
327
*
324
328
* Unlike [file], this method doesn't throw if the multipart field has an unexpected type.
325
329
*/
326
- fun _file (): MultipartField <String > = body._file ()
330
+ fun _file (): MultipartField <InputStream > = body._file ()
327
331
328
332
/* *
329
333
* Returns the raw multipart value of [fileName].
@@ -552,15 +556,38 @@ private constructor(
552
556
* When supplying a URL, the server must receive the response headers within 8 seconds;
553
557
* otherwise the request fails with 400 Bad Request.
554
558
*/
555
- fun file (file : String ) = apply { body.file(file) }
559
+ fun file (file : InputStream ) = apply { body.file(file) }
556
560
557
561
/* *
558
562
* Sets [Builder.file] to an arbitrary multipart value.
559
563
*
560
- * You should usually call [Builder.file] with a well-typed [String] value instead. This
561
- * method is primarily for setting the field to an undocumented or not yet supported value.
564
+ * You should usually call [Builder.file] with a well-typed [InputStream] value instead.
565
+ * This method is primarily for setting the field to an undocumented or not yet supported
566
+ * value.
567
+ */
568
+ fun file (file : MultipartField <InputStream >) = apply { body.file(file) }
569
+
570
+ /* *
571
+ * The API accepts any of the following:
572
+ * - **Binary data** – send the raw bytes as `multipart/form-data`.
573
+ * - **HTTP / HTTPS URL** – a publicly reachable URL that ImageKit’s servers can fetch.
574
+ * - **Base64 string** – the file encoded as a Base64 data URI or plain Base64.
575
+ *
576
+ * When supplying a URL, the server must receive the response headers within 8 seconds;
577
+ * otherwise the request fails with 400 Bad Request.
578
+ */
579
+ fun file (file : ByteArray ) = apply { body.file(file) }
580
+
581
+ /* *
582
+ * The API accepts any of the following:
583
+ * - **Binary data** – send the raw bytes as `multipart/form-data`.
584
+ * - **HTTP / HTTPS URL** – a publicly reachable URL that ImageKit’s servers can fetch.
585
+ * - **Base64 string** – the file encoded as a Base64 data URI or plain Base64.
586
+ *
587
+ * When supplying a URL, the server must receive the response headers within 8 seconds;
588
+ * otherwise the request fails with 400 Bad Request.
562
589
*/
563
- fun file (file : MultipartField < String > ) = apply { body.file(file ) }
590
+ fun file (path : Path ) = apply { body.file(path ) }
564
591
565
592
/* *
566
593
* The name with which the file has to be uploaded. The file name can contain:
@@ -1170,7 +1197,7 @@ private constructor(
1170
1197
1171
1198
class Body
1172
1199
private constructor (
1173
- private val file: MultipartField <String >,
1200
+ private val file: MultipartField <InputStream >,
1174
1201
private val fileName: MultipartField <String >,
1175
1202
private val token: MultipartField <String >,
1176
1203
private val checks: MultipartField <String >,
@@ -1208,7 +1235,7 @@ private constructor(
1208
1235
* @throws ImageKitInvalidDataException if the JSON field has an unexpected type or is
1209
1236
* unexpectedly missing or null (e.g. if the server responded with an unexpected value).
1210
1237
*/
1211
- fun file (): String = file.value.getRequired(" file" )
1238
+ fun file (): InputStream = file.value.getRequired(" file" )
1212
1239
1213
1240
/* *
1214
1241
* The name with which the file has to be uploaded. The file name can contain:
@@ -1465,7 +1492,7 @@ private constructor(
1465
1492
*
1466
1493
* Unlike [file], this method doesn't throw if the multipart field has an unexpected type.
1467
1494
*/
1468
- @JsonProperty(" file" ) @ExcludeMissing fun _file (): MultipartField <String > = file
1495
+ @JsonProperty(" file" ) @ExcludeMissing fun _file (): MultipartField <InputStream > = file
1469
1496
1470
1497
/* *
1471
1498
* Returns the raw multipart value of [fileName].
@@ -1699,7 +1726,7 @@ private constructor(
1699
1726
/* * A builder for [Body]. */
1700
1727
class Builder internal constructor() {
1701
1728
1702
- private var file: MultipartField <String >? = null
1729
+ private var file: MultipartField <InputStream >? = null
1703
1730
private var fileName: MultipartField <String >? = null
1704
1731
private var token: MultipartField <String > = MultipartField .of(null )
1705
1732
private var checks: MultipartField <String > = MultipartField .of(null )
@@ -1761,16 +1788,44 @@ private constructor(
1761
1788
* When supplying a URL, the server must receive the response headers within 8 seconds;
1762
1789
* otherwise the request fails with 400 Bad Request.
1763
1790
*/
1764
- fun file (file : String ) = file(MultipartField .of(file))
1791
+ fun file (file : InputStream ) = file(MultipartField .of(file))
1765
1792
1766
1793
/* *
1767
1794
* Sets [Builder.file] to an arbitrary multipart value.
1768
1795
*
1769
- * You should usually call [Builder.file] with a well-typed [String] value instead. This
1770
- * method is primarily for setting the field to an undocumented or not yet supported
1771
- * value.
1796
+ * You should usually call [Builder.file] with a well-typed [InputStream] value instead.
1797
+ * This method is primarily for setting the field to an undocumented or not yet
1798
+ * supported value.
1799
+ */
1800
+ fun file (file : MultipartField <InputStream >) = apply { this .file = file }
1801
+
1802
+ /* *
1803
+ * The API accepts any of the following:
1804
+ * - **Binary data** – send the raw bytes as `multipart/form-data`.
1805
+ * - **HTTP / HTTPS URL** – a publicly reachable URL that ImageKit’s servers can fetch.
1806
+ * - **Base64 string** – the file encoded as a Base64 data URI or plain Base64.
1807
+ *
1808
+ * When supplying a URL, the server must receive the response headers within 8 seconds;
1809
+ * otherwise the request fails with 400 Bad Request.
1810
+ */
1811
+ fun file (file : ByteArray ) = file(file.inputStream())
1812
+
1813
+ /* *
1814
+ * The API accepts any of the following:
1815
+ * - **Binary data** – send the raw bytes as `multipart/form-data`.
1816
+ * - **HTTP / HTTPS URL** – a publicly reachable URL that ImageKit’s servers can fetch.
1817
+ * - **Base64 string** – the file encoded as a Base64 data URI or plain Base64.
1818
+ *
1819
+ * When supplying a URL, the server must receive the response headers within 8 seconds;
1820
+ * otherwise the request fails with 400 Bad Request.
1772
1821
*/
1773
- fun file (file : MultipartField <String >) = apply { this .file = file }
1822
+ fun file (path : Path ) =
1823
+ file(
1824
+ MultipartField .builder<InputStream >()
1825
+ .value(path.inputStream())
1826
+ .filename(path.name)
1827
+ .build()
1828
+ )
1774
1829
1775
1830
/* *
1776
1831
* The name with which the file has to be uploaded. The file name can contain:
0 commit comments