package com.lukouguoji.module_base.util

import com.lukouguoji.module_base.bean.BaseResultBean
import com.lukouguoji.module_base.bean.UploadBean
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.ktx.loge
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.File

/**
 * @author：孟凡华
 * @date：2021/12/14 8:44
 */
object UploadUtil {

    suspend fun upload(path: String): BaseResultBean<UploadBean> {
        loge("上传的文件: $path")
        val file = File(path)
        val body = file.asRequestBody("multipart/form-data".toMediaTypeOrNull())
        val part = MultipartBody.Part.createFormData("file", file.name, body)
        return NetApply.api.uploadFile(part)
    }

}