import android.text.TextUtils
|
import com.example.basecore.util.ThreadUtils
|
import com.example.smart.bean.ImageListResponse
|
import com.example.smart.bean.UploadResponseBean
|
import com.example.smart.image.ImageProvider
|
import com.example.smart.oss.IOssCallBack
|
import com.example.smart.reqeust.RetrofitClient
|
import com.example.smart.reqeust.RetrofitClient.okHttpClientHeader
|
import com.example.smart.reqeust.interceptor.HeaderInterceptor
|
import com.example.smart.util.StatusProvider
|
import com.google.gson.Gson
|
import com.youth.banner.util.LogUtils
|
import okhttp3.*
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
import java.io.File
|
import java.io.IOException
|
|
|
object FileUploader {
|
|
|
/**
|
* 单个文件
|
*/
|
fun uploadFile(path: String, callback: ImageProvider.IImageCallback, end: () -> Unit) {
|
|
val file = File(path)
|
if (!file.exists()) {
|
LogUtils.e("File does not exist")
|
return
|
}
|
|
val requestBody = RequestBody.create(
|
"multipart/form-data".toMediaTypeOrNull(), file
|
)
|
|
val multipartBody = MultipartBody.Builder()
|
.setType(MultipartBody.FORM)
|
.addFormDataPart("file", file.name, requestBody)
|
.build()
|
|
val token = StatusProvider.getToken()
|
if (TextUtils.isEmpty(token)) {
|
LogUtils.e("Login first")
|
return
|
}
|
|
val request = Request.Builder()
|
.url(RetrofitClient.serverUrl + "public/file/upload")
|
.addHeader(HeaderInterceptor.authorization, "Bearer $token")
|
.post(multipartBody)
|
.build()
|
|
|
// val client = OkHttpClient()
|
val client = okHttpClientHeader()
|
client.newCall(request).enqueue(object : Callback {
|
override fun onFailure(call: Call, e: IOException) {
|
ThreadUtils.runOnUiThread {
|
end()
|
callback.onUploadFailed()
|
}
|
}
|
|
override fun onResponse(call: Call, response: Response) {
|
response.body?.let {
|
val string = it.string()
|
|
try {
|
val bean = Gson().fromJson(string, UploadResponseBean::class.java)
|
|
//net img address
|
//http://47.93.28.249:8181/public/file/download/
|
//entity.name
|
|
ThreadUtils.runOnUiThread {
|
end()
|
if (bean.data != null) {
|
bean.data.localPath = path
|
callback.onUploadSuccess(listOf(bean.data))
|
} else {
|
callback.onUploadFailed()
|
}
|
}
|
|
|
} catch (e: Exception) {
|
ThreadUtils.runOnUiThread {
|
end()
|
callback.onUploadFailed()
|
}
|
}
|
}
|
}
|
|
})
|
}
|
|
|
/**
|
* 多个文件
|
*/
|
fun uploadFile(
|
paths: List<String>, callback: ImageProvider.IImageCallback,
|
end: () -> Unit
|
) {
|
val token = StatusProvider.getToken()
|
if (TextUtils.isEmpty(token)) {
|
LogUtils.e("Login first")
|
return
|
}
|
|
val builder = MultipartBody.Builder()
|
.setType(MultipartBody.FORM)
|
|
for (path in paths) {
|
if (TextUtils.isEmpty(path)) {
|
LogUtils.d("file not exist = $path")
|
continue
|
}
|
val file = File(path)
|
val body = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
|
builder.addFormDataPart("files", file.name, body)
|
}
|
val multipartBody = builder.build()
|
|
val request = Request.Builder()
|
.url(RetrofitClient.serverUrl + "public/file/uploadMultiple")
|
.addHeader(HeaderInterceptor.authorization, "Bearer $token")
|
.post(multipartBody)
|
.build()
|
|
|
// val client = OkHttpClient()
|
val client = okHttpClientHeader()
|
client.newCall(request).enqueue(object : Callback {
|
override fun onFailure(call: Call, e: IOException) {
|
ThreadUtils.runOnUiThread {
|
end()
|
callback.onUploadFailed()
|
}
|
}
|
|
override fun onResponse(call: Call, response: Response) {
|
response.body?.let {
|
val string = it.string()
|
try {
|
val bean = Gson().fromJson(string, ImageListResponse::class.java)
|
ThreadUtils.runOnUiThread {
|
end()
|
|
if (bean.data != null) {
|
//此处可以根据文件名称设置本地路径
|
callback.onUploadSuccess(bean.data)
|
} else {
|
callback.onUploadFailed()
|
}
|
}
|
} catch (e: Exception) {
|
ThreadUtils.runOnUiThread {
|
end()
|
|
callback.onUploadFailed()
|
}
|
}
|
}
|
}
|
|
})
|
}
|
|
}
|