import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import STSClient, { AssumeRoleRequest } from '@alicloud/sts20150401'
import { Config } from '@alicloud/openapi-client'
import { RuntimeOptions } from '@alicloud/tea-util'
import Drive from '@ioc:Adonis/Core/Drive'
export default class FilesController {
    public async store({ request }: HttpContextContract) {
        const file = request.file('file')
        await file?.moveToDisk('./uploads', {})
        // const signedUrl = await Drive.getSignedUrl(file?.fileName!)
        // await Drive.setVisibility(file?.fileName!, 'public')
        return { url: file?.filePath }
    }

    public async sts() {
        // const client = new STSClient({
        //     credentials: {
        //         accessKeyId: 'LTAI5tQYh51ihAYExgrrs7Fu',
        //         secretAccessKey: 'fpEdyOcPWsDSEwCjKkZgnEC8ZKnmAk'
        //     },
        //     region: 'cn-hangzhou',
        //     endpoint: 'https://sts.cn-hangzhou.aliyuncs.com'
        //     // apiVersion: '2015-04-01'
        // })
        const client = new STSClient(
            new Config({
                accessKeyId: 'LTAI5tQYh51ihAYExgrrs7Fu',
                accessKeySecret: 'fpEdyOcPWsDSEwCjKkZgnEC8ZKnmAk',
                endpoint: 'sts.cn-hangzhou.aliyuncs.com'
            })
        )
        let assumeRoleRequest = new AssumeRoleRequest({
            durationSeconds: 1000,
            roleArn: 'acs:ram::1037005998695187:role/oss-read',
            roleSessionName: 'user'
        })
        try {
            // 复制代码运行请自行打印 API 的返回值
            return await client.assumeRoleWithOptions(assumeRoleRequest, new RuntimeOptions())
        } catch (error) {
            // 错误 message
            console.log(error)
        }
    }
}
