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

fix(server): file persistence for local upload provider #610

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
- PORT=80
volumes:
- ./conf.d:/app/conf.d
- ./data/rctf-uploads:/app/uploads
depends_on:
- redis
- postgres
Expand Down
2 changes: 1 addition & 1 deletion install/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ do_install() {

RCTF_GIT_REF="${RCTF_GIT_REF:-"master"}"

mkdir -p conf.d data/rctf-postgres data/rctf-redis
mkdir -p conf.d data/rctf-postgres data/rctf-redis data/rctf-uploads

printf "%s\n" \
"RCTF_DATABASE_PASSWORD=$(get_key)" \
Expand Down
14 changes: 13 additions & 1 deletion server/providers/uploads/local/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ export default class LocalProvider implements Provider {

this.uploadMap = new Map<string, Upload>()

const uploads = fs.readdirSync(this.uploadDirectory)

for (const hash of uploads) {
for (const name of fs.readdirSync(path.join(this.uploadDirectory, hash))) {
this.uploadMap.set(this.getKey(hash, name), {
filePath: path.join(this.uploadDirectory, hash, name),
name
})
}
}

void app.register(async (fastify) => {
void fastify.register(fastifyStatic, {
root: this.uploadDirectory,
Expand Down Expand Up @@ -99,13 +110,14 @@ export default class LocalProvider implements Provider {

const key = this.getKey(hash, name)
const urlPath = this.getUrlPath(key)
const filePath = path.join(this.uploadDirectory, hash)
const filePath = path.join(this.uploadDirectory, hash, name)

this.uploadMap.set(key, {
filePath,
name
})

await fs.promises.mkdir(path.join(this.uploadDirectory, hash))
await fs.promises.writeFile(filePath, data)

return (config.origin || '') + urlPath
Expand Down