144 lines
4.8 KiB
TypeScript
144 lines
4.8 KiB
TypeScript
import _ from "lodash";
|
||
|
||
import Request from "@/lib/request/Request.ts";
|
||
import { generateImages } from "@/api/controllers/images.ts";
|
||
import { tokenSplit } from "@/api/controllers/core.ts";
|
||
import util from "@/lib/util.ts";
|
||
import { ImagesTaskCache } from '@/api/ImagesTaskCache.ts';
|
||
import TOSService from "@/lib/tos/tos-service.ts";
|
||
import logger from "@/lib/logger.ts";
|
||
|
||
/**
|
||
* 处理图片URL上传到TOS
|
||
* @param imageUrls 图片URL数组
|
||
* @returns TOS URL数组
|
||
*/
|
||
async function uploadImagesToTOS(imageUrls: string[]): Promise<string[]> {
|
||
const tosUrls: string[] = [];
|
||
|
||
for (const imageUrl of imageUrls) {
|
||
try {
|
||
// 从URL获取文件名
|
||
const fileName = `image-${Date.now()}-${Math.random().toString(36).substr(2, 9)}.webp`;
|
||
// 上传到TOS
|
||
const tosUrl = await TOSService.uploadFromUrl(imageUrl, `images/${fileName}`);
|
||
tosUrls.push(tosUrl);
|
||
logger.info(`图片上传到TOS成功: ${imageUrl} -> ${tosUrl}`);
|
||
} catch (error) {
|
||
logger.error(`图片上传到TOS失败: ${imageUrl}`, error);
|
||
// 如果上传失败,保留原URL
|
||
tosUrls.push(imageUrl);
|
||
}
|
||
}
|
||
|
||
return tosUrls;
|
||
}
|
||
|
||
export default {
|
||
prefix: "/v1/images",
|
||
get: {
|
||
"/query": async (request: Request) => {
|
||
const imagesTaskCache = ImagesTaskCache.getInstance();
|
||
request
|
||
.validate("query.task_id", _.isString) // 从 query 中校验
|
||
const {
|
||
task_id,
|
||
} = request.query; // 从 query 中获取
|
||
let res = imagesTaskCache.getTaskStatus(task_id);
|
||
// console.log("查询任务状态", task_id, 'res:',res);
|
||
if(typeof res === 'string'){
|
||
// 任务已完成,检查是否已处理TOS上传
|
||
if (!imagesTaskCache.isTosProcessed(task_id)) {
|
||
// 尚未处理TOS上传,处理图片URL上传到TOS
|
||
try {
|
||
const imageUrls = res.split(',');
|
||
const tosUrls = await uploadImagesToTOS(imageUrls);
|
||
const tosUrlsString = tosUrls.join(',');
|
||
|
||
// 更新缓存中TOS URL并标记为已处理
|
||
imagesTaskCache.finishTask(task_id, -1, tosUrlsString);
|
||
imagesTaskCache.markTosProcessed(task_id);
|
||
|
||
return {
|
||
created: util.unixTimestamp(),
|
||
data:{task_id, url: tosUrlsString, status:-1},
|
||
};
|
||
} catch (error) {
|
||
logger.error(`处理图片TOS上传失败: ${task_id}`, error);
|
||
// 如果上传失败,返回原始URL
|
||
return {
|
||
created: util.unixTimestamp(),
|
||
data:{task_id, url:res, status:-1},
|
||
};
|
||
}
|
||
} else {
|
||
// 已处理TOS上传,直接返回缓存的TOS URL
|
||
return {
|
||
created: util.unixTimestamp(),
|
||
data:{task_id, url:res, status:-1},
|
||
};
|
||
}
|
||
}else{
|
||
return {
|
||
created: util.unixTimestamp(),
|
||
data:{task_id, url:"", status:res||0},
|
||
};
|
||
}
|
||
},
|
||
},
|
||
|
||
post: {
|
||
"/generations": async (request: Request) => {
|
||
request
|
||
// .validate("body.model", v => _.isUndefined(v) || _.isString(v))
|
||
.validate("body.task_id", _.isString)
|
||
.validate("body.prompt", _.isString)
|
||
// .validate("body.negative_prompt", v => _.isUndefined(v) || _.isString(v))
|
||
.validate("body.width", v => _.isUndefined(v) || _.isFinite(v))
|
||
.validate("body.height", v => _.isUndefined(v) || _.isFinite(v))
|
||
// .validate("body.sample_strength", v => _.isUndefined(v) || _.isFinite(v))
|
||
.validate("body.response_format", v => _.isUndefined(v) || _.isString(v))
|
||
.validate("headers.authorization", _.isString);
|
||
// refresh_token切分
|
||
const tokens = tokenSplit(request.headers.authorization);
|
||
// 随机挑选一个refresh_token
|
||
const token = _.sample(tokens);
|
||
const {
|
||
// model,
|
||
task_id,
|
||
prompt,
|
||
// negative_prompt: negativePrompt,
|
||
width,
|
||
height,
|
||
// sample_strength: sampleStrength,
|
||
response_format,
|
||
} = request.body;
|
||
const responseFormat = _.defaultTo(response_format, "url");
|
||
generateImages('jimeng-3.0', task_id, prompt, {
|
||
width,
|
||
height,
|
||
sampleStrength:0.5,
|
||
negativePrompt:"",
|
||
}, token);
|
||
// let data = [];
|
||
// if (responseFormat == "b64_json") {
|
||
// data = (
|
||
// await Promise.all(imageUrls.map((url) => util.fetchFileBASE64(url)))
|
||
// ).map((b64) => ({ b64_json: b64 }));
|
||
// } else {
|
||
// data = imageUrls.map((url) => ({
|
||
// url,
|
||
// }));
|
||
// }
|
||
// return {
|
||
// created: util.unixTimestamp(),
|
||
// data,
|
||
// };
|
||
return {
|
||
created: util.unixTimestamp(),
|
||
data:'success',
|
||
};
|
||
},
|
||
},
|
||
};
|