Browse Source

@task: file download api

bai 3 years ago
parent
commit
f26b4ebfe8

+ 4 - 3
demo/src/main/java/com/framework/app/api/Api.kt

@@ -63,16 +63,17 @@ interface Api {
      * 文件下载
      */
     @POST("http://www.ysbdtp.com/dmp-file/file/initFileDownload")
-    fun getFileDownloadInfo(
+    suspend fun getFileDownloadInfo(
         @QueryMap param: Map<String, String>,
-        @Body fileId: String
+        @Body map:Map<String, String>
     ): Model<FileDownloadInfo>
 
     /**
      * 返回地图
      */
+    @Streaming
     @GET
-    suspend fun downloadFile(@Url url: String): String
+    suspend fun downloadFile(@Url url: String): ByteArray?
 
     /**
      * 登录

+ 22 - 0
demo/src/main/java/com/framework/app/converter/ByteArrayConverterFactory.kt

@@ -0,0 +1,22 @@
+package com.framework.app.converter
+
+import okhttp3.ResponseBody
+import retrofit2.Converter
+import retrofit2.Retrofit
+import java.lang.reflect.Type
+
+/**
+ * 字节流转换器
+ */
+class ByteArrayConverterFactory : Converter.Factory() {
+    override fun responseBodyConverter(
+        type: Type,
+        annotations: Array<out Annotation>,
+        retrofit: Retrofit
+    ): Converter<ResponseBody, *>? {
+        if (type.typeName == "byte[]") {
+            return Converter<ResponseBody, ByteArray> { value -> value.bytes() }
+        }
+        return null
+    }
+}

+ 2 - 2
demo/src/main/java/com/framework/app/http/FileDownload.kt

@@ -5,8 +5,8 @@ package com.framework.app.http
  */
 data class FileDownloadInfo(
     val id: String,
-    val fileDownloadUrl: String,  // 文件下载Url
-    val fileType: String,         // 文件类型:0 文件,1 文件夹
+    val fileDownloadUrl: String?,  // 文件下载Url
+    val fileType: String?,         // 文件类型:0 文件,1 文件夹
     val fileName: String,         // 文件名
     val businessId: String,
     val fileBucket: String,

+ 2 - 0
demo/src/main/java/com/framework/di/Di.kt

@@ -3,6 +3,7 @@ package com.framework.di
 import android.content.Context
 import com.framework.app.IP
 import com.framework.app.api.Api
+import com.framework.app.converter.ByteArrayConverterFactory
 import com.framework.app.converter.NullOrEmptyConverter
 import com.framework.app.http.GzipRequestInterceptor
 import com.framework.mvvm.model.db.AdmDatabase
@@ -37,6 +38,7 @@ val httpModule = DI.Module("netModule") {
                 .baseUrl(IP)
                 .client(instance())
                 .addConverterFactory(NullOrEmptyConverter())
+                .addConverterFactory(ByteArrayConverterFactory())
                 .addConverterFactory(GsonConverterFactory.create())
                 .build()
         }

+ 2 - 2
demo/src/main/java/com/framework/mvvm/model/repository/AdmRepository.kt

@@ -630,13 +630,13 @@ class AdmRepository(
         param: Map<String, String>,
         fileId: String
     ): Model<FileDownloadInfo> {
-        return api.getFileDownloadInfo(param = param, fileId = fileId)
+        return api.getFileDownloadInfo(param = param, mapOf("fileId" to fileId))
     }
 
     /**
      * 下载地址
      */
-    suspend fun downloadFile(fileUrl: String): String {
+    suspend fun downloadFile(fileUrl: String): ByteArray? {
         return api.downloadFile(url = fileUrl)
     }
 

+ 50 - 2
demo/src/main/java/com/framework/mvvm/mv/AdmViewModel.kt

@@ -7,11 +7,11 @@ import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
 import cn.sagacloud.cadengine.OkhttpUtil
 import com.framework.app.*
+import com.framework.app.http.FileDownloadInfo
 import com.framework.app.http.FileInfo
 import com.framework.app.http.FileUploadInfo
 import com.framework.app.tools.md5
 import com.framework.app.tools.toBytes
-import com.framework.mvvm.model.data.Data
 import com.framework.mvvm.model.data.Response
 import com.framework.mvvm.model.data.SuccessResponse
 import com.framework.mvvm.model.data.request
@@ -25,7 +25,6 @@ import com.framework.mvvm.model.vo.Client
 import com.framework.mvvm.model.vo.Login
 import com.sybotan.android.demo.DemoApp
 import com.sybotan.android.demo.tools.LocalDataOperation
-import com.yuyh.library.imgsel.utils.LogUtils
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.async
 import kotlinx.coroutines.flow.*
@@ -263,6 +262,55 @@ class AdmViewModel(private val repository: AdmRepository) : ViewModel() {
         }
     }
 
+    private suspend fun getFileDownloadInfo(
+        param: Map<String, String>,
+        fileId: String
+    ): Response<FileDownloadInfo> {
+        return request { repository.getFileDownloadInfo(param, fileId) }
+    }
+
+    /**
+     * 上传下载
+     *
+     * @param param
+     *     mapOf(
+     *         "appId" to "adm",
+     *         "userId" to "bdtp",
+     *         "groupCode" to "Pj1101080665",
+     *         "projectId" to "BR"
+     *     )
+     * @param file
+     *     例:
+     *     val path: File? = getExternalFilesDir("/img/png")
+     *     if (path == null || !path.exists()) {
+     *         path?.mkdirs()
+     *     }
+     *     val file = File(path, "download.png")
+     * @param fileId
+     *     fileId: 文件存储平台的 fileId
+     */
+    fun fileDownload(param: Map<String, String>, file: File, fileId: String) {
+
+        viewModelScope.launch(Dispatchers.IO) {
+            val response = getFileDownloadInfo(param, fileId)
+            if (response is SuccessResponse) {
+                val data: FileDownloadInfo = response.data
+                val url = data.fileDownloadUrl ?: ""
+
+                if (url.isNotEmpty()) {
+                    val byteArray = repository.downloadFile(url)
+                    val fos = FileOutputStream(file)
+                    fos.use { out ->
+                        out.write(byteArray)
+                    }
+                }
+
+            } else {
+                Log.d("fileDownload", "fileDownload has something error:$response")
+            }
+        }
+    }
+
     /**
      * 获取服务端生成的客户端Id
      */

+ 7 - 1
demo/src/main/java/com/sybotan/android/demo/activities/PocActivity.kt

@@ -135,7 +135,13 @@ class PocActivity : BaseActivity(), DIAware {
 
         uploadBtn.setOnClickListener {
             lifecycleScope.launch(Dispatchers.IO) {
-                LocalDataOperation.getInstance().offlineSendRequest.uploadImageNew(this@PocActivity, mVm, projectId, groupCode)
+                LocalDataOperation.getInstance().offlineSendRequest.uploadImageNew(
+                    this@PocActivity,
+                    mVm,
+                    projectId,
+                    groupCode,
+                    userId
+                )
             }
             mVm.uploadBuilding(
                 request, block = {

+ 3 - 10
demo/src/main/java/com/sybotan/android/demo/tools/OfflineSendRequest.java

@@ -1,16 +1,9 @@
 package com.sybotan.android.demo.tools;
 
 import android.content.Context;
-import android.text.TextUtils;
-import android.util.Log;
 
 import com.framework.mvvm.mv.AdmViewModel;
 import com.google.gson.Gson;
-import com.sybotan.android.demo.retrofit.Constant;
-import com.yuyh.library.imgsel.utils.LogUtils;
-
-import org.json.JSONException;
-import org.json.JSONObject;
 
 import java.io.File;
 import java.util.HashMap;
@@ -36,16 +29,16 @@ public class OfflineSendRequest {
      * @param context
      * @param mVm
      */
-    public void uploadImageNew(Context context, AdmViewModel mVm, String projectId, String groupCode) {
+    public void uploadImageNew(Context context, AdmViewModel mVm, String projectId, String groupCode, String userId) {
         String imageFilePath = LocalDataOperation.getInstance().getOfflineImageFilePath(context);
         File imageFile = new File(imageFilePath);
         if (imageFile.exists()) {
             File[] files = imageFile.listFiles();
             if (files != null && files.length > 0) {
                 for (File file : files) {
-                    Map param = new HashMap<String, String>();
+                    Map<String, String> param = new HashMap<>();
                     param.put("appId", "adm");
-                    param.put("userId", "bdtp");
+                    param.put("userId", userId);
                     param.put("groupCode", groupCode);
                     param.put("projectId", projectId);
                     mVm.fileUpload(param, file);