-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdisbox-file.js
130 lines (121 loc) · 3.52 KB
/
disbox-file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const sqlite3 = require('sqlite3').verbose()
const DB_URL = process.env.DB_URL || 'disbox.db';
const db = new sqlite3.Database(DB_URL)
db.run(`
CREATE TABLE IF NOT EXISTS files (
id INTEGER PRIMARY KEY,
user_id TEXT,
parent_id INTEGER,
name TEXT,
type TEXT,
size INTEGER,
content TEXT,
created_at TEXT,
updated_at TEXT
);
`, (err) => {
if (err) {
console.log(err);
throw err;
}
})
// TODO: Optimize tree size by removing null fields
function getFileTree(user_id, resolve=() => {}) {
db.all(`SELECT * FROM files WHERE user_id = ?`, [user_id], (err, rows) => {
if (err) {
console.log(err);
resolveError(err, null);
return;
}
const allDirectories = {};
const root = {children:{}};
rows.forEach(row => {
let entry = row;
row.user_id = undefined;
if (row.type === 'directory') {
if (!(row.id in allDirectories)) {
allDirectories[row.id] = { ...row, children: {} };
} else {
allDirectories[row.id] = { ...allDirectories[row.id], ...row };
}
entry = allDirectories[row.id];
}
if (entry.parent_id === null) {
root.children[entry.name] = entry;
} else {
if (!entry.parent_id in allDirectories) {
allDirectories[entry.parent_id] = {
children: {}
};
}
allDirectories[entry.parent_id].children[entry.name] = entry;
}
});
resolve(null, root);
});
}
function updateFile(user_id, id, file, resolve=() => {}) {
// Only update present fields in sqlite
const fields = Object.keys(file);
const values = fields.map(field => `${field} = $${field}`);
const fileWithPrefixes = {};
fields.forEach((field) => {
fileWithPrefixes[`$${field}`] = file[field];
});
const sql = `UPDATE files SET ${values.join(', ')} WHERE user_id = $user_id AND id = $id`;
db.run(sql, {
...fileWithPrefixes,
$user_id: user_id, // TODO: Test spread operator override rules
$id: id
}, (err) => {
if (err) {
console.log(err);
resolve(err);
return;
} else {
resolve(null);
}
});
}
function createFile(user_id, file, resolve=() => {}) {
const fields = Object.keys(file);
fields.push('user_id');
const values = fields.map(field => `$${field}`);
const fileWithPrefixes = {};
fields.forEach((field) => {
fileWithPrefixes[`$${field}`] = file[field];
});
const sql = `INSERT INTO files (${fields.join(', ')}) VALUES (${values.join(', ')})`;
db.run(sql, {
...fileWithPrefixes,
$user_id: user_id // TODO: Test spread operator override rules
}, function(err) {
if (err) {
console.log(err);
resolve(err, null);
return;
} else {
resolve(null, this.lastID);
}
});
}
function deleteFile(user_id, id, resolve=() => {}) {
db.run(`DELETE FROM files WHERE user_id = $user_id AND id = $id`, {
$user_id: user_id,
$id: id
}, (err) => {
if (err) {
console.log(err);
resolveError(err);
return;
} else {
resolve(null);
}
});
}
module.exports = {
getFileTree,
updateFile,
createFile,
deleteFile
}