diff --git a/backend/package.json b/backend/package.json index f29e2e1..9a0acbb 100644 --- a/backend/package.json +++ b/backend/package.json @@ -12,7 +12,7 @@ "express": "^4.18.0", "sqlite3": "^5.1.0", "jsonwebtoken": "^9.0.0", - "multer": "^1.4.5", + "multer": "^1.4.5-lts.1", "bcrypt": "^5.1.0", "cors": "^2.8.5" }, diff --git a/backend/src/db.js b/backend/src/db.js index 1947e48..5b1883c 100644 --- a/backend/src/db.js +++ b/backend/src/db.js @@ -1,72 +1,86 @@ -// Simple SQLite database wrapper -// In production, use better-sqlite3 or similar +// Simple SQLite database wrapper using sqlite3 +const sqlite3 = require('sqlite3').verbose(); -const Database = require('better-sqlite3'); -const db = new Database('clouddisk.db'); +const dbPath = process.env.DB_PATH || './clouddisk.db'; +const db = new sqlite3.Database(dbPath); // Initialize tables -db.exec(` - CREATE TABLE IF NOT EXISTS users ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - username TEXT UNIQUE NOT NULL, - password_hash TEXT NOT NULL, - email TEXT, - storage_used INTEGER DEFAULT 0, - storage_limit INTEGER DEFAULT 10737418240, - created_at DATETIME DEFAULT CURRENT_TIMESTAMP, - updated_at DATETIME DEFAULT CURRENT_TIMESTAMP - ); +db.serialize(() => { + db.run(` + CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT UNIQUE NOT NULL, + password_hash TEXT NOT NULL, + email TEXT, + storage_used INTEGER DEFAULT 0, + storage_limit INTEGER DEFAULT 10737418240, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP + ) + `); - CREATE TABLE IF NOT EXISTS files ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGER NOT NULL, - parent_id INTEGER, - name TEXT NOT NULL, - type TEXT, - size INTEGER DEFAULT 0, - path TEXT, - hash TEXT, - is_folder INTEGER DEFAULT 0, - created_at DATETIME DEFAULT CURRENT_TIMESTAMP, - updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, - deleted_at DATETIME, - FOREIGN KEY (user_id) REFERENCES users(id), - FOREIGN KEY (parent_id) REFERENCES files(id) - ); + db.run(` + CREATE TABLE IF NOT EXISTS files ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER NOT NULL, + parent_id INTEGER, + name TEXT NOT NULL, + type TEXT, + size INTEGER DEFAULT 0, + path TEXT, + hash TEXT, + is_folder INTEGER DEFAULT 0, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, + deleted_at DATETIME, + FOREIGN KEY (user_id) REFERENCES users(id), + FOREIGN KEY (parent_id) REFERENCES files(id) + ) + `); - CREATE TABLE IF NOT EXISTS shares ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - file_id INTEGER NOT NULL, - share_token TEXT UNIQUE NOT NULL, - password TEXT, - expires_at DATETIME, - view_count INTEGER DEFAULT 0, - created_at DATETIME DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (file_id) REFERENCES files(id) - ); + db.run(` + CREATE TABLE IF NOT EXISTS shares ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + file_id INTEGER NOT NULL, + share_token TEXT UNIQUE NOT NULL, + password TEXT, + expires_at DATETIME, + view_count INTEGER DEFAULT 0, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (file_id) REFERENCES files(id) + ) + `); - CREATE TABLE IF NOT EXISTS sync_logs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGER NOT NULL, - file_id INTEGER, - action TEXT, - status TEXT, - created_at DATETIME DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (user_id) REFERENCES users(id) - ); -`); + db.run(` + CREATE TABLE IF NOT EXISTS sync_status ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER NOT NULL, + last_sync_time DATETIME, + status TEXT DEFAULT 'idle', + total_files INTEGER DEFAULT 0, + synced_files INTEGER DEFAULT 0, + FOREIGN KEY (user_id) REFERENCES users(id) + ) + `); +}); -module.exports = db; +// Wrapper functions to match the old API +const query = (sql, params = []) => { + return new Promise((resolve, reject) => { + db.all(sql, params, (err, rows) => { + if (err) reject(err); + else resolve(rows); + }); + }); +}; -// Sync tables (for reference - would be in migration in production) -db.exec(` - CREATE TABLE IF NOT EXISTS sync_status ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGER NOT NULL, - last_sync_time DATETIME, - status TEXT DEFAULT 'idle', - total_files INTEGER DEFAULT 0, - synced_files INTEGER DEFAULT 0, - FOREIGN KEY (user_id) REFERENCES users(id) - ); -`); +const run = (sql, params = []) => { + return new Promise((resolve, reject) => { + db.run(sql, params, function(err) { + if (err) reject(err); + else resolve({ lastInsertRowid: this.lastID, changes: this.changes }); + }); + }); +}; + +module.exports = { query, run, db };