FilesController.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  2. import STSClient, { AssumeRoleRequest } from '@alicloud/sts20150401'
  3. import { Config } from '@alicloud/openapi-client'
  4. import { RuntimeOptions } from '@alicloud/tea-util'
  5. import Drive from '@ioc:Adonis/Core/Drive'
  6. import FilesService from 'App/Services/FilesService'
  7. import { v4 as uuidv4 } from 'uuid'
  8. export default class FilesController {
  9. public async store({ request }: HttpContextContract) {
  10. const file = request.file('file')
  11. await file?.moveToDisk('./uploads', {})
  12. // const signedUrl = await Drive.getSignedUrl(file?.fileName!)
  13. // await Drive.setVisibility(file?.fileName!, 'public')
  14. return { url: file?.filePath }
  15. }
  16. public async sts() {
  17. // const client = new STSClient({
  18. // credentials: {
  19. // accessKeyId: 'LTAI5tQYh51ihAYExgrrs7Fu',
  20. // secretAccessKey: 'fpEdyOcPWsDSEwCjKkZgnEC8ZKnmAk'
  21. // },
  22. // region: 'cn-hangzhou',
  23. // endpoint: 'https://sts.cn-hangzhou.aliyuncs.com'
  24. // // apiVersion: '2015-04-01'
  25. // })
  26. const client = new STSClient(
  27. new Config({
  28. accessKeyId: 'LTAI5tQYh51ihAYExgrrs7Fu',
  29. accessKeySecret: 'fpEdyOcPWsDSEwCjKkZgnEC8ZKnmAk',
  30. endpoint: 'sts.cn-hangzhou.aliyuncs.com'
  31. })
  32. )
  33. let assumeRoleRequest = new AssumeRoleRequest({
  34. durationSeconds: 1000,
  35. roleArn: 'acs:ram::1037005998695187:role/oss-read',
  36. roleSessionName: 'user'
  37. })
  38. try {
  39. // 复制代码运行请自行打印 API 的返回值
  40. return await client.assumeRoleWithOptions(assumeRoleRequest, new RuntimeOptions())
  41. } catch (error) {
  42. // 错误 message
  43. console.log(error)
  44. }
  45. }
  46. public async uploadVideo({ request }: HttpContextContract) {
  47. const video = request.file('video', {
  48. extnames: ['mp4', 'mov', 'avi', 'mkv'],
  49. size: '500mb'
  50. })
  51. let folder = request.input('folder')
  52. if (!video) {
  53. return { error: '未上传文件' }
  54. }
  55. if (!folder) {
  56. return { error: '未指定文件夹名' }
  57. }
  58. const folderName = folder.replace(/\s+/g, '').replace(/[^a-zA-Z0-9_-]/g, '')
  59. const filePath = `video/${folderName}/${Date.now()}_${video.clientName}`
  60. await video.moveToDisk('.', { name: filePath }, 's3')
  61. return { url: filePath }
  62. }
  63. public async batchDelete({ request, response }: HttpContextContract) {
  64. try {
  65. const { filePaths } = request.only(['filePaths'])
  66. const result = await FilesService.batchDelete(filePaths)
  67. return response.ok({
  68. success: true,
  69. message: '批量删除操作完成',
  70. data: result
  71. })
  72. } catch (error) {
  73. return response.status(500).json({
  74. success: false,
  75. message: '批量删除操作失败',
  76. error: error.message
  77. })
  78. }
  79. }
  80. }