Skip to content

[REFACTOR] getStoragePath() Helper — 30+ Files with inconsistent STORAGE_DIR fallback #106

@Delqhi

Description

@Delqhi

Problem

30+ files across server/ and collector/ use the pattern:

process.env.STORAGE_DIR
  ? path.resolve(process.env.STORAGE_DIR, `some/subdir`)
  : path.resolve(__dirname, `../../storage/some/subdir`)

The fallback logic is inconsistent — some files crash if STORAGE_DIR is undefined, others fall back to different default paths. This is a code smell and a deployment risk.

Scope

Files to migrate (server side)

  • server/endpoints/api/document/index.js (line 21)
  • server/endpoints/api/reports/index.js (line 32)
  • server/endpoints/utils.js (lines 148-149, 173)
  • server/utils/EmbeddingEngines/native/index.js (lines 38-39, 118-119)
  • server/utils/DocumentManager/index.js (line 6)
  • server/utils/agents/aibitat/plugins/create-files/lib.js (line 22)
  • server/utils/agents/aibitat/plugins/filesystem/lib.js (line 55)
  • server/utils/agents/imported.js (line 8)
  • server/utils/MCP/hypervisor/index.js (line 76)
  • server/utils/agentFlows/index.js (lines 19-20)
  • server/utils/vectorDbProviders/lance/index.js (lines 24-25)
  • server/utils/comKey/index.js (line 9)
  • server/utils/AiProviders/{fireworksAi,togetherAi,gemini,openRouter,dockerModelRunner,novita,apipie,modelMap,ppio,giteeai,cometapi}/index.js
  • server/utils/files/{index,pfp,logo,multer}.js
  • server/utils/PushNotifications/index.js (line 77)
  • server/utils/reports/index.js (line 17)
  • server/utils/EmbeddingRerankers/native/index.js (lines 21-22)
  • server/jobs/helpers/index.js (line 13)

Files to migrate (collector side)

  • collector/utils/OCRLoader/index.js (lines 27-28)
  • collector/utils/extensions/RepoLoader/{GithubRepo,GitlabRepo}/index.js (line 50)
  • collector/utils/extensions/Confluence/index.js (line 92)
  • collector/utils/comKey/index.js (line 9)
  • collector/utils/WhisperProviders/localWhisper.js (lines 16-17)
  • collector/utils/files/index.js (lines 13, 23)

Test files — DO NOT MIGRATE

  • collector/__tests__/utils/url/index.test.js
  • collector/__tests__/utils/extensions/Confluence/ConfluenceLoader.test.js
  • collector/__tests__/utils/WhisperProviders/ffmpeg/index.test.js
  • server/__tests__/endpoints/api/reports.test.js
  • server/__tests__/utils/agents/defaults.test.js
  • server/__tests__/utils/files/isWithin.test.js
  • server/__tests__/utils/reports/index.test.js

Proposed Solution

  1. Create server/utils/paths.js:
const path = require("path");
function getStoragePath(...subdirs) {
  const base = process.env.STORAGE_DIR || path.resolve(__dirname, "../../storage");
  return subdirs.length > 0 ? path.resolve(base, ...subdirs) : base;
}
module.exports = { getStoragePath };
  1. Create collector/utils/paths.js (same logic, different relative path)

  2. Migrate all 30+ files to use getStoragePath(...subdirs)

  3. Bug 1 fix: collector/utils/comKey/index.js:29 — replace hardcoded /app/server/ path with STORAGE_DIR reference

  4. Bug 3 fix: docker/docker-entrypoint.sh:28 — add comment about Bash 4.3+ requirement for wait -n

Migration Pattern

BEFORE:

const storageDir = process.env.STORAGE_DIR || path.resolve(__dirname, "../../storage");
const modelPath = storageDir ? path.resolve(storageDir, "models", "gemini") : path.resolve(__dirname, "../../storage/models/gemini");

AFTER:

const { getStoragePath } = require("../paths");
const modelPath = getStoragePath("models", "gemini");

Acceptance Criteria

  • server/utils/paths.js and collector/utils/paths.js exist with getStoragePath() helper
  • All 30+ files migrated (server/ + collector/)
  • grep -rn 'process\.env\.STORAGE_DIR' server/ collector/ --include="*.js" | grep -v __tests__ | grep -v paths.js → 0 results
  • Server tests pass: cd server && npx jest --ci --passWithNoTests
  • Frontend tests pass: cd frontend && npx vitest run
  • collector/utils/comKey/index.js:29 comment fixed (no hardcoded /app/server/)
  • docker/docker-entrypoint.sh:28 has Bash 4.3+ comment

Aufwand

~1-2h (mechanical refactor)

Related

Status

No work done yet. Waiting on #87 to be fixed first (Docker build broken).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions