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

First pass at file api #275

Merged
merged 23 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ if (NODE_MAJOR_VERSION > 14) {

const { Launcher } = require('./lib/launcher')
const { AdminInterface } = require('./lib/admin')
const { filesInterface } = require('./lib/files')

const cmdLineOptions = [
{ name: 'port', alias: 'p', type: Number },
Expand Down Expand Up @@ -111,6 +112,7 @@ async function main () {
await launcher.logAuditEvent('start-failed', { error })
}

filesInterface(adminInterface.app, launcher.settings)
// const wss = new ws.Server({ clientTracking: false, noServer: true })
//
// server.on('upgrade', (req, socket, head) => {
Expand Down
1 change: 1 addition & 0 deletions lib/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class AdminInterface {
this.launcher = launcher

const app = express()
this.app = app
this.server = http.createServer(app)

app.use(bodyParser.json({}))
Expand Down
120 changes: 120 additions & 0 deletions lib/files/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
const { mkdir, readdir, rm, rename, stat, writeFile } = require('fs/promises')
const { existsSync } = require('fs')
const { dirname, join, normalize } = require('path')
const multer = require('multer')

const filesInterface = (app, settings) => {
const BASE_PATH = join(settings.rootDir, settings.userDir, 'storage')
// not sure about this yet, need to check how file storage works
const storage = multer.memoryStorage()
const upload = multer({storage: storage})

const regex = /^\/flowforge\/files\/_\/([\.a-zA-Z0-9\/\-_ ]*)$/
hardillb marked this conversation as resolved.
Show resolved Hide resolved

const listDirectory = async (path = '') => {
const fullPath = normalize(join(BASE_PATH, path))
if (fullPath.startsWith(BASE_PATH)) {
const files = await readdir(fullPath, {withFileTypes: true})
const response = {
meta: {
next_cursor: ''
},
hardillb marked this conversation as resolved.
Show resolved Hide resolved
files: [],
count: files.length
}
for (i in files) {
const file = files[i]
const fullPath = join(file.path, file.name)
const s = await stat(fullPath)
const rep = {
name: file.name,
lastModified: s.mtime
}
if (file.isFile()) {
rep.type = 'file'
rep.size = s.size

} else if (file.isDirectory()) {
rep.type = 'directory'
}
response.files.push(rep)
}
return response
} else {
return []
}
}

app.get(regex, async (request, reply) => {
try {
const files = await listDirectory(request.params[0])
reply.send(files)
} catch (err) {
if (err.code === 'ENOENT') {
reply.status(404).send()
}
}
Steve-Mcl marked this conversation as resolved.
Show resolved Hide resolved
})

app.put(regex, async (request, reply) => {
const fullPath = normalize(join(BASE_PATH, request.params[0]))
const newPath = normalize(join(BASE_PATH, request.body.path))
if (fullPath.startsWith(BASE_PATH) && newPath.startsWith(BASE_PATH)) {
if (existsSync(fullPath) && existsSync(dirname(newPath))) {
await rename(fullPath, newPath)
reply.status(202).send()
} else {
reply.status(404).send()
}
}
reply.status(403).send()
})

app.post(regex, upload.single('file'), async (request, reply) => {
const startPath = normalize(join(BASE_PATH, request.params[0]))
console.log(startPath)
if (startPath.startsWith(BASE_PATH)) {
console.log(request.get('content-type'))
if (request.get('content-type') === 'application/json') {
const newPath = request.body.path
const fullPath = normalize(join(startPath, newPath))
console.log(fullPath)
if (fullPath.startsWith(BASE_PATH)) {
await mkdir(fullPath, {recursive: true})
reply.status(201).send()
} else {
console.log('problem')
reply.status(500).send()
}
} else if (request.get('content-type').startsWith('multipart/form-data')) {
await writeFile(startPath, request.file.buffer)
reply.status(201).send()
} else {
reply.status(406).send()
}
} else {
reply.status(403).send()
}
})

app.delete(regex, async (request, reply) => {
const fullPath = normalize(join(BASE_PATH, request.params[0]))
if (fullPath !== BASE_PATH && fullPath.startsWith(BASE_PATH)) {
if (existsSync(fullPath)) {
await rm(fullPath, {
force: true,
recursive: true
})
reply.status(204).send()
} else {
reply.status(404).send()
}
} else {
reply.status(403).send()
}
})
}

module.exports = {
filesInterface
}
105 changes: 104 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"got": "^11.8.6",
"json-stringify-safe": "5.0.1",
"memorystore": "^1.6.7",
"multer": "^1.4.5-lts.1",
"oauth": "^0.9.15",
"parse-prometheus-text-format": "^1.1.1",
"passport": "0.6.0",
Expand Down