shashwatIDR commited on
Commit
2a07df7
·
verified ·
1 Parent(s): ea94f5e

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +40 -1
server.js CHANGED
@@ -8,6 +8,7 @@ import bcrypt from 'bcrypt';
8
  import jwt from 'jsonwebtoken';
9
  import FormData from 'form-data';
10
  import pg from 'pg';
 
11
 
12
  dotenv.config();
13
 
@@ -17,7 +18,7 @@ const app = express();
17
  const port = process.env.PORT || 3000;
18
 
19
  // Middleware
20
- app.use(cors());
21
  app.use(express.json());
22
  app.use(express.static('public'));
23
 
@@ -921,6 +922,44 @@ app.post('/api/admin/process-old', async (req, res) => {
921
  }
922
  });
923
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
924
  app.listen(port, () => {
925
  console.log(`Server running on port ${port}`);
926
  });
 
8
  import jwt from 'jsonwebtoken';
9
  import FormData from 'form-data';
10
  import pg from 'pg';
11
+ import fetch from 'node-fetch';
12
 
13
  dotenv.config();
14
 
 
18
  const port = process.env.PORT || 3000;
19
 
20
  // Middleware
21
+ app.use(cors({ origin: '*', credentials: true }));
22
  app.use(express.json());
23
  app.use(express.static('public'));
24
 
 
922
  }
923
  });
924
 
925
+ // Proxy route for HuggingFace/Gradio images
926
+ app.get('/proxy-image', async (req, res) => {
927
+ const { url, proxied } = req.query;
928
+ if (!url) {
929
+ return res.status(400).send('Missing url parameter');
930
+ }
931
+ if (!url.startsWith('https://yanze-pulid-flux.hf.space/')) {
932
+ return res.status(403).send('Forbidden: Only allowed to proxy from yanze-pulid-flux.hf.space');
933
+ }
934
+ try {
935
+ const response = await fetch(url, {
936
+ method: 'GET',
937
+ headers: {
938
+ 'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
939
+ 'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8,hi;q=0.7',
940
+ 'Cache-Control': 'no-cache',
941
+ 'Pragma': 'no-cache',
942
+ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
943
+ }
944
+ });
945
+ if (!response.ok) {
946
+ return res.status(response.status).send('Failed to fetch image');
947
+ }
948
+ res.set('Access-Control-Allow-Origin', '*');
949
+ res.set('Access-Control-Allow-Credentials', 'true');
950
+ res.set('Content-Type', response.headers.get('content-type') || 'image/png');
951
+ res.set('Content-Disposition', response.headers.get('content-disposition') || 'inline');
952
+ // If proxied param is set, respond with the proxied URL
953
+ if (proxied === '1') {
954
+ const proxiedUrl = `https://shashwatidr-vision.hf.space/proxy-image?url=${encodeURIComponent(url)}`;
955
+ return res.json({ proxiedUrl });
956
+ }
957
+ response.body.pipe(res);
958
+ } catch (err) {
959
+ res.status(500).send('Proxy error: ' + err.message);
960
+ }
961
+ });
962
+
963
  app.listen(port, () => {
964
  console.log(`Server running on port ${port}`);
965
  });