forked from EXT/VR180-Web-Player
This commit is contained in:
@@ -7,7 +7,9 @@ R2_SECRET_ACCESS_KEY=your_r2_secret_access_key
|
||||
R2_BUCKET=your-r2-bucket-name
|
||||
|
||||
# Optional settings.
|
||||
# Local folder to upload after running npm run build.
|
||||
R2_SOURCE_DIR=vr180player
|
||||
# Object key prefix inside the bucket.
|
||||
R2_PREFIX=vr180player
|
||||
R2_ENDPOINT=
|
||||
R2_CACHE_CONTROL=public, max-age=31536000, immutable
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createHmac, createHash } from 'node:crypto';
|
||||
import { readdir, readFile } from 'node:fs/promises';
|
||||
import { basename, dirname, join, relative, sep } from 'node:path';
|
||||
import { readdir, readFile, stat } from 'node:fs/promises';
|
||||
import { basename, dirname, join, relative, resolve, sep } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const rootDir = dirname(dirname(fileURLToPath(import.meta.url)));
|
||||
@@ -9,6 +9,7 @@ const args = new Set(process.argv.slice(2));
|
||||
await loadEnvFile(join(rootDir, '.env.r2'));
|
||||
|
||||
const config = getConfig();
|
||||
await assertReadableSourceDir(config.sourceDir);
|
||||
const files = await listFiles(config.sourceDir);
|
||||
|
||||
if (files.length === 0) {
|
||||
@@ -45,10 +46,32 @@ function getConfig() {
|
||||
endpoint: endpoint.replace(/\/+$/, ''),
|
||||
prefix: trimSlashes(process.env.R2_PREFIX ?? 'vr180player'),
|
||||
secretAccessKey,
|
||||
sourceDir: join(rootDir, process.env.R2_SOURCE_DIR?.trim() || 'vr180player')
|
||||
sourceDir: resolve(rootDir, process.env.R2_SOURCE_DIR?.trim() || 'vr180player')
|
||||
};
|
||||
}
|
||||
|
||||
async function assertReadableSourceDir(sourceDir) {
|
||||
try {
|
||||
const sourceStats = await stat(sourceDir);
|
||||
if (!sourceStats.isDirectory()) {
|
||||
console.error(`R2_UPLOAD: R2_SOURCE_DIR is not a directory: ${sourceDir}`);
|
||||
process.exit(1);
|
||||
}
|
||||
} catch (error) {
|
||||
if (error?.code === 'ENOENT') {
|
||||
console.error([
|
||||
`R2_UPLOAD: Source directory not found: ${sourceDir}`,
|
||||
'R2_SOURCE_DIR is a local folder to upload, not the R2 bucket name.',
|
||||
'If "VR-180" is your bucket name, set R2_BUCKET=VR-180 and leave R2_SOURCE_DIR=vr180player.',
|
||||
'If vr180player/ is missing, run npm run build first.'
|
||||
].join('\n'));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadEnvFile(filePath) {
|
||||
let contents;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user