-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
149 lines (123 loc) · 3.92 KB
/
app.js
File metadata and controls
149 lines (123 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const WebSocket = require('ws');
const { default: axios } = require('axios');
const { uploadToS3, getFilesFromS3 } = require('./lib/utils');
const express = require('express');
const http = require('http');
// const serverless = require('serverless-http');
const cors = require('cors')
const app = express();
const server = http.createServer(app);
// middleware
app.use(cors());
app.use(express.json());
// Test route
app.get('/', (req, res) => {
res.send('Hello from goPdf')
});
// get all files from S3 bucket
app.get('/view', (req, res) => {
const { client_id, key } = req.query;
getFilesFromS3(client_id, key).then((files) => {
files = files.map((file) => {
return {
url: `https://pdf-expert.s3.ap-south-1.amazonaws.com/${file.Key}`,
pdfKey: file.Key.split('/')[1],
fileName: file.Key.split('/')[2],
lastModified: file.LastModified,
size: file.Size,
}
});
res.json({
files: files, query: {
client_id: client_id,
key: key
}, total: files.length
}
);
}).catch((error) => {
console.error(error);
res.send('Error fetching files from S3');
});
});
app.post('/shorturl', (req, res) => {
let data = JSON.stringify({
"long_url": req.body.url,
});
let config = {
method: 'post',
maxBodyLength: Infinity,
url: 'https://api-ssl.bitly.com/v4/shorten',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ` + process.env.BITLY_TOKEN
},
data: data
};
axios.request(config)
.then((response) => {
// console.log(JSON.stringify(response.data));
res.json({
url: response.data.link,
id: response.data.id
});
})
.catch((error) => {
console.log(error);
});
})
// Create a WebSocket server
const wss = new WebSocket.Server({ server });
// Store connected clients with unique client_ids
const clients = new Map();
// Function to broadcast data to specific client
function broadcastData(clientclient_id, data) {
if (clients.has(clientclient_id)) {
const client = clients.get(clientclient_id);
client.send(data);
}
}
// WebSocket server connection event
wss.on('connection', (ws) => {
// WebSocket message event
ws.on('message', async (message) => {
// Handle incoming data from clients
const data = JSON.parse(message);
const { client_id, base64, file_id } = data;
clients.set(client_id, ws);
console.log(`Client ${client_id} connected.`);
try {
// Extract images from PDF
let res = await axios.post(process.env.SERVERLESS_URL, {
base64: base64
})
let images = res.data.images;
// Upload images to S3
for (let i = 0; i < images.length; i++) {
let image = images[i];
let url = await uploadToS3(image, client_id, file_id);
// Broadcast data to client
broadcastData(client_id, JSON.stringify({
client_id: client_id,
url: url,
total: images.length
}));
}
ws.close();
} catch (error) {
console.error('Error parsing message:', error);
broadcastData(client_id, JSON.stringify({
client_id: client_id,
error: 'Error parsing message'
}));
}
});
// WebSocket close event
ws.on('close', () => {
console.log('Connection to the WebSocket server closed.')
});
});
const port = process.env.PORT || 8080;
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
// module.exports = app;