@@ -4,15 +4,15 @@ const config = require("../config/config.json"), crypto = require("crypto"), emo
4
4
const MODULE_FUNCTION = "handle_requests" ,
5
5
6
6
// Base path for module folder creation and navigation
7
- BASE_PATH = "/EUS" ;
7
+ BASE_PATH = "/EUS" ,
8
+ API_CACHE_LIFESPAN = 3600000 ;
8
9
9
10
let node_modules = { } ;
10
11
11
12
let eusConfig = { } ,
12
13
useUploadKey = true ,
13
14
cacheJSON = "" ,
14
15
startupFinished = false ,
15
- diskRunningOnSize = 0 ,
16
16
timeSinceLastCache = Date . now ( ) ;
17
17
18
18
class Database {
@@ -100,16 +100,11 @@ function init() {
100
100
node_modules [ "chalk" ] = require ( "chalk" ) ;
101
101
node_modules [ "busboy" ] = require ( "connect-busboy" ) ;
102
102
node_modules [ "randomstring" ] = require ( "randomstring" ) ;
103
- node_modules [ "diskUsage" ] = require ( "diskusage" ) ;
104
103
node_modules [ "streamMeter" ] = require ( "stream-meter" ) ;
105
104
node_modules [ "mysql2" ] = require ( "mysql2" ) ;
106
105
107
106
// Only ran on startup so using sync functions is fine
108
107
109
- // Fetch total size of disk on startup, this will never change during runtime
110
- // if it does something seriously wrong has happened.
111
- diskRunningOnSize = node_modules . diskUsage . checkSync ( __dirname ) . total ;
112
-
113
108
// Makes the folder for files of the module
114
109
if ( ! fs . existsSync ( __dirname + BASE_PATH ) ) {
115
110
fs . mkdirSync ( __dirname + BASE_PATH ) ;
@@ -126,6 +121,11 @@ function init() {
126
121
console . log ( `[EUS] Made EUS images folder` ) ;
127
122
}
128
123
124
+ if ( ! fs . existsSync ( "/tmp/EUS_UPLOADS" ) ) {
125
+ fs . mkdirSync ( "/tmp/EUS_UPLOADS" ) ;
126
+ console . log ( "[EUS] Made EUS temp upload folder" ) ;
127
+ }
128
+
129
129
// Makes the config file
130
130
if ( ! fs . existsSync ( __dirname + BASE_PATH + "/config.json" ) ) {
131
131
// Config doesn't exist, make it.
@@ -151,6 +151,7 @@ function init() {
151
151
// Cache for the file count and space usage, this takes a while to do so it's best to cache the result
152
152
let cacheIsReady = false ;
153
153
function cacheFilesAndSpace ( ) {
154
+ timeSinceLastCache = Date . now ( ) ;
154
155
return new Promise ( async ( resolve , reject ) => {
155
156
const startCacheTime = Date . now ( ) ;
156
157
cacheIsReady = false ;
@@ -166,20 +167,14 @@ function cacheFilesAndSpace() {
166
167
} ) ;
167
168
cachedFilesAndSpace [ "filesTotal" ] = totalFiles ;
168
169
169
- cachedFilesAndSpace [ "space " ] = { usage : { } , total : { } } ;
170
+ cachedFilesAndSpace [ "size " ] = { } ;
170
171
171
- const dbSize = ( await dbConnection . query ( `SELECT SUM(imageSize ) FROM images LIMIT 1` ) ) [ "SUM(imageSize )" ] ;
172
+ const dbSize = ( await dbConnection . query ( `SELECT SUM(fileSize ) FROM images LIMIT 1` ) ) [ "SUM(fileSize )" ] ;
172
173
const totalSizeBytes = dbSize == null ? 0 : dbSize ;
173
174
const mbSize = totalSizeBytes / 1024 / 1024 ;
174
- cachedFilesAndSpace [ "space" ] [ "usage" ] [ "mb" ] = parseFloat ( mbSize . toFixed ( 4 ) ) ;
175
- cachedFilesAndSpace [ "space" ] [ "usage" ] [ "gb" ] = parseFloat ( ( mbSize / 1024 ) . toFixed ( 4 ) ) ;
176
- cachedFilesAndSpace [ "space" ] [ "usage" ] [ "string" ] = await spaceToLowest ( totalSizeBytes , true ) ;
177
-
178
- //totalDiskSize
179
- const totalMBSize = diskRunningOnSize / 1024 / 1024 ;
180
- cachedFilesAndSpace [ "space" ] [ "total" ] [ "mb" ] = parseFloat ( totalMBSize . toFixed ( 4 ) ) ;
181
- cachedFilesAndSpace [ "space" ] [ "total" ] [ "gb" ] = parseFloat ( ( totalMBSize / 1024 ) . toFixed ( 4 ) ) ;
182
- cachedFilesAndSpace [ "space" ] [ "total" ] [ "string" ] = await spaceToLowest ( diskRunningOnSize , true ) ;
175
+ cachedFilesAndSpace [ "size" ] [ "mb" ] = parseFloat ( mbSize . toFixed ( 4 ) ) ;
176
+ cachedFilesAndSpace [ "size" ] [ "gb" ] = parseFloat ( ( mbSize / 1024 ) . toFixed ( 4 ) ) ;
177
+ cachedFilesAndSpace [ "size" ] [ "string" ] = await spaceToLowest ( totalSizeBytes , true ) ;
183
178
184
179
resolve ( cachedFilesAndSpace ) ;
185
180
global . modules . consoleHelper . printInfo ( emoji . folder , `Stats api cache took ${ Date . now ( ) - startCacheTime } ms` ) ;
@@ -250,7 +245,7 @@ function validateConfig(json) {
250
245
251
246
// Check if server needs to be shutdown
252
247
if ( performShutdownAfterValidation ) {
253
- console . error ( "EUS config properties are missing, refer to docs for more details (https://wiki.eusv.ml )" ) ;
248
+ console . error ( "EUS config properties are missing, refer to example config on GitHub (https://github.com/tgpholly/EUS )" ) ;
254
249
process . exit ( 1 ) ;
255
250
}
256
251
else return true ;
@@ -277,7 +272,7 @@ function regularFile(req, res, urs = "", startTime = 0) {
277
272
global . modules . consoleHelper . printInfo ( emoji . cross , `${ req . method } : ${ node_modules . chalk . red ( "[404]" ) } ${ req . url } ${ Date . now ( ) - startTime } ms` ) ;
278
273
} else {
279
274
// File does exist, send it back to the client.
280
- res . sendFile ( __dirname + BASE_PATH + " /files" + req . url ) ;
275
+ res . sendFile ( ` ${ __dirname } ${ BASE_PATH } /files${ req . url } ` ) ;
281
276
global . modules . consoleHelper . printInfo ( emoji . heavy_check , `${ req . method } : ${ node_modules . chalk . green ( "[200]" ) } ${ req . url } ${ Date . now ( ) - startTime } ms` ) ;
282
277
}
283
278
} ) ;
@@ -298,8 +293,6 @@ module.exports = {
298
293
// Setup express to use busboy
299
294
global . app . use ( node_modules . busboy ( ) ) ;
300
295
startupFinished = true ;
301
- //cacheJSON = JSON.stringify(await cacheFilesAndSpace());
302
- //cacheIsReady = true;
303
296
} ,
304
297
get :async function ( req , res ) {
305
298
/*
@@ -316,7 +309,7 @@ module.exports = {
316
309
res . set ( "X-Frame-Options" , "SAMEORIGIN" ) ;
317
310
res . set ( "X-Content-Type-Options" , "nosniff" ) ;
318
311
319
- req . url = cleanURL ( req . url ) ;
312
+ req . url = decodeURIComponent ( req . url . split ( "?" ) [ 0 ] ) ;
320
313
321
314
// The Funny Response
322
315
if ( req . url . includes ( ".php" ) || req . url . includes ( "/wp-" ) ) {
@@ -337,16 +330,20 @@ module.exports = {
337
330
338
331
if ( dbAble ) {
339
332
if ( urs in existanceCache ) {
340
- imageFile ( req , res , `${ urs } .${ existanceCache [ urs ] } ` , startTime ) ;
333
+ const cachedFile = existanceCache [ urs ] ;
334
+ imageFile ( req , res , `${ cachedFile . fileHash } .${ cachedFile . fileType } ` , startTime ) ;
341
335
} else {
342
336
if ( dbConnection . dbActive ) {
343
337
// Try to get what we think is an image's details from the DB
344
- const dbEntry = await dbConnection . query ( `SELECT imageType FROM images WHERE imageId = ? LIMIT 1` , [ urs ] ) ;
338
+ const dbEntry = await dbConnection . query ( `SELECT hash, imageType FROM images WHERE imageId = ? LIMIT 1` , [ urs ] ) ;
345
339
346
340
// There's an entry in the DB for this, send the file back.
347
341
if ( dbEntry != null ) {
348
- existanceCache [ urs ] = dbEntry . imageType ;
349
- imageFile ( req , res , `${ urs } .${ dbEntry . imageType } ` , startTime ) ;
342
+ existanceCache [ urs ] = {
343
+ fileHash : dbEntry . hash ,
344
+ fileType : dbEntry . imageType
345
+ } ;
346
+ imageFile ( req , res , `${ dbEntry . hash } .${ dbEntry . imageType } ` , startTime ) ;
350
347
}
351
348
// There's no entry, so treat this as a regular file.
352
349
else regularFile ( req , res , urs , startTime ) ;
@@ -396,7 +393,7 @@ module.exports = {
396
393
return ;
397
394
}
398
395
// Create a write stream for the file
399
- let fstream = fs . createWriteStream ( __dirname + BASE_PATH + "/i/ " + fileOutName + "." + thefe ) ;
396
+ let fstream = fs . createWriteStream ( "/tmp/EUS_UPLOADS/ " + fileOutName ) ;
400
397
file . pipe ( fstream ) ;
401
398
402
399
// Get all file data for the file MD5
@@ -416,19 +413,41 @@ module.exports = {
416
413
hash . write ( md5Buffer ) ;
417
414
hash . end ( ) ;
418
415
419
- // Add to the existance cache
420
- existanceCache [ fileOutName ] = thefe [ 0 ] ;
421
-
422
- // Store image data in db
423
- await dbConnection . query ( `INSERT INTO images (id, imageId, imageType, hash, imageSize) VALUES (NULL, ?, ?, ?, ?)` , [ fileOutName , thefe [ 0 ] , hash . read ( ) , md5Buffer . length ] ) ;
424
-
425
- // Send URL of the uploaded image to the client
426
- res . end ( eusConfig . baseURL + fileOutName ) ;
427
- global . modules . consoleHelper . printInfo ( emoji . heavy_check , `${ req . method } : Upload of ${ fileOutName } finished. Took ${ Date . now ( ) - startTime } ms` ) ;
428
-
429
- // Update cached files & space
430
- cacheJSON = JSON . stringify ( await cacheFilesAndSpace ( ) ) ;
431
- cacheIsReady = true ;
416
+ const fileHash = hash . read ( ) ;
417
+ const dataOnHash = await dbConnection . query ( "SELECT imageId FROM images WHERE hash = ? LIMIT 1" , [ fileHash ] ) ;
418
+ if ( dataOnHash !== undefined )
419
+ {
420
+ fs . unlink ( `/tmp/EUS_UPLOADS/${ fileOutName } ` , ( ) => { } ) ;
421
+ res . end ( `${ eusConfig . baseURL } ${ dataOnHash . imageId } ` ) ;
422
+ global . modules . consoleHelper . printInfo ( emoji . heavy_check , `${ req . method } : Hash matched! Sending ${ dataOnHash . imageId } instead. Took ${ Date . now ( ) - startTime } ms` ) ;
423
+ return ;
424
+ } else {
425
+ fs . rename ( `/tmp/EUS_UPLOADS/${ fileOutName } ` , `${ __dirname } ${ BASE_PATH } /i/${ fileHash } .${ thefe [ 0 ] } ` , async ( ) => {
426
+ // Add to the existance cache
427
+ existanceCache [ fileOutName ] = {
428
+ fileHash : fileHash ,
429
+ fileType : thefe [ 0 ]
430
+ } ;
431
+
432
+ // Store image data in db
433
+ await dbConnection . query ( `INSERT INTO images (id, imageId, imageType, hash, fileSize) VALUES (NULL, ?, ?, ?, ?)` , [ fileOutName , thefe [ 0 ] , fileHash , md5Buffer . length ] ) ;
434
+
435
+ // Send URL of the uploaded image to the client
436
+ res . end ( eusConfig . baseURL + fileOutName ) ;
437
+ global . modules . consoleHelper . printInfo ( emoji . heavy_check , `${ req . method } : Upload of ${ fileOutName } finished. Took ${ Date . now ( ) - startTime } ms` ) ;
438
+
439
+ // Update cached files & space
440
+ if ( ( Date . now ( ) - timeSinceLastCache ) >= API_CACHE_LIFESPAN ) {
441
+ cacheJSON = JSON . stringify ( await cacheFilesAndSpace ( ) ) ;
442
+ } else {
443
+ const tempJson = JSON . parse ( cacheJSON ) ;
444
+ tempJson . fileCounts [ thefe [ 0 ] ] ++ ;
445
+ cacheJSON = JSON . stringify ( tempJson ) ;
446
+ global . modules . consoleHelper . printInfo ( emoji . folder , `Skiped api cache` ) ;
447
+ }
448
+ cacheIsReady = true ;
449
+ } ) ;
450
+ }
432
451
} ) ;
433
452
434
453
/*fstream.on('close', async () => {
@@ -529,5 +548,5 @@ module.exports.MOD_FUNC = MODULE_FUNCTION;
529
548
530
549
module . exports . REQUIRED_NODE_MODULES = [
531
550
"chalk" , "connect-busboy" , "randomstring" ,
532
- "diskusage" , " stream-meter", "mysql2"
551
+ "stream-meter" , "mysql2"
533
552
] ;
0 commit comments