yuanjiajun commited on
Commit ·
9de72d6
1
Parent(s): f1d5f29
feat: 静态服务
Browse files- src/{const.ts → const}/article.ts +0 -0
- src/const/common.ts +6 -0
- src/const/index.ts +2 -0
- src/service/article-service.ts +4 -4
- src/utils/index.ts +1 -0
- src/utils/static.ts +52 -0
src/{const.ts → const}/article.ts
RENAMED
|
File without changes
|
src/const/common.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export interface ApiError {
|
| 2 |
+
status?: number;
|
| 3 |
+
message: string;
|
| 4 |
+
details?: any;
|
| 5 |
+
}
|
| 6 |
+
|
src/const/index.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export * from './article'
|
| 2 |
+
export * from './common'
|
src/service/article-service.ts
CHANGED
|
@@ -3,8 +3,8 @@ import htmlDocx from 'html-docx-js';
|
|
| 3 |
import path from 'path';
|
| 4 |
import { blobToArrayBuffer, bufferToBase64ImageSrc, delay, getFluxImageBuffer, getRandomValueFromArray, retryAsync } from '@/utils';
|
| 5 |
|
| 6 |
-
import { requestQw } from '@/utils';
|
| 7 |
-
import { emotionalStoryTopic } from '@/const
|
| 8 |
|
| 9 |
// const uploadDir = path.join(__dirname, '../../uploads');
|
| 10 |
const uploadDir = '/app/static'
|
|
@@ -91,11 +91,11 @@ export const processArticleServe = async (data: { title: string; content: string
|
|
| 91 |
|
| 92 |
if (config.output === 'docx') {
|
| 93 |
const outputFilename = `docx-${Date.now()}.docx`;
|
| 94 |
-
const outputPath = path.join(uploadDir, outputFilename);
|
| 95 |
const blob = htmlDocx.asBlob(htmlWithImages) as Blob;
|
| 96 |
const arrayBuffer = (await blobToArrayBuffer(blob)) as ArrayBuffer;
|
| 97 |
const docxBuffer = Buffer.from(arrayBuffer);
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
return {
|
| 101 |
article: outputFilename,
|
|
|
|
| 3 |
import path from 'path';
|
| 4 |
import { blobToArrayBuffer, bufferToBase64ImageSrc, delay, getFluxImageBuffer, getRandomValueFromArray, retryAsync } from '@/utils';
|
| 5 |
|
| 6 |
+
import { requestQw, uploadFile } from '@/utils';
|
| 7 |
+
import { emotionalStoryTopic } from '@/const';
|
| 8 |
|
| 9 |
// const uploadDir = path.join(__dirname, '../../uploads');
|
| 10 |
const uploadDir = '/app/static'
|
|
|
|
| 91 |
|
| 92 |
if (config.output === 'docx') {
|
| 93 |
const outputFilename = `docx-${Date.now()}.docx`;
|
|
|
|
| 94 |
const blob = htmlDocx.asBlob(htmlWithImages) as Blob;
|
| 95 |
const arrayBuffer = (await blobToArrayBuffer(blob)) as ArrayBuffer;
|
| 96 |
const docxBuffer = Buffer.from(arrayBuffer);
|
| 97 |
+
|
| 98 |
+
await uploadFile(docxBuffer, outputFilename)
|
| 99 |
|
| 100 |
return {
|
| 101 |
article: outputFilename,
|
src/utils/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
export * from './common';
|
| 2 |
export * from './file-utils';
|
| 3 |
export * from './hugging-face';
|
|
|
|
|
|
| 1 |
export * from './common';
|
| 2 |
export * from './file-utils';
|
| 3 |
export * from './hugging-face';
|
| 4 |
+
export * from './static';
|
src/utils/static.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import axios from 'axios';
|
| 2 |
+
|
| 3 |
+
const BASE_URL = 'http://localhost:3001';
|
| 4 |
+
|
| 5 |
+
// 定义响应类型
|
| 6 |
+
interface FileNameListResponse {
|
| 7 |
+
files: string[];
|
| 8 |
+
}
|
| 9 |
+
interface FileUploadResponse {
|
| 10 |
+
message: string;
|
| 11 |
+
}
|
| 12 |
+
interface FileDeleteResponse {
|
| 13 |
+
message: string;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
// 获取文件名列表
|
| 17 |
+
async function getFileNameList(): Promise<FileNameListResponse> {
|
| 18 |
+
try {
|
| 19 |
+
const response = await axios.get<FileNameListResponse>(`${BASE_URL}/file/getFileNameList`);
|
| 20 |
+
return response.data;
|
| 21 |
+
} catch (error: any) {
|
| 22 |
+
throw new Error(error.response?.data?.error);
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// 上传文件
|
| 27 |
+
async function uploadFile(buffer: Buffer, fileName: string): Promise<FileUploadResponse> {
|
| 28 |
+
try {
|
| 29 |
+
const response = await axios.post<FileUploadResponse>(`${BASE_URL}/file/uploadFile`, buffer, {
|
| 30 |
+
headers: {
|
| 31 |
+
'Content-Type': 'application/octet-stream',
|
| 32 |
+
'Content-Disposition': `attachment; filename=${fileName}`,
|
| 33 |
+
},
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
return response.data;
|
| 37 |
+
} catch (error: any) {
|
| 38 |
+
throw new Error(error.response?.data?.error);
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
// 删除文件
|
| 43 |
+
async function deleteFile(fileName: string): Promise<FileDeleteResponse> {
|
| 44 |
+
try {
|
| 45 |
+
const response = await axios.post<FileDeleteResponse>(`${BASE_URL}/file/deleteFile`, { fileName });
|
| 46 |
+
return response.data;
|
| 47 |
+
} catch (error: any) {
|
| 48 |
+
throw new Error(error.response?.data?.error);
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
export { getFileNameList, uploadFile, deleteFile };
|