mirror of
				https://gitlab.com/MrFry/mrfrys-node-server
				synced 2025-04-01 20:24:18 +02:00 
			
		
		
		
	Prettied all js in src/
This commit is contained in:
		| @@ -32,7 +32,7 @@ const logger = require('../../utils/logger.js') | ||||
| let publicdirs = [] | ||||
| let url = '' | ||||
|  | ||||
| function GetApp () { | ||||
| function GetApp() { | ||||
|   const p = publicdirs[0] | ||||
|   if (!p) { | ||||
|     throw new Error(`No public dir! ( Stuff )`) | ||||
| @@ -42,33 +42,36 @@ function GetApp () { | ||||
|   const listedFiles = './' + p + 'files' | ||||
|  | ||||
|   app.set('view engine', 'ejs') | ||||
|   app.set('views', [ | ||||
|     './modules/stuff/views', | ||||
|     './sharedViews' | ||||
|   ]) | ||||
|   app.set('views', ['./modules/stuff/views', './sharedViews']) | ||||
|   publicdirs.forEach((pdir) => { | ||||
|     logger.Log(`Using public dir: ${pdir}`) | ||||
|     app.use(express.static(pdir)) | ||||
|   }) | ||||
|   app.use(busboy({ | ||||
|     limits: { | ||||
|       fileSize: 10000 * 1024 * 1024 | ||||
|     } | ||||
|   })) | ||||
|   app.use( | ||||
|     busboy({ | ||||
|       limits: { | ||||
|         fileSize: 10000 * 1024 * 1024, | ||||
|       }, | ||||
|     }) | ||||
|   ) | ||||
|   app.use(bodyParser.json()) | ||||
|   app.use(bodyParser.urlencoded({ | ||||
|     limit: '5mb', | ||||
|     extended: true | ||||
|   })) | ||||
|   app.use(bodyParser.json({ | ||||
|     limit: '5mb' | ||||
|   })) | ||||
|   app.use( | ||||
|     bodyParser.urlencoded({ | ||||
|       limit: '5mb', | ||||
|       extended: true, | ||||
|     }) | ||||
|   ) | ||||
|   app.use( | ||||
|     bodyParser.json({ | ||||
|       limit: '5mb', | ||||
|     }) | ||||
|   ) | ||||
|  | ||||
|   // -------------------------------------------------------------- | ||||
|  | ||||
|   // app, '/*.mp4', 'video/mp4', 'stuff/video' | ||||
|   function appGetFileType (app, wildcard, contentType, pageToRender) { | ||||
|     app.get(wildcard, function (req, res) { | ||||
|   function appGetFileType(app, wildcard, contentType, pageToRender) { | ||||
|     app.get(wildcard, function(req, res) { | ||||
|       let p = decodeURI(req.url) | ||||
|       let fp = p | ||||
|       if (p.includes('?')) { | ||||
| @@ -80,7 +83,7 @@ function GetApp () { | ||||
|       if (!fs.existsSync(fpath)) { | ||||
|         res.render('nofile', { | ||||
|           missingFile: fpath, | ||||
|           url | ||||
|           url, | ||||
|         }) | ||||
|         return | ||||
|       } | ||||
| @@ -91,23 +94,21 @@ function GetApp () { | ||||
|         if (range) { | ||||
|           const parts = range.replace(/bytes=/, '').split('-') | ||||
|           const start = parseInt(parts[0], 10) | ||||
|           const end = parts[1] | ||||
|             ? parseInt(parts[1], 10) | ||||
|             : fileSize - 1 | ||||
|           const chunksize = (end - start) + 1 | ||||
|           const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1 | ||||
|           const chunksize = end - start + 1 | ||||
|           const file = fs.createReadStream(fpath, { start, end }) | ||||
|           const head = { | ||||
|             'Content-Range': `bytes ${start}-${end}/${fileSize}`, | ||||
|             'Accept-Ranges': 'bytes', | ||||
|             'Content-Length': chunksize, | ||||
|             'Content-Type': contentType | ||||
|             'Content-Type': contentType, | ||||
|           } | ||||
|           res.writeHead(206, head) | ||||
|           file.pipe(res) | ||||
|         } else { | ||||
|           const head = { | ||||
|             'Content-Length': fileSize, | ||||
|             'Content-Type': contentType | ||||
|             'Content-Type': contentType, | ||||
|           } | ||||
|           res.writeHead(200, head) | ||||
|           fs.createReadStream(fpath).pipe(res) | ||||
| @@ -121,7 +122,7 @@ function GetApp () { | ||||
|           fname, | ||||
|           url, | ||||
|           contentType, | ||||
|           albumArt: GetAlbumArt(p) | ||||
|           albumArt: GetAlbumArt(p), | ||||
|         }) | ||||
|       } | ||||
|     }) | ||||
| @@ -132,10 +133,10 @@ function GetApp () { | ||||
|     ['/*.mkv', 'audio/x-matroska', 'video'], | ||||
|     ['/*.mp3', 'audio/mpeg', 'audio'], | ||||
|     ['/*.pdf', 'application/pdf'], | ||||
|     ['/*.zip', 'application/zip'] | ||||
|     ['/*.zip', 'application/zip'], | ||||
|   ] | ||||
|  | ||||
|   function GetAlbumArt (path) { | ||||
|   function GetAlbumArt(path) { | ||||
|     let tmp = path.split('.') | ||||
|     tmp.pop() | ||||
|     tmp = tmp.join('.').split('/') | ||||
| @@ -148,16 +149,23 @@ function GetApp () { | ||||
|     appGetFileType(app, t[0], t[1], t[2]) | ||||
|   }) | ||||
|  | ||||
|   app.get('/*', function (req, res) { | ||||
|   app.get('/*', function(req, res) { | ||||
|     let parsedUrl = decodeURI(req.url) | ||||
|     let curr = listedFiles + '/' + parsedUrl.substring('/'.length, parsedUrl.length).split('?')[0] | ||||
|     let curr = | ||||
|       listedFiles + | ||||
|       '/' + | ||||
|       parsedUrl.substring('/'.length, parsedUrl.length).split('?')[0] | ||||
|     let relPath = curr.substring(listedFiles.length, curr.length) | ||||
|  | ||||
|     if (relPath[relPath.length - 1] !== '/') { relPath += '/' } | ||||
|     if (relPath[relPath.length - 1] !== '/') { | ||||
|       relPath += '/' | ||||
|     } | ||||
|  | ||||
|     let t = relPath.split('/') | ||||
|     let prevDir = '' | ||||
|     for (let i = 0; i < t.length - 2; i++) { prevDir += t[i] + '/' } | ||||
|     for (let i = 0; i < t.length - 2; i++) { | ||||
|       prevDir += t[i] + '/' | ||||
|     } | ||||
|  | ||||
|     // curr = curr.replace(/\//g, "/"); | ||||
|     // relPath = relPath.replace(/\//g, "/"); | ||||
| @@ -167,14 +175,18 @@ function GetApp () { | ||||
|     try { | ||||
|       const stat = fs.lstatSync(curr) | ||||
|       if (stat.isDirectory() || stat.isSymbolicLink()) { | ||||
|         if (curr[curr.length - 1] !== '/') { curr += '/' } | ||||
|         if (curr[curr.length - 1] !== '/') { | ||||
|           curr += '/' | ||||
|         } | ||||
|  | ||||
|         let f = [] | ||||
|  | ||||
|         let files = fs.readdirSync(curr) | ||||
|         files.sort(function (a, b) { | ||||
|           return fs.statSync(curr + b).mtime.getTime() - | ||||
|                       fs.statSync(curr + a).mtime.getTime() | ||||
|         files.sort(function(a, b) { | ||||
|           return ( | ||||
|             fs.statSync(curr + b).mtime.getTime() - | ||||
|             fs.statSync(curr + a).mtime.getTime() | ||||
|           ) | ||||
|         }) | ||||
|  | ||||
|         files.forEach((item) => { | ||||
| @@ -186,7 +198,9 @@ function GetApp () { | ||||
|             res.size = Math.round(fileSizeInBytes / 1000000) | ||||
|  | ||||
|             res.path = relPath | ||||
|             if (res.path[res.path.length - 1] !== '/') { res.path += '/' } | ||||
|             if (res.path[res.path.length - 1] !== '/') { | ||||
|               res.path += '/' | ||||
|             } | ||||
|             res.path += item | ||||
|  | ||||
|             res.mtime = stat['mtime'].toLocaleString() | ||||
| @@ -200,7 +214,7 @@ function GetApp () { | ||||
|           folders: f, | ||||
|           dirname: relPath, | ||||
|           prevDir, | ||||
|           url | ||||
|           url, | ||||
|         }) | ||||
|       } else { | ||||
|         let fileStream = fs.createReadStream(curr) | ||||
| @@ -209,23 +223,23 @@ function GetApp () { | ||||
|     } catch (e) { | ||||
|       res.render('nofile', { | ||||
|         missingFile: curr, | ||||
|         url | ||||
|         url, | ||||
|       }) | ||||
|     } | ||||
|   }) | ||||
|  | ||||
|   // ----------------------------------------------------------------------------------------------- | ||||
|  | ||||
|   app.get('*', function (req, res) { | ||||
|   app.get('*', function(req, res) { | ||||
|     res.status(404).render('404') | ||||
|   }) | ||||
|  | ||||
|   app.post('*', function (req, res) { | ||||
|   app.post('*', function(req, res) { | ||||
|     res.status(404).render('404') | ||||
|   }) | ||||
|  | ||||
|   return { | ||||
|     app: app | ||||
|     app: app, | ||||
|   } | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user