diff --git a/.github/workflows/cdn.yml b/.github/workflows/cdn.yml new file mode 100644 index 0000000..832b84d --- /dev/null +++ b/.github/workflows/cdn.yml @@ -0,0 +1,36 @@ +name: Upload to R2 Bucket + +on: + push: + branches: + - master + + workflow_dispatch: + +jobs: + upload-cdn: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Install Node.js + uses: actions/setup-node@v2 + with: + node-version: '14' + + - name: Install dependencies + run: npm install + + - name: Run npm run package + run: npm run package + + - name: Configure AWS credentials + run: | + aws s3 cp ./cdn s3://$BUCKET_NAME --recursive --region $REGION --access-key-id $ACCESS_KEY_ID --secret-access-key $SECRET_ACCESS_KEY + env: + ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }} + SECRET_ACCESS_KEY: ${{ secrets.SECRET_ACCESS_KEY }} + BUCKET_NAME: skinbook-cdn + REGION: WEUR diff --git a/.gitignore b/.gitignore index 7f2a692..b8672db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ **/node_modules pnpm-lock.yaml -yarn.lock \ No newline at end of file +yarn.lock +.env +**/cdn \ No newline at end of file diff --git a/package.json b/package.json index da713b2..0e66a6f 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "", "main": "./scripts/validator.js", "scripts": { - "validate": "node ./scripts/validator.js" + "validate": "node ./scripts/validator.js", + "package": "node ./scripts/prepare.js" }, "repository": { "type": "git", @@ -17,6 +18,8 @@ }, "homepage": "https://github.com/skinbookmc/ServerMappings#readme", "dependencies": { - "ajv": "^8.12.0" + "ajv": "^8.12.0", + "cloudflare": "3.0.0-beta.14", + "dotenv": "^16.4.5" } } diff --git a/scripts/prepare.js b/scripts/prepare.js new file mode 100644 index 0000000..c8f43c0 --- /dev/null +++ b/scripts/prepare.js @@ -0,0 +1,78 @@ +const fs = require('fs') +const path = require('path') + +const serversDir = path.join(__dirname, '../servers') +const cdnDir = path.join(__dirname, '../cdn') + +// Function to read directories recursively +function readDirectoriesRecursively(dir) { + let files = [] + + const items = fs.readdirSync(dir) + + items.forEach((item) => { + const fullPath = path.join(dir, item) + + if (fs.statSync(fullPath).isDirectory()) { + files = files.concat(readDirectoriesRecursively(fullPath)) + } else { + files.push(fullPath) + } + }) + + return files +} + +// Function to combine metadata files into a single servers.json file +function combineMetadataToServersJSON() { + const metadataFiles = readDirectoriesRecursively(serversDir).filter( + (file) => path.basename(file) === 'metadata.json' + ) + + const serversData = metadataFiles.map((file) => { + return JSON.parse(fs.readFileSync(file, 'utf-8')) + }) + + const combinedData = serversData.reduce((acc, curr) => { + return acc.concat(curr) + }, []) + + fs.writeFileSync( + path.join(cdnDir, 'servers.json'), + JSON.stringify(combinedData, null, 2) + ) +} + +// Function to copy and rename files to the CDN folders +function copyAndRenameFiles(folderName, fileExtension) { + const files = readDirectoriesRecursively(serversDir).filter( + (file) => path.basename(file) === fileExtension + ) + + files.forEach((file) => { + const folderId = path.basename(path.dirname(file)) + const destination = path.join(cdnDir, folderName, `${folderId}.png`) + + // Create destination folder if it doesn't exist + const destFolder = path.dirname(destination) + if (!fs.existsSync(destFolder)) { + fs.mkdirSync(destFolder, { recursive: true }) + } + + fs.copyFileSync(file, destination) + }) +} + +// Main function to execute all tasks +function prepareCDNUpload() { + if (!fs.existsSync(cdnDir)) { + fs.mkdirSync(cdnDir) + } + + combineMetadataToServersJSON() + copyAndRenameFiles('logos', 'logo.png') + copyAndRenameFiles('banners', 'background.png') +} + +// Usage +prepareCDNUpload()