-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
208 lines (174 loc) · 6.5 KB
/
server.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import dotenv from 'dotenv'
dotenv.config()
import axios from 'axios'
import express from 'express'
import path from 'path'
import { fileURLToPath } from 'url';
// import AWS from 'aws-sdk';
import fs from 'fs';
import cors from 'cors';
import { checkIfInS3, saveLatLongsToS3, getFileDirectory, getLatLongsFromS3 } from './Server/awsHelper.js'
import { createFrames } from './Server/frameHelper.js'
import redis from 'redis'
export let progressLog = {}
const app = express()
app.use(cors())
app.use(express.json({ limit: '2mb' }))
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const redisClient = redis.createClient({
url: process.env.REDIS_URL,
tls: {} //
});
redisClient.on("error", (err) => console.log("Redis Client Error", err));
(async () => {
await redisClient.connect();
console.log('Redis client connected');
})();
process.on('SIGINT', () => {
redisClient.quit().then(() => {
console.log('Redis client disconnected');
process.exit(0);
});
});
const getAthlete = async () => {
try {
let { data } = await axios.get('https://www.strava.com/api/v3/athlete/activities', {
headers: {
Authorization: `Bearer ${process.env.ACCESS_TOKEN}`
}
})
console.log(data)
} catch (error) {
console.log(error)
}
}
app.use(express.static(path.join(__dirname, '.', 'dist')))
app.get('/', (req, res) => {
res.setHeader('Surrogate-Control', 'no-store');
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache'); res.setHeader('Expires', '0');
res.sendFile(path.join(__dirname, 'dist/index.html'))
})
app.get('/api/images', (req, res) => {
const start = parseInt(req.query.start, 10) || 0;
const end = parseInt(req.query.end, 10) || 150; // default chunk size
const directoryPath = path.join(__dirname, 'accident-ride-frames-lofi-compressed');
fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error('Error reading directory:', err);
return res.status(500).send('Server error');
}
// Sort and filter the files based on the frame number
const sortedFilteredFiles = files
.sort((a, b) => {
const numA = parseInt(a.replace('frame', '').replace('-fs8.webp', ''), 10);
const numB = parseInt(b.replace('frame', '').replace('-fs8.webp', ''), 10);
return numA - numB; // sort in ascending order
})
.slice(start, end);
res.json(sortedFilteredFiles);
});
});
app.get('/api/simplestatus', async (req, res) => {
let activityId = req.query.activityId;
let activityInS3 = await checkIfInS3(activityId)
let videoInS3 = await checkIfInS3(`${activityId}/video.mp4`)
if (videoInS3) {
res.json({ status: 'videoInS3' })
} else if (activityInS3) {
res.json({
status: 'activityInS3',
progress: progressLog[activityId] ? progressLog[activityId].length : 0})
} else {
res.json({ status: 'notInS3' })
}
})
app.get('/api/customactivities', async (req, res) => {
let activities = await getFileDirectory(`custom`)
let activitiesArray = Array.from(activities)
let deduped = new Set()
activitiesArray.map(activity => {
let parsed = activity.split('/')
let result = parsed ? parsed[1] : activity
deduped.add(result)
})
let dedupedArray = Array.from(deduped)
res.send(dedupedArray)
})
app.post('/api/customactivities', async (req, res) => {
let name = req.body.name;
let latlongs = req.body.latlongs;
let latlongsjson = JSON.parse(latlongs)
let size = req.body.size;
// check if already in S3, if so return error
if (await checkIfInS3(`custom/${name}`)) {
res.status(400).send(`Activity ${name} already exists`)
} else {
await saveLatLongsToS3(`custom/${name}`, latlongsjson)
progressLog[`custom/${name}`] = [`created custom activity ${name}`]
let url = 'http://soundslikeagoodti.me/api/video'
if (process.env.ENVIRONMENT == 'dev') url = 'http://localhost:8000/api/video'
try {
await axios.post(url, {
activityId: `custom/${name}`,
latlongs: latlongs,
size: size
})
} catch (error) {
console.log(error)
}
res.send("Ok, saved " + name)
}
})
app.get('/api/progress', (req, res) => {
let activityId = req.query.activityId
// fs.writeFileSync('./progressLog.json', JSON.stringify(progressLog, null, 2))
if (progressLog[activityId]) progressLog[activityId] = progressLog[activityId].slice(-50)
res.send(progressLog[activityId])
})
app.post('/api/progress', (req, res) => {
let activityId = req.body.activityId;
let progress = req.body.progress;
if (!progressLog[activityId]) progressLog[activityId] = []
progressLog[activityId].push(progress) // add progress
res.send("Ok, updated progress for activity " + activityId)
})
const enqueueFrameGenerationTask = async (activityId, size) => {
let queueName = 'frame-queue';
// if (activityId.split('/')[0] == 'custom') queueName = 'frame-queue-test'; // The same queue your worker listens on
const task = { activityId, size };
try {
await redisClient.rPush(queueName, JSON.stringify(task));
}
catch (err) {
console.error(err);
}
};
app.post('/api/video', async (req, res) => {
let activityId = req.body.activityId;
let latlongs = req.body.latlongs;
let size = req.body.size;
let videosize = size
if (size !== "large") videosize = "small"
res.send("Ok, generating video for activity " + activityId)
progressLog[activityId] = []
// add latlongs to s3, if not custom
if (activityId.split('/')[0] !== 'custom') {
await saveLatLongsToS3(activityId, latlongs);
} else {
// get latlongs from s3
latlongs = await getLatLongsFromS3(activityId);
}
// generate frame generation task
await enqueueFrameGenerationTask(activityId, videosize);
})
app.use('*', (req, res) => {
res.setHeader('Surrogate-Control', 'no-store');
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache'); res.setHeader('Expires', '0');
res.sendFile(path.join(__dirname, 'dist/index.html'))
})
app.listen(8000, () => {
console.log('listening on port 8000')
})