-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from skinbookmc/servers
Added R2 Bucket
- Loading branch information
Showing
4 changed files
with
122 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
**/node_modules | ||
pnpm-lock.yaml | ||
yarn.lock | ||
yarn.lock | ||
.env | ||
**/cdn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |