File.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import Env from '@ioc:Adonis/Core/Env'
  2. import BaseSeeder from '@ioc:Adonis/Lucid/Seeder'
  3. import Drive from '@ioc:Adonis/Core/Drive'
  4. import * as fs from 'fs'
  5. import * as path from 'path'
  6. import OSS from 'ali-oss'
  7. export default class extends BaseSeeder {
  8. public async run() {
  9. // Write your database queries inside the run method
  10. // const dir = '/Volumes/Data HD/shortplay/files/08F2CNhR'
  11. // await Promise.all(
  12. // fs
  13. // .readdirSync(dir)
  14. // .map((file) =>
  15. // Drive.putStream(
  16. // path.join('files/08F2CNhR', file),
  17. // fs.createReadStream(path.join(dir, file))
  18. // )
  19. // )
  20. // )
  21. const client = new OSS({
  22. // yourregion填写Bucket所在地域。以华东1(杭州)为例,Region填写为oss-cn-hangzhou。
  23. region: Env.get('OSS_REGION'),
  24. // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
  25. accessKeyId: Env.get('OSS_KEY'),
  26. accessKeySecret: Env.get('OSS_SECRET'),
  27. // 填写Bucket名称。
  28. bucket: Env.get('OSS_BUCKET')
  29. })
  30. // const dirs = await client.listV2({
  31. // prefix: 'files/asdf',
  32. // delimiter: '/'
  33. // })
  34. // console.log(dirs)
  35. const downloaded = '/Volumes/Data HD/shortplay/downloaded'
  36. const dir = '/Volumes/Data HD/shortplay/files'
  37. const subDirs = fs
  38. .readdirSync(downloaded)
  39. .filter((file) => {
  40. const stat = fs.statSync(path.join(downloaded, file))
  41. return !stat.isDirectory() && !/^\./.test(file)
  42. })
  43. .sort()
  44. .map((file) => {
  45. return file.replace('.json', '')
  46. })
  47. for (let i = 0; i < 10; i++) {
  48. const subDir = subDirs[i]
  49. const files = fs.readdirSync(path.join(dir, subDir)).filter((file) => {
  50. const stat = fs.statSync(path.join(dir, subDir, file))
  51. return !stat.isDirectory()
  52. })
  53. const cover = files.find((file) => /^cover_/.test(file))
  54. if (cover) {
  55. const key = path.join('files', subDir, cover)
  56. try {
  57. await client.head(key)
  58. await client.putACL(key, 'public-read')
  59. console.log('exists', key)
  60. } catch (error) {}
  61. }
  62. }
  63. }
  64. }