PicList/scripts/upload-dist-to-r2.js
萌萌哒赫萝 efeadb8fb8 Feature: add remote file delete , picBed management
First version of PicList.
In album, you can delete remote file now.
Add picBed management
function.
2023-02-15 23:36:47 +08:00

67 lines
2.6 KiB
JavaScript

// upload dist bundled-app to r2
require('dotenv').config()
const S3 = require('aws-sdk/clients/s3')
const pkg = require('../package.json')
const configList = require('./config')
const fs = require('fs')
const path = require('path')
const BUCKET = 'piclist-dl'
const VERSION = pkg.version
const FILE_PATH = `${VERSION}/`
const ACCOUNT_ID = process.env.R2_ACCOUNT_ID
const SECRET_ID = process.env.R2_SECRET_ID
const SECRET_KEY = process.env.R2_SECRET_KEY
console.log(ACCOUNT_ID, SECRET_ID, SECRET_KEY)
const s3 = new S3({
endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`,
accessKeyId: SECRET_ID,
secretAccessKey: SECRET_KEY,
signatureVersion: 'v4',
})
const uploadFile = async () => {
try {
const platform = process.platform
if (configList[platform]) {
let versionFileHasUploaded = false
for (const [index, config] of configList[platform].entries()) {
const fileName = `${config.appNameWithPrefix}${VERSION}${config.arch}${config.ext}`
const distPath = path.join(__dirname, '../dist_electron')
let versionFileName = config['version-file']
console.log('[PicList Dist] Uploading...', fileName, `${index + 1}/${configList[platform].length}`)
const fileBuffer = fs.readFileSync(path.join(distPath, fileName))
await s3.upload({
Bucket: BUCKET,
Key: `${FILE_PATH}${fileName}`,
Body: fileBuffer
}).promise()
// upload version file
if (!versionFileHasUploaded) {
console.log('[PicList Version File] Uploading...', versionFileName)
let versionFilePath
if (platform === 'win32') {
versionFilePath = path.join(distPath, 'latest.yml')
} else if (platform === 'darwin') {
versionFilePath = path.join(distPath, 'latest-mac.yml')
} else {
versionFilePath = path.join(distPath, 'latest-linux.yml')
}
const versionFileBuffer = fs.readFileSync(versionFilePath)
await s3.upload({
Bucket: BUCKET,
Key: `${versionFileName}`,
Body: versionFileBuffer
}).promise()
versionFileHasUploaded = true
}
}
} else {
console.warn('platform not supported!', platform)
}
} catch (err) {
console.error(err)
}
}
uploadFile()