Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add File Manager #19

Merged
merged 6 commits into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@
**/*.njsproj
**/*.sln
**/*.sw?

# Development components
expressjs/src/storage
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ yarn-error.log*
*.sln

# Development components
wifi/
wifi/
expressjs/src/storage
2 changes: 1 addition & 1 deletion Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ FROM node:16.13.2-alpine3.14

ENV NODE_ENV=production

WORKDIR /usr/src/app
WORKDIR /app

# Copy ExpressJS node_modules in to container
COPY --from=ejs-node-build /build-context/node_modules ./node_modules
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A community-built starter user interface for using Balena devices.

<img width="1392" alt="Screenshot 2021-11-15 at 10 06 02" src="https://user-images.githubusercontent.com/64841595/141763422-a395ca10-c86b-44b9-9723-e5114fb8d563.png">
<img width="1288" alt="ui" src="https://user-images.githubusercontent.com/64841595/154854103-2e37d96a-81ab-4fc4-aa28-faefff4d5188.png">

# Description

Expand Down
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ services:
restart: always
ports:
- "80:80"
volumes:
- "storage:/app/storage"

# If using the Volume Manager to access volumes on other containers, ensure this container `depends_on` your
# other container using the below:
# depends_on:
# - "your-other-container-name"

labels:
io.balena.features.supervisor-api: 1
io.balena.features.balena-api: 1
Expand All @@ -33,3 +41,6 @@ services:
cap_add:
- NET_ADMIN
privileged: true # This can be removed if you do not need the LED indicator.

volumes:
storage:
8 changes: 7 additions & 1 deletion expressjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@
"build": "yarn tsc -p ."
},
"dependencies": {
"axios": "^0.25.0",
"axios": "^0.26.0",
"balena-sdk": "^16.13.4",
"cors": "^2.8.5",
"express": "^4.17.2",
"formidable": "^2.0.1",
"fs-extra": "^10.0.0",
"klaw-sync": "^6.0.0",
"lockfile": "^1.0.4",
"lodash": "^4.17.21",
"winston": "^3.6.0"
},
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/formidable": "^2.0.4",
"@types/fs-extra": "^9.0.13",
"@types/klaw-sync": "^6.0.1",
"@types/lockfile": "^1.0.2",
"@types/node": "^17.0.17",
"@typescript-eslint/eslint-plugin": "^5.11.0",
Expand Down
4 changes: 3 additions & 1 deletion expressjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import cors from 'cors'
import express from 'express'
import BalenaSDKRoutes from './routes/BalenaSDKRoutes'
import CustomRoutes from './routes/CustomRoutes'
import FilemanagerRoutes from './routes/FilemanagerRoutes'
import SupervisorRoutes from './routes/SupervisorRoutes'
import TestRoutes from './routes/TestRoutes'
import WifiRoutes from './routes/WifiConnectRoutes'
Expand All @@ -16,11 +17,12 @@ app.locals.defaultCacheTimeout = 0 // Default cache timeout used when none is pr
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(express.static('public'))
app.use(express.static('public', { dotfiles: 'allow' }))

// Routes
app.use(BalenaSDKRoutes)
app.use(CustomRoutes)
app.use(FilemanagerRoutes)
app.use(SupervisorRoutes)
app.use(WifiRoutes)

Expand Down
112 changes: 112 additions & 0 deletions expressjs/src/routes/FilemanagerRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import Logger from '../common/logger'
import express from 'express'
import formidable from 'formidable'
import fse from 'fs-extra'
import klawSync, { Item } from 'klaw-sync'
import path from 'path'

const router = express.Router()

// Root directory files
// `path.join` sterilizes paths to prevent manipulation of root dir
const rootDir = path.join(__dirname + '/../storage/')

// Prevent Directory Traversal
// https://nodejs.org/en/knowledge/file-system/security/introduction/#preventing-directory-traversal
function validatePath(path: string) {
if (path.indexOf(rootDir) !== 0) {
Logger.warn('User attempting to reach out of the root dir?')
throw new Error('User attempting to reach out of the root dir?')
}
return path
}

// Ignore hidden directories and files
const filterFn = (item: Item) => {
const basename = path.basename(item.path)
return basename === '.' || basename[0] !== '.'
}

// Fetch files
function fetchList(currentPath: Array<string>) {
const files = klawSync(
validatePath(path.join(rootDir, currentPath.join('/'))),
{
depthLimit: 0,
nodir: true,
filter: filterFn
}
)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
files.forEach(function (file: any) {
file.type = 'file'
})

const folders = klawSync(
validatePath(path.join(path.join(rootDir, currentPath.join('/')))),
{
depthLimit: 0,
nofile: true,
filter: filterFn
}
)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
folders.forEach(function (folder: any) {
folder.type = 'folder'
})

return folders.concat(files)
}

// Routes //

router.post('/filemanager/delete', function (req, res) {
fse.remove(validatePath(path.join(req.body.currentPath))).catch((err) => {
Logger.error(err)
})
res.json({ message: 'success' })
})

router.get('/filemanager/download', function (req, res) {
res.download(validatePath(path.join(req.query.currentPath as string)))
})

router.post('/filemanager/list', function (req, res) {
res.json(fetchList(req.body.currentPath))
})

router.post('/filemanager/newfolder', function (req, res) {
const newFolder = validatePath(
path.join(rootDir, req.body.currentPath.join('/'), req.body.newFolderName)
)

fse.ensureDir(newFolder).catch((err) => Logger.error(err))

res.json({ message: 'success' })
})

router.post('/filemanager/upload', function (req, res) {
const form = new formidable.IncomingForm({
maxFileSize: 5000 * 1024 * 1024
})

form.on('error', (err) => {
Logger.error(err)
})

form.on('fileBegin', function (_name, file) {
file.filepath = validatePath(
path.join(
rootDir,
req.headers.currentpath as string,
file.originalFilename || file.newFilename
)
)
})

form.parse(req, () => {
res.send('success')
})
})

export default router
Loading