xiongzhu %!s(int64=2) %!d(string=hai) anos
pai
achega
d4a97f4c37
Modificáronse 2 ficheiros con 45 adicións e 1 borrados
  1. 1 1
      src/paper/paper.service.ts
  2. 44 0
      testupload.ts

+ 1 - 1
src/paper/paper.service.ts

@@ -8,7 +8,7 @@ import { PageRequest } from 'src/common/dto/page-request'
 import { CreatePaperOrderDto } from './dto/create-order.dto'
 import { genPaper } from './paper-gen'
 
-@Injectable()
+@Injectable() 
 export class PaperService implements OnModuleInit {
     constructor(
         @InjectRepository(PaperOrder)

+ 44 - 0
testupload.ts

@@ -0,0 +1,44 @@
+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'
+import * as path from 'path'
+
+async function uploadDoc(title, content) {
+    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 = `doc/${title}_${Date.now()}.docx`
+
+    const upload = new Upload({
+        client: s3,
+        params: {
+            ACL: 'public-read',
+            Bucket: 'nebuai',
+            Key: key,
+            Body: stream
+        }
+    })
+
+    const { execa } = await (eval('import("execa")') as Promise<typeof import('execa')>)
+    const p = execa('pandoc', ['-f', 'markdown', '-t', 'docx'])
+    Readable.from(content).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}`
+}
+
+uploadDoc('test', '# test')