Skip to content

Commit

Permalink
refactor(uploads): overhaul file handling and paths across containers
Browse files Browse the repository at this point in the history
BREAKING CHANGES:
- Simplified directory structure for uploads
- Remove filesystem operations from frontend consts.ts
- Standardize paths across containers
- Update API URLs to use container hostnames

Features:
- Remove redundant directory creation logic
- Simplify path resolution in frontend
- Add better error handling for file operations
- Use container networking DNS resolution

Technical Updates:
- Change API_BASE_URL to use service name: kenyare-backend:8000
- Remove fs operations from consts.ts
- Update path handling in saveFile function
- Add detailed error logging
- Fix directory permissions
  • Loading branch information
kurosakiaduma committed Jan 22, 2025
1 parent 78af46f commit 5ed14de
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 26 deletions.
22 changes: 4 additions & 18 deletions src/lib/consts.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import "dotenv/config";
import path from 'path';
import fs from 'fs';

const FLASK_PORT = process.env.FLASK_PORT ?? 8000;
const FLASK_HOST = process.env.FLASK_HOST ?? "http://127.0.0.1";
export const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || "http://kenyare-backend:8000";
const UPLOADS_DIR = 'uploads';
export const FINANCIAL_AUDITS_DIR = '/api/static/uploads/audits';
export const PROPOSAL_FORMS_DIR = '/api/static/uploads/proposals';
export const QUOTATIONS_DIR = '/api/static/uploads/quotations';
// Update paths to be relative to container mount point
export const FINANCIAL_AUDITS_DIR = 'audits';
export const PROPOSAL_FORMS_DIR = 'proposals';
export const QUOTATIONS_DIR = 'quotations';
export const DELETE_UPLOADS = process.env.DELETE_UPLOADS === "1";

if (!fs.existsSync(UPLOADS_DIR)) await fs.promises.mkdir(UPLOADS_DIR);
if (process.env.CLEAR_UPLOADS_DIR === "1") {
await fs.promises.rm(UPLOADS_DIR, { recursive: true });
await fs.promises.mkdir(UPLOADS_DIR);

}

if (!fs.existsSync(FINANCIAL_AUDITS_DIR)) await fs.promises.mkdir(FINANCIAL_AUDITS_DIR);
if (!fs.existsSync(PROPOSAL_FORMS_DIR)) await fs.promises.mkdir(PROPOSAL_FORMS_DIR);
10 changes: 2 additions & 8 deletions src/routes/quotation/input/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ import type { QuotationInput } from '$lib/types';
async function saveFile(file: File, saveDir: string): Promise<string> {
const fileExtension = file.name.split('.').pop();
const fileName = `${randomUUID()}.${fileExtension}`;

// Use absolute paths for file operations
const uploadDir = path.join('/app/static/uploads', saveDir);
const filePath = path.join(uploadDir, fileName);

// Ensure directory exists
await fs.promises.mkdir(uploadDir, { recursive: true });
const filePath = path.join('/app/static/uploads', saveDir, fileName);

try {
const arrayBuffer = await file.arrayBuffer();
const dataView = new DataView(arrayBuffer);
await fs.promises.writeFile(filePath, dataView);

// Return relative path for API
// Return path relative to uploads directory
return `/static/uploads/${saveDir}/${fileName}`;
} catch (err) {
console.error(`Failed to save file: ${err}`);
Expand Down

0 comments on commit 5ed14de

Please sign in to comment.