| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 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'
- import FilesService from 'App/Services/FilesService'
- import { v4 as uuidv4 } from 'uuid'
- 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)
- }
- }
- public async uploadVideo({ request }: HttpContextContract) {
- const video = request.file('video', {
- extnames: ['mp4', 'mov', 'avi', 'mkv'],
- size: '500mb'
- })
- let folder = request.input('folder')
- if (!video) {
- return { error: '未上传文件' }
- }
- if (!folder) {
- return { error: '未指定文件夹名' }
- }
- const folderName = folder.replace(/\s+/g, '').replace(/[^a-zA-Z0-9_-]/g, '')
- const filePath = `video/${folderName}/${Date.now()}_${video.clientName}`
- await video.moveToDisk('.', { name: filePath }, 's3')
- return { url: filePath }
- }
- public async batchDelete({ request, response }: HttpContextContract) {
- try {
- const { filePaths } = request.only(['filePaths'])
- const result = await FilesService.batchDelete(filePaths)
- return response.ok({
- success: true,
- message: '批量删除操作完成',
- data: result
- })
- } catch (error) {
- return response.status(500).json({
- success: false,
- message: '批量删除操作失败',
- error: error.message
- })
- }
- }
- }
|