Skip to content

Commit

Permalink
测试完成新的上传接口
Browse files Browse the repository at this point in the history
  • Loading branch information
czf0613 committed Oct 12, 2022
1 parent dff49c6 commit 67883b0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 47 deletions.
43 changes: 22 additions & 21 deletions KCosSDK/src/main/java/ltd/kevinc/kcos/KCosFileDownloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ object KCosFileDownloader {
fileId: Long,
password: String? = null
): DownloadMetadata {
val url = makeUrl(fileId, password)
val request = Request.Builder()
.url(url)
.header("X-AppId", KCosClient.appId)
.header("X-AppKey", KCosClient.appKey)
.header("X-UserId", KCosClient.userId.toString())
.head()
.build()

return withContext(Dispatchers.IO) {
val url = makeUrl(fileId, password)
val request = Request.Builder()
.url(url)
.header("X-AppId", KCosClient.appId)
.header("X-AppKey", KCosClient.appKey)
.header("X-UserId", KCosClient.userId.toString())
.head()
.build()

val resp = KCosClient.httpClient.newCall(request).execute()

if (resp.code >= 400)
Expand Down Expand Up @@ -66,21 +66,21 @@ object KCosFileDownloader {
fileId: Long,
password: String? = null
): ByteArray {
val url = makeUrl(fileId, password)
val meta = getFileDownloadMeta(fileId, password)
return withContext(Dispatchers.IO) {
val url = makeUrl(fileId, password)
val meta = getFileDownloadMeta(fileId, password)

if (meta.contentLength > 4 * 1048576L)
throw ArrayIndexOutOfBoundsException("下载文件的大小超过4MB,请使用大文件下载接口")
if (meta.contentLength > 4 * 1048576L)
throw ArrayIndexOutOfBoundsException("下载文件的大小超过4MB,请使用大文件下载接口")

val request = Request.Builder()
.url(url)
.header("X-AppId", KCosClient.appId)
.header("X-AppKey", KCosClient.appKey)
.header("X-UserId", KCosClient.userId.toString())
.get()
.build()
val request = Request.Builder()
.url(url)
.header("X-AppId", KCosClient.appId)
.header("X-AppKey", KCosClient.appKey)
.header("X-UserId", KCosClient.userId.toString())
.get()
.build()

return withContext(Dispatchers.IO) {
val resp = KCosClient.httpClient.newCall(request).execute()
resp.body!!.bytes()
}
Expand Down Expand Up @@ -151,6 +151,7 @@ object KCosFileDownloader {
): Long {
val url = makeUrl(fileId, password)
val meta = getFileDownloadMeta(fileId, password)

val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val request = DownloadManager.Request(Uri.parse(url)).apply {
setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
Expand Down
6 changes: 0 additions & 6 deletions KCosSDK/src/main/java/ltd/kevinc/kcos/KCosUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class KCosUtils {
System.loadLibrary("MediaConverter")
}

private lateinit var delegate: KCosProcessDelegate
private var compressVideoJob: Job? = null
private lateinit var mediaEncoder: MediaCodec
private lateinit var mediaDecoder: MediaCodec
Expand Down Expand Up @@ -151,10 +150,7 @@ class KCosUtils {
context: Context,
width: Int = -1,
height: Int = -1,
delegate: KCosProcessDelegate
) {
this.delegate = delegate

// 在计算核心上面跑转码运算性能会更好
withContext(Dispatchers.Default) {
compressVideoJob?.cancel()
Expand All @@ -168,10 +164,8 @@ class KCosUtils {
width,
height
)
delegate.onConversionSuccess(File(output))
} ?: throw FileNotFoundException()
} catch (e: Exception) {
delegate.onError(e)
Log.e("KCos.video.convert", "native code error!")
}
}
Expand Down
52 changes: 32 additions & 20 deletions app/src/main/java/ltd/kevinc/cos/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package ltd.kevinc.cos

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.net.toUri
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import ltd.kevinc.kcos.KCosClient
import ltd.kevinc.kcos.KCosFileUploader
import ltd.kevinc.kcos.R
import ltd.kevinc.kcos.pocos.CreateFileEntryRequest
import java.io.File

class MainActivity : AppCompatActivity() {
private var cnt = 0
Expand All @@ -23,7 +27,7 @@ class MainActivity : AppCompatActivity() {

println(userId)

// downloader.downloadLargeFile(44)
// KCosFileDownloader.downloadLargeFile(44)
// .onStart {
// println("开始下载")
// }
Expand All @@ -48,7 +52,15 @@ class MainActivity : AppCompatActivity() {
super.onStart()

lifecycleScope.launch {
// val file = File("${applicationContext.dataDir.path}/fake.dat")
val userId = KCosClient.initializeKCosService(
"20b332a9-35a9-0b09-0bf2-79421bde4a54",
"e8c688ac-7a6e-1add-1a70-564a02fce624",
"1919810"
)

println(userId)

val file = File("${applicationContext.dataDir.path}/fake.dat")
// file.createNewFile()
//
// for (i in 0..10240) {
Expand Down Expand Up @@ -85,30 +97,30 @@ class MainActivity : AppCompatActivity() {
//
// println(file.length())

// uploader.uploadData(
// val (entry1, flow1) = KCosFileUploader.uploadData(
// file.toUri(),
// this@MainActivity,
// CreateFileEntryRequest(
// path = "/czf0613/soft/",
// fileNameWithExt = "fake.file",
// path = "/czf0613/android_test/",
// fileNameWithExt = "fake.nope",
// fileSize = file.length(),
// deadLine = "2022-12-31"
// ),
// object : KCosProcessDelegate {
// override fun onUploadTick(currentStep: Long, totalSteps: Long) {
// println("上传进度:$currentStep / $totalSteps")
// }
//
// override fun onError(e: Throwable) {
// e.printStackTrace()
// }
// })
// )
// )
// println(entry1.id)
// flow1?.collect {
// println("packs: $it")
// }

// uploader.continueUpload(36, file, object : KCosProcessDelegate {
// override fun onContinueUploadTick(currentStep: Long) {
// println(currentStep)
// }
// })
val entry2 = KCosFileUploader.uploadData(
ByteArray(10485552), CreateFileEntryRequest(
path = "/czf0613/androidTest/",
fileNameWithExt = "array.wtf",
fileSize = 10485552,
deadLine = "2022-12-31"
)
)
println(entry2.id)
}
}
}

0 comments on commit 67883b0

Please sign in to comment.