gitdeem commited on
Commit
d17b633
·
verified ·
1 Parent(s): e85cd64

Upload 2 files

Browse files
Files changed (2) hide show
  1. Dockerfile +3 -0
  2. app.js +57 -57
Dockerfile CHANGED
@@ -6,4 +6,7 @@ COPY . /app
6
 
7
  RUN npm install
8
 
 
 
 
9
  CMD ["npm", "start"]
 
6
 
7
  RUN npm install
8
 
9
+ RUN mkdir /app/images \
10
+ && chmod 777 /app/images
11
+
12
  CMD ["npm", "start"]
app.js CHANGED
@@ -2,17 +2,29 @@ import express from "express";
2
  import bodyParser from "body-parser";
3
  import dotenv from "dotenv";
4
  import fetch from "node-fetch";
 
 
 
5
  dotenv.config();
6
 
7
  const app = express();
8
  app.use(bodyParser.json());
9
 
 
 
 
 
 
 
 
 
 
10
  app.get("/", (req, res) => {
11
  res.send(`
12
  <html>
13
  <head>
14
  <title>OPENAI</title>
15
- </head
16
  <body>
17
  <h1>OpenAI</h1>
18
  <p>Congratulations! Your project has been successfully deployed.</p>
@@ -22,7 +34,7 @@ app.get("/", (req, res) => {
22
  });
23
 
24
  app.post("/deem/v1/chat/completions", async (req, res) => {
25
- let token; // 将 token 的定义提到函数的开头
26
  const authHeader =
27
  req.headers["authorization"] || req.headers["Authorization"];
28
  if (!authHeader) {
@@ -31,7 +43,7 @@ app.post("/deem/v1/chat/completions", async (req, res) => {
31
  errmsg: "Unauthorized.",
32
  });
33
  } else {
34
- token = authHeader.split(" ")[1]; // 删除 const 定义
35
  if (!token) {
36
  return res.status(401).json({
37
  code: 401,
@@ -45,32 +57,24 @@ app.post("/deem/v1/chat/completions", async (req, res) => {
45
  const messages = data.messages;
46
  const lastMessage = messages[messages.length - 1];
47
  const messagesContent = lastMessage.content;
48
- //const messagesContent = messages.map(m => m.content).join(' ');
49
 
50
- //const resp = await fetch(`https://aihh.api.ecylt.top/?key=${token}&prompt=${messagesContent}`);
51
- //const imgUrl = resp.url;//直接获取图片地址
52
- //或者 imgUrl = https://aihh.api.ecylt.top/?key=${token}&prompt=${messagesContent}
53
-
54
- const apiUrl = `https://aihh.api.ecylt.top/?key=${token}&prompt=${messagesContent}`
55
 
56
  const apiResponse = await fetch(apiUrl);
57
  if (!apiResponse.ok) {
58
  throw new Error("文生图服务失效:" + apiResponse.status);
59
  }
60
- const imageBlob = await apiResponse.blob();
61
- const formData = new FormData();
62
- formData.append("file", imageBlob, "image.jpg");
63
-
64
- const uploadResponse = await fetch("https://img.deem.love/upload", {
65
- method: 'POST',
66
- body: formData
67
- });
68
-
69
- if (!uploadResponse.ok) {
70
- throw new Error("图片上传失败");
71
- }
72
- const uploadResult = await uploadResponse.json();
73
- const imgUrl = "https://img.deem.love" + uploadResult[0].src;
74
 
75
  console.log(imgUrl);
76
 
@@ -100,15 +104,16 @@ app.post("/deem/v1/chat/completions", async (req, res) => {
100
  res.write("data: [DONE]\n\n");
101
  res.end();
102
  } else {
103
- res.status(500).json({ error: "地址获取失效" });
104
  }
105
  } catch (error) {
106
  console.error("Error:", error);
 
107
  }
108
  });
109
 
110
  app.post("/deem/v1/images/generations", async (req, res) => {
111
- let token; // 将 token 的定义提到函数的开头
112
  const authHeader =
113
  req.headers["authorization"] || req.headers["Authorization"];
114
  if (!authHeader) {
@@ -117,7 +122,7 @@ app.post("/deem/v1/images/generations", async (req, res) => {
117
  errmsg: "Unauthorized.",
118
  });
119
  } else {
120
- token = authHeader.split(" ")[1]; // 删除 const 定义
121
  if (!token) {
122
  return res.status(401).json({
123
  code: 401,
@@ -130,49 +135,44 @@ app.post("/deem/v1/images/generations", async (req, res) => {
130
  const data = req.body;
131
  const messages = data.prompt;
132
 
133
- const apiUrl = `https://aihh.api.ecylt.top/?key=${token}&prompt=${messages}`
134
 
135
  const apiResponse = await fetch(apiUrl);
136
  if (!apiResponse.ok) {
137
  throw new Error("文生图服务失效:" + apiResponse.status);
138
  }
139
- const imageBlob = await apiResponse.blob();
140
- const formData = new FormData();
141
- formData.append("file", imageBlob, "image.jpg");
142
-
143
- const uploadResponse = await fetch("https://img.deem.love/upload", {
144
- method: 'POST',
145
- body: formData
146
- });
147
-
148
- if (!uploadResponse.ok) {
149
- throw new Error("图片上传失败");
150
- }
151
- const uploadResult = await uploadResponse.json();
152
- const imgUrl = "https://img.deem.love" + uploadResult[0].src;
153
 
154
  console.log(imgUrl);
155
 
156
  if (imgUrl) {
157
  const chunkCreated = Math.floor(Date.now() / 1000);
158
 
159
- res.write(
160
- JSON.stringify({
161
- created: chunkCreated,
162
- data: [
163
- {
164
- url: imgUrl,
165
- },
166
- ],
167
- })
168
- );
169
- res.end();
170
  } else {
171
- res.status(500).json({ error: "地址获取失效" });
172
- }
173
  } catch (error) {
174
- console.error("Error:", error);
175
- }
 
176
  });
177
 
178
- app.listen(process.env.PORT || 3000);
 
2
  import bodyParser from "body-parser";
3
  import dotenv from "dotenv";
4
  import fetch from "node-fetch";
5
+ import fs from "fs";
6
+ import path from "path";
7
+
8
  dotenv.config();
9
 
10
  const app = express();
11
  app.use(bodyParser.json());
12
 
13
+ // 创建图片保存目录
14
+ const imageDir = path.join(process.cwd(), 'images');
15
+ if (!fs.existsSync(imageDir)) {
16
+ fs.mkdirSync(imageDir);
17
+ }
18
+
19
+ // 静态文件服务
20
+ app.use('/images', express.static(imageDir));
21
+
22
  app.get("/", (req, res) => {
23
  res.send(`
24
  <html>
25
  <head>
26
  <title>OPENAI</title>
27
+ </head>
28
  <body>
29
  <h1>OpenAI</h1>
30
  <p>Congratulations! Your project has been successfully deployed.</p>
 
34
  });
35
 
36
  app.post("/deem/v1/chat/completions", async (req, res) => {
37
+ let token;
38
  const authHeader =
39
  req.headers["authorization"] || req.headers["Authorization"];
40
  if (!authHeader) {
 
43
  errmsg: "Unauthorized.",
44
  });
45
  } else {
46
+ token = authHeader.split(" ")[1];
47
  if (!token) {
48
  return res.status(401).json({
49
  code: 401,
 
57
  const messages = data.messages;
58
  const lastMessage = messages[messages.length - 1];
59
  const messagesContent = lastMessage.content;
 
60
 
61
+ const apiUrl = `https://aihh.api.ecylt.top/?key=${token}&prompt=${messagesContent}`;
 
 
 
 
62
 
63
  const apiResponse = await fetch(apiUrl);
64
  if (!apiResponse.ok) {
65
  throw new Error("文生图服务失效:" + apiResponse.status);
66
  }
67
+ const imageBuffer = await apiResponse.buffer();
68
+
69
+ // 生成唯一的文件名
70
+ const fileName = `image_${Date.now()}.jpg`;
71
+ const filePath = path.join(imageDir, fileName);
72
+
73
+ // 保存图片到本地
74
+ fs.writeFileSync(filePath, imageBuffer);
75
+
76
+ // 构建本地图片URL
77
+ const imgUrl = `${req.protocol}://${req.get('host')}/images/${fileName}`;
 
 
 
78
 
79
  console.log(imgUrl);
80
 
 
104
  res.write("data: [DONE]\n\n");
105
  res.end();
106
  } else {
107
+ res.status(500).json({ error: "图片保存失败" });
108
  }
109
  } catch (error) {
110
  console.error("Error:", error);
111
+ res.status(500).json({ error: error.message });
112
  }
113
  });
114
 
115
  app.post("/deem/v1/images/generations", async (req, res) => {
116
+ let token;
117
  const authHeader =
118
  req.headers["authorization"] || req.headers["Authorization"];
119
  if (!authHeader) {
 
122
  errmsg: "Unauthorized.",
123
  });
124
  } else {
125
+ token = authHeader.split(" ")[1];
126
  if (!token) {
127
  return res.status(401).json({
128
  code: 401,
 
135
  const data = req.body;
136
  const messages = data.prompt;
137
 
138
+ const apiUrl = `https://aihh.api.ecylt.top/?key=${token}&prompt=${messages}`;
139
 
140
  const apiResponse = await fetch(apiUrl);
141
  if (!apiResponse.ok) {
142
  throw new Error("文生图服务失效:" + apiResponse.status);
143
  }
144
+ const imageBuffer = await apiResponse.buffer();
145
+
146
+ // 生成唯一的文件名
147
+ const fileName = `image_${Date.now()}.jpg`;
148
+ const filePath = path.join(imageDir, fileName);
149
+
150
+ // 保存图片到本地
151
+ fs.writeFileSync(filePath, imageBuffer);
152
+
153
+ // 构建本地图片URL
154
+ const imgUrl = `${req.protocol}://${req.get('host')}/images/${fileName}`;
 
 
 
155
 
156
  console.log(imgUrl);
157
 
158
  if (imgUrl) {
159
  const chunkCreated = Math.floor(Date.now() / 1000);
160
 
161
+ res.json({
162
+ created: chunkCreated,
163
+ data: [
164
+ {
165
+ url: imgUrl,
166
+ },
167
+ ],
168
+ });
 
 
 
169
  } else {
170
+ res.status(500).json({ error: "图片保存失败" });
171
+ }
172
  } catch (error) {
173
+ console.error("Error:", error);
174
+ res.status(500).json({ error: error.message });
175
+ }
176
  });
177
 
178
+ app.listen(process.env.PORT || 3000);