1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /*
- * @Author: Rindy
- * @Date: 2020-03-13 10:56:21
- * @LastEditors: Rindy
- * @LastEditTime: 2021-09-16 10:20:55
- * @Description: 静态文件上传
- */
- const fs = require('fs')
- const url = require('url')
- const path = require('path')
- const OSS = require('ali-oss')
- const package = require('../lerna.json')
- let client = new OSS({
- region: 'oss-cn-shenzhen',
- accessKeyId: 'LTAI4FreM2331vLtfDfG7qfX',
- accessKeySecret: 's8g77lxt1KPxCzDE4hyjwlAgPwfhnG',
- bucket: '4dkankan',
- timeout: 120000,
- maxSockets: 200,
- })
- const toPath = dist => path.resolve(dist)
- async function upload(filepath, key, cache, retry = 0) {
- try {
- let result = await client.put(key, filepath, {
- headers: {
- 'Cache-Control': cache,
- },
- })
- if (result.res.status != 200) {
- console.log('上传失败:' + key, 'status code:' + result.res.status)
- } else {
- console.log('上传成功:' + key)
- }
- } catch (err) {
- if (++retry < 3) {
- return upload(filepath, key, cache, retry)
- }
- console.log('上传失败:' + key)
- throw err
- }
- }
- function list_files(dir, callback) {
- fs.readdir(dir, (err, files) => {
- if (err) {
- throw err
- }
- files.forEach(async file => {
- let filepath = path.join(dir, file)
- const state = fs.statSync(filepath)
- if (state.isDirectory()) {
- list_files(filepath, callback)
- } else {
- if (filepath.endsWith('.map') == false) {
- callback(filepath)
- }
- }
- })
- })
- }
- function uploads() {
- ;[
- {
- dir: 'dist/editor/editor',
- key: 'v4-test/www/editor',
- },
- {
- dir: 'dist/viewer/viewer',
- key: 'v4-test/www/viewer',
- },
- {
- dir: 'dist/sdk',
- key: 'v4-test/www/sdk',
- },
- {
- dir: 'dist/sdk',
- key: 'v4-test/sdk',
- sdk: true,
- },
- ].forEach(item => {
- list_files(toPath(item.dir), filepath => {
- let temppath = filepath.replace(/\\/g, '/')
- let keyspath = item.key + temppath.split(item.dir)[1]
- if (item.sdk) {
- keyspath = item.key + '/' + package.version + temppath.split(item.dir)[1]
- }
- upload(filepath, keyspath, 'max-age=3153600') // 缓存30天
- })
- })
- }
- uploads()
|