mirror of
				https://gitlab.com/MrFry/mrfrys-node-server
				synced 2025-04-01 20:24:18 +02:00 
			
		
		
		
	Apis for listing user files, log rotate fix
This commit is contained in:
		@@ -1,7 +1,11 @@
 | 
			
		||||
import fs from 'fs'
 | 
			
		||||
 | 
			
		||||
import logger from '../../../utils/logger'
 | 
			
		||||
import utils from '../../../utils/utils'
 | 
			
		||||
import { Request, SubmoduleData } from '../../../types/basicTypes'
 | 
			
		||||
 | 
			
		||||
const usersFileName = '.users.json'
 | 
			
		||||
 | 
			
		||||
function setup(data: SubmoduleData): void {
 | 
			
		||||
  const { app, /* userDB, url, */ publicdirs /* moduleSpecificData */ } = data
 | 
			
		||||
 | 
			
		||||
@@ -9,16 +13,64 @@ function setup(data: SubmoduleData): void {
 | 
			
		||||
 | 
			
		||||
  const userFilesDir = publicDir + 'userFiles'
 | 
			
		||||
 | 
			
		||||
  app.get('/listUserFiles', (req: Request, res) => {
 | 
			
		||||
  app.get('/listUserDir', (req: Request, res) => {
 | 
			
		||||
    logger.LogReq(req)
 | 
			
		||||
 | 
			
		||||
    if (!utils.FileExists(userFilesDir)) {
 | 
			
		||||
      utils.CreatePath(userFilesDir, true)
 | 
			
		||||
    }
 | 
			
		||||
    const subdir: any = req.query.subdir
 | 
			
		||||
 | 
			
		||||
    res.json({
 | 
			
		||||
      files: utils.ReadDir(userFilesDir),
 | 
			
		||||
    })
 | 
			
		||||
    if (subdir) {
 | 
			
		||||
      const safeSubdir = subdir.replace(/\./g, '').replace(/\/+/g, '')
 | 
			
		||||
      const dir = userFilesDir + '/' + safeSubdir
 | 
			
		||||
      const usersFile = dir + '/' + usersFileName
 | 
			
		||||
 | 
			
		||||
      if (!utils.FileExists(usersFile)) {
 | 
			
		||||
        utils.WriteFile('{}', usersFile)
 | 
			
		||||
      }
 | 
			
		||||
      const users = utils.ReadJSON(usersFile)
 | 
			
		||||
 | 
			
		||||
      if (!utils.FileExists(dir)) {
 | 
			
		||||
        res.json({
 | 
			
		||||
          success: false,
 | 
			
		||||
          msg: `Path '${safeSubdir}' does not exists`,
 | 
			
		||||
        })
 | 
			
		||||
        return
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      res.json({
 | 
			
		||||
        files: utils.ReadDir(dir).reduce((acc, file) => {
 | 
			
		||||
          const stat = fs.lstatSync(dir + '/' + file)
 | 
			
		||||
 | 
			
		||||
          acc.push({
 | 
			
		||||
            name: file,
 | 
			
		||||
            path: dir.replace(publicDir, '') + '/' + file,
 | 
			
		||||
            size: stat.size,
 | 
			
		||||
            uploadDate: stat.mtime.getTime(),
 | 
			
		||||
            user: users[file],
 | 
			
		||||
          })
 | 
			
		||||
          return acc
 | 
			
		||||
        }, []),
 | 
			
		||||
      })
 | 
			
		||||
    } else {
 | 
			
		||||
      if (!utils.FileExists(userFilesDir)) {
 | 
			
		||||
        utils.CreatePath(userFilesDir, true)
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      res.json({
 | 
			
		||||
        dirs: utils.ReadDir(userFilesDir).reduce((acc, file) => {
 | 
			
		||||
          const stat = fs.lstatSync(userFilesDir + '/' + file)
 | 
			
		||||
 | 
			
		||||
          if (!stat.isDirectory()) {
 | 
			
		||||
            return acc
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          acc.push({
 | 
			
		||||
            name: file,
 | 
			
		||||
            date: stat.mtime.getTime(),
 | 
			
		||||
          })
 | 
			
		||||
          return acc
 | 
			
		||||
        }, []),
 | 
			
		||||
      })
 | 
			
		||||
    }
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -281,8 +281,8 @@ function rotateLog() {
 | 
			
		||||
    utils.CopyFile(vlogFile, logger.vlogDir + fname)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  utils.WriteFile(fname, logger.logDir + fname)
 | 
			
		||||
  utils.WriteFile(fname, logger.vlogDir + fname)
 | 
			
		||||
  utils.WriteFile(fname, logFile)
 | 
			
		||||
  utils.WriteFile(fname, vlogFile)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function LogTimerAction() {
 | 
			
		||||
 
 | 
			
		||||
@@ -70,8 +70,14 @@ function CopyFile(from: string, to: string): void {
 | 
			
		||||
  fs.copyFileSync(from, to)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function ReadDir(path: string): Array<string> {
 | 
			
		||||
  return fs.readdirSync(path)
 | 
			
		||||
function ReadDir(path: string, listHidden?: boolean): Array<string> {
 | 
			
		||||
  if (listHidden) {
 | 
			
		||||
    return fs.readdirSync(path)
 | 
			
		||||
  } else {
 | 
			
		||||
    return fs.readdirSync(path).filter((file) => {
 | 
			
		||||
      return !file.startsWith('.')
 | 
			
		||||
    })
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function ReadJSON(name: string): any {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user