T1ckbase commited on
Commit
52f6b1b
·
1 Parent(s): 9f8d826
Files changed (2) hide show
  1. main.ts +37 -11
  2. tmp/foo.txt +0 -1
main.ts CHANGED
@@ -17,7 +17,24 @@ app.use(logger());
17
 
18
  app.get('/', (c) => c.text('Hello Hono!'));
19
 
20
- app.use('/tmp/*', serveStatic({ root: './' }));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  // LM Studio
23
  app.get('/v1/models', (c) => {
@@ -69,7 +86,7 @@ app.post('/v1/chat/completions', async (c) => {
69
 
70
  app.post('/v1/images/generations', async (c) => {
71
  const headers = new Headers(c.req.raw.headers);
72
- headers.delete('Authorization');
73
  headers.has('x-use-cache') || headers.set('x-use-cache', 'false');
74
  console.log('headers:', Object.fromEntries(headers));
75
 
@@ -103,13 +120,22 @@ app.post('/v1/images/generations', async (c) => {
103
  });
104
  if (!response.ok) return response;
105
 
106
- const ext = response.headers.get('content-type')!.substring('image/'.length).toLowerCase();
107
- const image = await response.arrayBuffer();
108
- const fileName = `${crypto.randomUUID()}.${ext}`;
109
- const url = `${headers.get('Host')}/tmp/${fileName}`;
 
 
 
 
 
 
 
 
 
110
 
111
- await ensureDir('/tmp');
112
- await Deno.writeFile(`/tmp/${fileName}`, new Uint8Array(image), { create: true });
113
 
114
  console.log(url);
115
  let data: any = {
@@ -117,16 +143,16 @@ app.post('/v1/images/generations', async (c) => {
117
  };
118
  if (params.response_format === 'b64_json') {
119
  data = {
120
- b64_json: encodeBase64(image),
121
  };
122
  }
123
 
124
- const resposne = {
125
  created: Math.floor(Date.now() / 1000),
126
  data: [data],
127
  };
128
 
129
- return new Response(JSON.stringify(resposne));
130
 
131
  // switch (body.model) {
132
  // case 'flux-dev': {
 
17
 
18
  app.get('/', (c) => c.text('Hello Hono!'));
19
 
20
+ // In-memory storage for images
21
+ const imageCache = new Map<string, { data: Uint8Array; contentType: string }>();
22
+
23
+ // Modified route to serve from in-memory cache instead of filesystem
24
+ app.get('/tmp/:id', async (c) => {
25
+ const id = c.req.param('id');
26
+ const cachedImage = imageCache.get(id);
27
+
28
+ if (!cachedImage) {
29
+ return c.text('Image not found', 404);
30
+ }
31
+
32
+ return new Response(cachedImage.data, {
33
+ headers: {
34
+ 'Content-Type': cachedImage.contentType,
35
+ },
36
+ });
37
+ });
38
 
39
  // LM Studio
40
  app.get('/v1/models', (c) => {
 
86
 
87
  app.post('/v1/images/generations', async (c) => {
88
  const headers = new Headers(c.req.raw.headers);
89
+ // headers.delete('Authorization');
90
  headers.has('x-use-cache') || headers.set('x-use-cache', 'false');
91
  console.log('headers:', Object.fromEntries(headers));
92
 
 
120
  });
121
  if (!response.ok) return response;
122
 
123
+ const contentType = response.headers.get('content-type')!;
124
+ const ext = contentType.substring('image/'.length).toLowerCase();
125
+ const imageArrayBuffer = await response.arrayBuffer();
126
+ const imageData = new Uint8Array(imageArrayBuffer);
127
+
128
+ // Generate a unique ID without the file extension
129
+ const fileId = crypto.randomUUID();
130
+
131
+ // Store in our in-memory cache instead of writing to disk
132
+ imageCache.set(fileId, {
133
+ data: imageData,
134
+ contentType: contentType,
135
+ });
136
 
137
+ const host = headers.get('Host') || c.req.header('Host');
138
+ const url = `${host}/tmp/${fileId}`;
139
 
140
  console.log(url);
141
  let data: any = {
 
143
  };
144
  if (params.response_format === 'b64_json') {
145
  data = {
146
+ b64_json: encodeBase64(imageArrayBuffer),
147
  };
148
  }
149
 
150
+ const responseBody = {
151
  created: Math.floor(Date.now() / 1000),
152
  data: [data],
153
  };
154
 
155
+ return c.json(responseBody);
156
 
157
  // switch (body.model) {
158
  // case 'flux-dev': {
tmp/foo.txt DELETED
@@ -1 +0,0 @@
1
- bar