| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import { execa } from "execa"
- import path from "path"
- import { fileURLToPath } from "url"
- import fs from "fs"
- import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3"
- import { Upload } from "@aws-sdk/lib-storage"
- import { PassThrough, Readable } from "stream"
- import { WritableStreamBuffer } from "stream-buffers"
- const __filename = fileURLToPath(import.meta.url)
- const __dirname = path.dirname(__filename)
- async function uploadUml(plantUml) {
- const s3 = new S3Client({
- region: "oss-cn-hangzhou",
- endpoint: "https://oss-cn-hangzhou.aliyuncs.com",
- credentials: {
- accessKeyId: "PXzJyah5rZfWHIIH",
- secretAccessKey: "e1MS6j0wypXJrw8CM0hObZu8qKbfah"
- }
- })
- const stream = new PassThrough()
- const key = `uml/${Date.now()}.png`
- const upload = new Upload({
- client: s3,
- params: {
- ACL: "public-read",
- Bucket: "nebuai",
- Key: key,
- Body: stream
- }
- })
- const p = execa("java", [
- "-jar",
- "-Djava.awt.headless=true",
- "-DPLANTUML_LIMIT_SIZE=8192",
- `${path.join(__dirname, "plantuml-1.2023.12.jar")}`,
- "-config",
- `${path.join(__dirname, "plantuml.cfg")}`,
- "-tpng",
- "-pipe",
- "-fastfail",
- "-noerror"
- ])
- Readable.from(plantUml).pipe(p.stdin)
- const err = new WritableStreamBuffer()
- p.pipeStdout(stream)
- p.pipeStderr(err)
- try {
- await p
- } catch (error) {
- throw new Error(err.getContents().toString())
- }
- await upload.done()
- return `https://nebuai.oss-cn-hangzhou.aliyuncs.com/${key}`
- }
- export { uploadUml }
|