Skip to content

Commit

Permalink
Merge pull request #108 from FlowFuse/add-file-api
Browse files Browse the repository at this point in the history
First pass files api for docker
  • Loading branch information
hardillb authored Aug 28, 2024
2 parents 6509756 + 0aa9987 commit 1e8464c
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
62 changes: 62 additions & 0 deletions docker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const got = require('got')
const FormData = require('form-data')
const Docker = require('dockerode')
const path = require('path')
const { chownSync, mkdirSync, rmSync } = require('fs')
Expand Down Expand Up @@ -170,6 +171,10 @@ const createContainer = async (project, domain) => {
})
}

const getStaticFileUrl = async (instance, filePath) => {
return `http://${instance.id}:2880/flowforge/files/_/${encodeURIComponent(filePath)}`
}

/**
* Docker Container driver
*
Expand Down Expand Up @@ -547,5 +552,62 @@ module.exports = {
}

return properties
},

// Static Assets API
listFiles: async (instance, filePath) => {
const fileUrl = await getStaticFileUrl(instance, filePath)
try {
return got.get(fileUrl).json()
} catch (err) {
err.statusCode = err.response.statusCode
throw err
}
},

updateFile: async (instance, filePath, update) => {
const fileUrl = await getStaticFileUrl(instance, filePath)
try {
return got.put(fileUrl, {
json: update
})
} catch (err) {
err.statusCode = err.response.statusCode
throw err
}
},

deleteFile: async (instance, filePath) => {
const fileUrl = await getStaticFileUrl(instance, filePath)
try {
return got.delete(fileUrl)
} catch (err) {
err.statusCode = err.response.statusCode
throw err
}
},
createDirectory: async (instance, filePath, directoryName) => {
const fileUrl = await getStaticFileUrl(instance, filePath)
try {
return got.post(fileUrl, {
json: { path: directoryName }
})
} catch (err) {
err.statusCode = err.response.statusCode
throw err
}
},
uploadFile: async (instance, filePath, fileBuffer) => {
const form = new FormData()
form.append('file', fileBuffer, { filename: filePath })
const fileUrl = await getStaticFileUrl(instance, filePath)
try {
return got.post(fileUrl, {
body: form
})
} catch (err) {
err.statusCode = err.response.statusCode
throw err
}
}
}
98 changes: 98 additions & 0 deletions 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 @@ -22,6 +22,7 @@
"license": "Apache-2.0",
"dependencies": {
"dockerode": "^3.3.1",
"form-data": "^4.0.0",
"got": "^11.8.0"
},
"devDependencies": {
Expand Down

0 comments on commit 1e8464c

Please sign in to comment.