yuanjiajun commited on
Commit ·
9f42a0f
1
Parent(s): f4d13e9
feat: docx问题处理
Browse files- package-lock.json +0 -0
- package.json +1 -0
- src/service/article-service.ts +7 -13
package-lock.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
package.json
CHANGED
|
@@ -17,6 +17,7 @@
|
|
| 17 |
"license": "ISC",
|
| 18 |
"dependencies": {
|
| 19 |
"axios": "^1.7.7",
|
|
|
|
| 20 |
"fs": "0.0.1-security",
|
| 21 |
"html-docx-js": "^0.3.1",
|
| 22 |
"html-to-docx": "^1.8.0",
|
|
|
|
| 17 |
"license": "ISC",
|
| 18 |
"dependencies": {
|
| 19 |
"axios": "^1.7.7",
|
| 20 |
+
"docx": "^9.0.2",
|
| 21 |
"fs": "0.0.1-security",
|
| 22 |
"html-docx-js": "^0.3.1",
|
| 23 |
"html-to-docx": "^1.8.0",
|
src/service/article-service.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
| 1 |
-
import
|
| 2 |
-
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, uploadFile } from '@/utils';
|
| 7 |
import { emotionalStoryTopic } from '@/const';
|
|
|
|
| 8 |
|
| 9 |
-
// const uploadDir = path.join(__dirname, '../../uploads');
|
| 10 |
-
const uploadDir = '/app/static'
|
| 11 |
|
| 12 |
async function getImageBase64ByText(text: string, hfApiKey: string) {
|
| 13 |
console.log(`------------ 开始获取图片,原文案:${text} ---------------`);
|
|
@@ -35,8 +31,6 @@ export const processArticleServe = async (data: { title: string; content: string
|
|
| 35 |
const paragraphs = content.split(/\\n|\n/).filter((p) => p.trim() !== '');
|
| 36 |
const imageParagraphCount = Math.max(3, Math.ceil(paragraphs.length / (3 * 2)));
|
| 37 |
|
| 38 |
-
// await preheat(config.hfApiKey);
|
| 39 |
-
|
| 40 |
// 遍历段落,根据段落内容调用接口生成图片并插入到HTML中
|
| 41 |
const htmlArray = [`<h1>${title}</h1>`];
|
| 42 |
let promiseArray = [];
|
|
@@ -91,11 +85,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
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
-
await uploadFile(docxBuffer, outputFilename)
|
| 99 |
|
| 100 |
return {
|
| 101 |
article: outputFilename,
|
|
@@ -108,5 +102,5 @@ export const processArticleServe = async (data: { title: string; content: string
|
|
| 108 |
};
|
| 109 |
|
| 110 |
export const processArticleTopicServe = async () => {
|
| 111 |
-
return getRandomValueFromArray(emotionalStoryTopic)
|
| 112 |
};
|
|
|
|
| 1 |
+
import { bufferToBase64ImageSrc, delay, getFluxImageBuffer, getRandomValueFromArray, retryAsync } from '@/utils';
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import { requestQw, uploadFile } from '@/utils';
|
| 4 |
import { emotionalStoryTopic } from '@/const';
|
| 5 |
+
const htmlToDocx = require('html-to-docx');
|
| 6 |
|
|
|
|
|
|
|
| 7 |
|
| 8 |
async function getImageBase64ByText(text: string, hfApiKey: string) {
|
| 9 |
console.log(`------------ 开始获取图片,原文案:${text} ---------------`);
|
|
|
|
| 31 |
const paragraphs = content.split(/\\n|\n/).filter((p) => p.trim() !== '');
|
| 32 |
const imageParagraphCount = Math.max(3, Math.ceil(paragraphs.length / (3 * 2)));
|
| 33 |
|
|
|
|
|
|
|
| 34 |
// 遍历段落,根据段落内容调用接口生成图片并插入到HTML中
|
| 35 |
const htmlArray = [`<h1>${title}</h1>`];
|
| 36 |
let promiseArray = [];
|
|
|
|
| 85 |
|
| 86 |
if (config.output === 'docx') {
|
| 87 |
const outputFilename = `docx-${Date.now()}.docx`;
|
| 88 |
+
const docxBuffer = await htmlToDocx(htmlWithImages, {
|
| 89 |
+
orientation: 'portrait',
|
| 90 |
+
});
|
| 91 |
|
| 92 |
+
await uploadFile(docxBuffer, outputFilename);
|
| 93 |
|
| 94 |
return {
|
| 95 |
article: outputFilename,
|
|
|
|
| 102 |
};
|
| 103 |
|
| 104 |
export const processArticleTopicServe = async () => {
|
| 105 |
+
return getRandomValueFromArray(emotionalStoryTopic);
|
| 106 |
};
|