package com.adealink.weparty.commonui.mediapicker.imageselect.processor

import com.adealink.frame.storage.file.imageProgressPath
import com.adealink.frame.util.ImageUtil
import kotlinx.coroutines.suspendCancellableCoroutine
import java.io.File
import java.util.UUID
import kotlin.coroutines.resume

/**
 * 图片压缩
 */
class ImageCompressProcessor(private val maxSizeKB: Int, private val minQuality: Int) :
    ImageProcessor() {
    override suspend fun process(path: String): String {
        return suspendCancellableCoroutine {
            val newFilePath = File(
                imageProgressPath,
                "${UUID.randomUUID()}.jpg"
            ).absolutePath
            if (ImageUtil.compressImage(path, newFilePath, maxSizeKB, minQuality)) {
                if (it.isActive) {
                    it.resume(newFilePath)
                }
            } else {
                if (it.isActive) {
                    it.resume(path)
                }
            }
        }
    }
}