-
Notifications
You must be signed in to change notification settings - Fork 407
feat: Add material upload script #1516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
b47c448
3024d93
d265463
511deaf
df06dd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Buffer } from 'buffer' | ||
import dotenv from 'dotenv' | ||
import fs from 'fs-extra' | ||
import path from 'node:path' | ||
import Logger from './logger.mjs' | ||
|
||
const logger = new Logger('uploadMaterials') | ||
|
||
// 先构造出.env*文件的绝对路径 | ||
const appDirectory = fs.realpathSync(process.cwd()) | ||
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath) | ||
const pathsDotenv = resolveApp('.env') | ||
dotenv.config({ path: `${pathsDotenv}.local` }) | ||
const { backend_url } = process.env | ||
|
||
const bundlePath = path.join(process.cwd(), '/designer-demo/public/mock/bundle.json') | ||
const bundle = fs.readJSONSync(bundlePath) | ||
Comment on lines
+16
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Make the bundle path configurable and add validation. The hardcoded path to Apply this diff to make the path configurable and add validation: -const bundlePath = path.join(process.cwd(), '/designer-demo/public/mock/bundle.json')
-const bundle = fs.readJSONSync(bundlePath)
+const bundlePath = process.env.BUNDLE_PATH || path.join(process.cwd(), 'designer-demo/public/mock/bundle.json')
+
+if (!fs.existsSync(bundlePath)) {
+ logger.error(`Bundle file not found at: ${bundlePath}`)
+ process.exit(1)
+}
+
+let bundle
+try {
+ bundle = fs.readJSONSync(bundlePath)
+} catch (error) {
+ logger.error(`Failed to read bundle file: ${error.message}`)
+ process.exit(1)
+} Also add
🤖 Prompt for AI Agents
|
||
const jsonBuffer = Buffer.from(JSON.stringify(bundle)) | ||
const boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW' | ||
const formHeaders = { | ||
'Content-Type': `multipart/form-data; boundary=${boundary}`, | ||
} | ||
|
||
let body = `--${boundary}\r\n` | ||
body += 'Content-Disposition: form-data; name="file"; filename="bundle.json"\r\n' | ||
body += 'Content-Type: application/json\r\n\r\n' | ||
body += jsonBuffer.toString() + `\r\n--${boundary}--` | ||
Comment on lines
+18
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use FormData API instead of manual multipart construction. Manually constructing multipart/form-data is error-prone and hard to maintain. The current implementation uses a fixed boundary string which could potentially cause conflicts. Apply this diff to use the built-in FormData API: -const jsonBuffer = Buffer.from(JSON.stringify(bundle))
-const boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW'
-const formHeaders = {
- 'Content-Type': `multipart/form-data; boundary=${boundary}`,
-}
-
-let body = `--${boundary}\r\n`
-body += 'Content-Disposition: form-data; name="file"; filename="bundle.json"\r\n'
-body += 'Content-Type: application/json\r\n\r\n'
-body += jsonBuffer.toString() + `\r\n--${boundary}--`
+const formData = new FormData()
+const blob = new Blob([JSON.stringify(bundle)], { type: 'application/json' })
+formData.append('file', blob, 'bundle.json') And update the fetch call: -fetch(backend_url, {
+fetch(BACKEND_URL, {
method: 'POST',
- headers: formHeaders,
- body: body,
+ body: formData,
}) 🤖 Prompt for AI Agents
|
||
|
||
fetch(backend_url, { | ||
method: 'POST', | ||
headers: formHeaders, | ||
body: body, | ||
}) | ||
.then(response => response.json()) | ||
.then(data => { | ||
logger.success('File uploaded successfully:', data) | ||
}) | ||
.catch(error => { | ||
logger.error('Error uploading file:', error) | ||
}) | ||
Comment on lines
+29
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve error handling and add input validation. The current error handling is basic and doesn't provide proper exit codes for script automation. Additionally, there's no validation of the backend URL. Apply this diff to improve error handling: +// Validate backend URL
+if (!BACKEND_URL) {
+ logger.error('BACKEND_URL environment variable is not set')
+ process.exit(1)
+}
+
+try {
+ new URL(BACKEND_URL)
+} catch (error) {
+ logger.error(`Invalid backend URL: ${BACKEND_URL}`)
+ process.exit(1)
+}
+
-fetch(backend_url, {
+fetch(BACKEND_URL, {
method: 'POST',
body: formData,
})
- .then(response => response.json())
+ .then(async response => {
+ if (!response.ok) {
+ throw new Error(`HTTP error! status: ${response.status}`)
+ }
+ return response.json()
+ })
.then(data => {
logger.success('File uploaded successfully:', data)
+ process.exit(0)
})
.catch(error => {
logger.error('Error uploading file:', error)
+ process.exit(1)
})
🤖 Prompt for AI Agents
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Specify the exact env key & give an example
Bullet 3 just says “在 .env.local 中配置访问后端接口的路径” but never names the key. The accompanying code and backend doc call it
backend_url
, yet that string never appears here.Add the key name and a minimal example to prevent misconfiguration:
🤖 Prompt for AI Agents