@@ -28,9 +28,13 @@ import com.imagekit.api.core.http.Headers
2828import com.imagekit.api.core.http.QueryParams
2929import com.imagekit.api.core.toImmutable
3030import com.imagekit.api.errors.ImageKitInvalidDataException
31+ import java.io.InputStream
32+ import java.nio.file.Path
3133import java.util.Collections
3234import java.util.Objects
3335import java.util.Optional
36+ import kotlin.io.path.inputStream
37+ import kotlin.io.path.name
3438import kotlin.jvm.optionals.getOrNull
3539
3640/* *
@@ -74,7 +78,7 @@ private constructor(
7478 * @throws ImageKitInvalidDataException if the JSON field has an unexpected type or is
7579 * unexpectedly missing or null (e.g. if the server responded with an unexpected value).
7680 */
77- fun file (): String = body.file()
81+ fun file (): InputStream = body.file()
7882
7983 /* *
8084 * The name with which the file has to be uploaded. The file name can contain:
@@ -323,7 +327,7 @@ private constructor(
323327 *
324328 * Unlike [file], this method doesn't throw if the multipart field has an unexpected type.
325329 */
326- fun _file (): MultipartField <String > = body._file ()
330+ fun _file (): MultipartField <InputStream > = body._file ()
327331
328332 /* *
329333 * Returns the raw multipart value of [fileName].
@@ -552,15 +556,38 @@ private constructor(
552556 * When supplying a URL, the server must receive the response headers within 8 seconds;
553557 * otherwise the request fails with 400 Bad Request.
554558 */
555- fun file (file : String ) = apply { body.file(file) }
559+ fun file (file : InputStream ) = apply { body.file(file) }
556560
557561 /* *
558562 * Sets [Builder.file] to an arbitrary multipart value.
559563 *
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.
562589 */
563- fun file (file : MultipartField < String > ) = apply { body.file(file ) }
590+ fun file (path : Path ) = apply { body.file(path ) }
564591
565592 /* *
566593 * The name with which the file has to be uploaded. The file name can contain:
@@ -1170,7 +1197,7 @@ private constructor(
11701197
11711198 class Body
11721199 private constructor (
1173- private val file: MultipartField <String >,
1200+ private val file: MultipartField <InputStream >,
11741201 private val fileName: MultipartField <String >,
11751202 private val token: MultipartField <String >,
11761203 private val checks: MultipartField <String >,
@@ -1208,7 +1235,7 @@ private constructor(
12081235 * @throws ImageKitInvalidDataException if the JSON field has an unexpected type or is
12091236 * unexpectedly missing or null (e.g. if the server responded with an unexpected value).
12101237 */
1211- fun file (): String = file.value.getRequired(" file" )
1238+ fun file (): InputStream = file.value.getRequired(" file" )
12121239
12131240 /* *
12141241 * The name with which the file has to be uploaded. The file name can contain:
@@ -1465,7 +1492,7 @@ private constructor(
14651492 *
14661493 * Unlike [file], this method doesn't throw if the multipart field has an unexpected type.
14671494 */
1468- @JsonProperty(" file" ) @ExcludeMissing fun _file (): MultipartField <String > = file
1495+ @JsonProperty(" file" ) @ExcludeMissing fun _file (): MultipartField <InputStream > = file
14691496
14701497 /* *
14711498 * Returns the raw multipart value of [fileName].
@@ -1699,7 +1726,7 @@ private constructor(
16991726 /* * A builder for [Body]. */
17001727 class Builder internal constructor() {
17011728
1702- private var file: MultipartField <String >? = null
1729+ private var file: MultipartField <InputStream >? = null
17031730 private var fileName: MultipartField <String >? = null
17041731 private var token: MultipartField <String > = MultipartField .of(null )
17051732 private var checks: MultipartField <String > = MultipartField .of(null )
@@ -1761,16 +1788,44 @@ private constructor(
17611788 * When supplying a URL, the server must receive the response headers within 8 seconds;
17621789 * otherwise the request fails with 400 Bad Request.
17631790 */
1764- fun file (file : String ) = file(MultipartField .of(file))
1791+ fun file (file : InputStream ) = file(MultipartField .of(file))
17651792
17661793 /* *
17671794 * Sets [Builder.file] to an arbitrary multipart value.
17681795 *
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.
17721821 */
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+ )
17741829
17751830 /* *
17761831 * The name with which the file has to be uploaded. The file name can contain:
0 commit comments