@@ -26,8 +26,9 @@ function GetCurrentTimeAsString() {
26
26
return "" + ( new Date ( ) ) . valueOf ( )
27
27
}
28
28
29
- let startupTime = GetCurrentTimeAsString ( )
30
- let databaseModificationTime = startupTime
29
+ const startupTime = GetCurrentTimeAsString ( )
30
+ let databaseLastDeleteTime = startupTime
31
+ let databaseLastModificationTime = startupTime
31
32
32
33
function isObjectAllowedInDB ( inObj ) {
33
34
let isAllowed = true ;
@@ -89,7 +90,7 @@ api.get('/get_submission', (req, res) => {
89
90
return
90
91
}
91
92
92
- let etag = req . query . ID > latestReportID ? "0" : startupTime
93
+ let etag = req . query . ID > latestReportID ? "0" : databaseLastDeleteTime
93
94
if ( req . headers [ 'if-none-match' ] == etag ) {
94
95
res . status ( 304 )
95
96
res . send ( )
@@ -100,7 +101,12 @@ api.get('/get_submission', (req, res) => {
100
101
let rows = getSubmissionStatement . all ( [ req . query . ID ] )
101
102
rows = rows . map ( database_common . unpackDatabaseObject )
102
103
103
- res . header ( "Cache-Control" , "public, max-age=120" )
104
+ let cacheLifetime = 300
105
+ if ( rows . length != 0 ) {
106
+ cacheLifetime = 86400
107
+ }
108
+
109
+ res . header ( "Cache-Control" , "public, max-age=" + cacheLifetime )
104
110
res . header ( "ETag" , etag )
105
111
res . send ( JSON . stringify ( rows ) )
106
112
}
@@ -116,19 +122,18 @@ api.get('/get_submission', (req, res) => {
116
122
117
123
const getAllSubmissionsStatement = db . prepare ( "SELECT * FROM Submissions" )
118
124
api . get ( '/get_all_submissions' , ( req , res ) => {
119
- let etag = databaseModificationTime
125
+ let etag = databaseLastModificationTime
120
126
if ( req . headers [ 'if-none-match' ] == etag ) {
121
127
res . status ( 304 )
122
128
res . send ( )
123
129
return
124
130
}
125
131
126
132
try {
127
-
128
133
let rows = getAllSubmissionsStatement . all ( )
129
134
rows = rows . map ( database_common . unpackDatabaseObject )
130
- res . header ( "Cache-Control" , "public, max-age=120 " )
131
- res . header ( "ETag" , databaseModificationTime )
135
+ res . header ( "Cache-Control" , "public, max-age=300 " )
136
+ res . header ( "ETag" , databaseLastModificationTime )
132
137
res . send ( JSON . stringify ( rows ) )
133
138
}
134
139
catch ( e ) {
@@ -205,12 +210,12 @@ api.post('/post_submission', (req, res) => {
205
210
let info = postSubmissionStatement . run ( parameterList )
206
211
207
212
res . send ( "" + info . lastInsertRowid )
208
- databaseModificationTime = GetCurrentTimeAsString ( )
209
- latestReportID = info . lastInsertRowid
213
+ databaseLastModificationTime = GetCurrentTimeAsString ( )
214
+ latestReportID = Math . max ( info . lastInsertRowid , latestReportID )
210
215
211
216
console . log ( `Inserted submission ID ${ info . lastInsertRowid } - ${ newSubmission [ "DXGI_ADAPTER_DESC3.Description" ] } ` )
212
217
213
- notification_handler . notify ( {
218
+ let notification = {
214
219
"embeds" : [
215
220
{
216
221
"title" : newSubmission [ "DXGI_ADAPTER_DESC3.Description" ] ,
@@ -247,7 +252,17 @@ api.post('/post_submission', (req, res) => {
247
252
}
248
253
}
249
254
]
250
- } )
255
+ }
256
+
257
+ if ( process . env . SubmissionRemovalPassword ) {
258
+ notification . embeds [ 0 ] . fields . push ( {
259
+ "name" : "Remove submission" ,
260
+ "value" : `[x](https://d3d12infodb.boolka.dev/remove_submission?ID=${ info . lastInsertRowid } &password=${ process . env . SubmissionRemovalPassword } )` ,
261
+ "inline" : true
262
+ } )
263
+ }
264
+
265
+ notification_handler . notify ( notification )
251
266
252
267
return
253
268
}
@@ -261,6 +276,57 @@ api.post('/post_submission', (req, res) => {
261
276
}
262
277
} )
263
278
279
+ if ( process . env . SubmissionRemovalPassword ) {
280
+ const removeSubmissionStatement = db . prepare ( "DELETE FROM Submissions WHERE ID = ?" )
281
+ api . get ( '/remove_submission' , ( req , res ) => {
282
+ const password = req . query . password
283
+ const ID = Number ( req . query . ID )
284
+
285
+ if ( password !== process . env . SubmissionRemovalPassword ) {
286
+ res . status ( 401 )
287
+ res . send ( 'Unauthorized' )
288
+ console . log ( 'Unauthorized removal attempt' )
289
+ return
290
+ }
291
+
292
+ if ( ! ID ) {
293
+ res . status ( 400 )
294
+ res . send ( 'ID is missing' )
295
+ console . log ( 'Failed removal attempt. ID is missing.' )
296
+ return
297
+ }
298
+
299
+ if ( ! Number . isInteger ( ID ) || ID < 1 || ID > latestReportID ) {
300
+ res . status ( 400 )
301
+ res . send ( 'ID is not a valid number' )
302
+ console . log ( 'Failed removal attempt. ID is not a valid number.' )
303
+ return
304
+ }
305
+
306
+ try {
307
+ const info = removeSubmissionStatement . run ( ID )
308
+ if ( info . changes === 0 ) {
309
+ res . status ( 404 )
310
+ res . send ( 'Submission not found' )
311
+ console . log ( `Failed removal attempt. Submission not found ID ${ ID } .` )
312
+ return
313
+ }
314
+ res . send ( 'OK' )
315
+ databaseLastDeleteTime = GetCurrentTimeAsString ( )
316
+ databaseLastModificationTime = databaseLastDeleteTime
317
+ console . log ( `Removed submission ID ${ ID } ` )
318
+ }
319
+ catch ( e ) {
320
+ console . log ( 'DB Error' )
321
+ console . log ( e )
322
+
323
+ res . status ( 500 )
324
+ res . send ( 'DB Error' )
325
+ return
326
+ }
327
+ } )
328
+ }
329
+
264
330
if ( process . env . SSLKey && process . env . SSLCert ) {
265
331
console . log ( 'Starting HTTPS Server' )
266
332
const options = {
0 commit comments