mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Lotsa typescript bullshit
This commit is contained in:
parent
b7ac485689
commit
b927988017
65 changed files with 801 additions and 8447 deletions
|
@ -19,27 +19,29 @@
|
|||
------------------------------------------------------------------------- */
|
||||
|
||||
// package requires
|
||||
const express = require('express')
|
||||
const bodyParser = require('body-parser')
|
||||
const busboy = require('connect-busboy')
|
||||
const fs = require('fs')
|
||||
import express from 'express'
|
||||
import bodyParser from 'body-parser'
|
||||
import busboy from 'connect-busboy'
|
||||
import fs from 'fs'
|
||||
const app = express()
|
||||
|
||||
// other requires
|
||||
const logger = require('../../utils/logger.js')
|
||||
import logger from '../../utils/logger'
|
||||
import { SetupData } from '../../server'
|
||||
import { ModuleType } from '../../types/basicTypes'
|
||||
|
||||
// stuff gotten from server.js
|
||||
let publicdirs = []
|
||||
let url = ''
|
||||
|
||||
function GetApp() {
|
||||
const p = publicdirs[0]
|
||||
if (!p) {
|
||||
function GetApp(): ModuleType {
|
||||
const publicDir = publicdirs[0]
|
||||
if (!publicDir) {
|
||||
throw new Error(`No public dir! ( Stuff )`)
|
||||
}
|
||||
|
||||
// files in public dirs
|
||||
const listedFiles = './' + p + 'files'
|
||||
const listedFiles = './' + publicDir + 'files'
|
||||
|
||||
app.set('view engine', 'ejs')
|
||||
app.set('views', ['./src/modules/stuff/views', './src/sharedViews'])
|
||||
|
@ -72,10 +74,10 @@ function GetApp() {
|
|||
// app, '/*.mp4', 'video/mp4', 'stuff/video'
|
||||
function appGetFileType(app, wildcard, contentType, pageToRender) {
|
||||
app.get(wildcard, function(req, res) {
|
||||
let p = decodeURI(req.url)
|
||||
let fp = p
|
||||
if (p.includes('?')) {
|
||||
fp = p.split('?')
|
||||
const path = decodeURI(req.url)
|
||||
let fp: any = path
|
||||
if (path.includes('?')) {
|
||||
fp = path.split('?')
|
||||
fp.pop()
|
||||
fp = fp.join('/')
|
||||
}
|
||||
|
@ -115,14 +117,13 @@ function GetApp() {
|
|||
}
|
||||
} else {
|
||||
logger.LogReq(req)
|
||||
let fname = fpath.split('/')
|
||||
fname = fname.pop()
|
||||
const fname = fpath.split('/').pop()
|
||||
res.render(pageToRender, {
|
||||
path: fp,
|
||||
fname,
|
||||
url,
|
||||
contentType,
|
||||
albumArt: GetAlbumArt(p),
|
||||
albumArt: GetAlbumArt(path),
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -140,7 +141,7 @@ function GetApp() {
|
|||
let tmp = path.split('.')
|
||||
tmp.pop()
|
||||
tmp = tmp.join('.').split('/')
|
||||
let last = tmp.pop()
|
||||
const last = tmp.pop()
|
||||
|
||||
return tmp.join('/') + '/.' + last + '.png'
|
||||
}
|
||||
|
@ -150,7 +151,7 @@ function GetApp() {
|
|||
})
|
||||
|
||||
app.get('/*', function(req, res) {
|
||||
let parsedUrl = decodeURI(req.url)
|
||||
const parsedUrl = decodeURI(req.url)
|
||||
let curr =
|
||||
listedFiles +
|
||||
'/' +
|
||||
|
@ -161,10 +162,10 @@ function GetApp() {
|
|||
relPath += '/'
|
||||
}
|
||||
|
||||
let t = relPath.split('/')
|
||||
const temp = relPath.split('/')
|
||||
let prevDir = ''
|
||||
for (let i = 0; i < t.length - 2; i++) {
|
||||
prevDir += t[i] + '/'
|
||||
for (let i = 0; i < temp.length - 2; i++) {
|
||||
prevDir += temp[i] + '/'
|
||||
}
|
||||
|
||||
// curr = curr.replace(/\//g, "/");
|
||||
|
@ -179,9 +180,9 @@ function GetApp() {
|
|||
curr += '/'
|
||||
}
|
||||
|
||||
let f = []
|
||||
const folders = []
|
||||
|
||||
let files = fs.readdirSync(curr)
|
||||
const files = fs.readdirSync(curr)
|
||||
files.sort(function(a, b) {
|
||||
return (
|
||||
fs.statSync(curr + b).mtime.getTime() -
|
||||
|
@ -191,10 +192,9 @@ function GetApp() {
|
|||
|
||||
files.forEach((item) => {
|
||||
if (item[0] !== '.') {
|
||||
let res = { name: item }
|
||||
let stat = fs.statSync(curr + '/' + item)
|
||||
|
||||
let fileSizeInBytes = stat['size']
|
||||
const res: any = { name: item }
|
||||
const stat = fs.statSync(curr + '/' + item)
|
||||
const fileSizeInBytes = stat['size']
|
||||
res.size = Math.round(fileSizeInBytes / 1000000)
|
||||
|
||||
res.path = relPath
|
||||
|
@ -206,21 +206,21 @@ function GetApp() {
|
|||
res.mtime = stat['mtime'].toLocaleString()
|
||||
res.isDir = stat.isDirectory()
|
||||
|
||||
f.push(res)
|
||||
folders.push(res)
|
||||
}
|
||||
})
|
||||
|
||||
res.render('folders', {
|
||||
folders: f,
|
||||
folders: folders,
|
||||
dirname: relPath,
|
||||
prevDir,
|
||||
url,
|
||||
})
|
||||
} else {
|
||||
let fileStream = fs.createReadStream(curr)
|
||||
const fileStream = fs.createReadStream(curr)
|
||||
fileStream.pipe(res)
|
||||
}
|
||||
} catch (e) {
|
||||
} catch (err) {
|
||||
res.render('nofile', {
|
||||
missingFile: curr,
|
||||
url,
|
||||
|
@ -243,9 +243,11 @@ function GetApp() {
|
|||
}
|
||||
}
|
||||
|
||||
exports.name = 'Stuff'
|
||||
exports.getApp = GetApp
|
||||
exports.setup = (data) => {
|
||||
url = data.url
|
||||
publicdirs = data.publicdirs
|
||||
export default {
|
||||
name: 'Stuff',
|
||||
getApp: GetApp,
|
||||
setup: (data: SetupData): void => {
|
||||
url = data.url
|
||||
publicdirs = data.publicdirs
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue