Repository navigation
Expand file tree
/
Copy pathdatabase.rb
More file actions
205 lines (162 loc) · 5.64 KB
/
Copy pathdatabase.rb
File metadata and controls
205 lines (162 loc) · 5.64 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
### Bundler and gem setup
require "rubygems"
require "bundler/setup"
require "sqlite3"
require "bcrypt"
class Database
def initialize(name = "filesharer.db")
@dbName = name;
@db = SQLite3::Database.new name;
@db.results_as_hash = true;
createTables unless tablesExist?
end
def addFile(uid, filename, expire = nil, password = nil, user = nil, requireLogin = false)
# Add the file to the database
# Will not actually move the file to the correct place in the filesystem
sql = <<-SQL
INSERT INTO `OneOffFiles`
(`userId`, `uid`, `filename`, `expire`, `password`, `requireLogin`)
VALUES (?, ?, ?, ?, ?, ?)
SQL
requireLogin = requireLogin ? 1 : 0;
@db.execute sql, [ user, uid, filename, expire, password, requireLogin ]
true;
end
def getFileData(uid)
## Get from da database
sql = <<-SQL
SELECT f.*, u.`email`, u.`firstName`, u.`lastName`
FROM `OneOffFiles` f
LEFT JOIN `User` u ON f.`userId` = u.`id`
WHERE f.`uid` = ?
SQL
@db.execute(
sql,
[ uid ]
).first;
end
def getUploads(id)
# Get uploads
sql = <<-SQL
SELECT * FROM `OneOffFiles` f
WHERE f.`userId` = ?
SQL
@db.execute(
sql,
[ id ]
);
end
# Will also return false if the file does not exist at all
def fileCollected?(uid)
collected = @db.execute(
"SELECT `collected` FROM `OneOffFiles` WHERE uid = ?",
[ uid ]
).first;
return collected != nil && collected["collected"] > 0;
end
def collectFile(uid, user = nil)
# Update to indicate the file has been collected
# Will not delete the file
sql = <<-SQL
UPDATE `OneOffFiles`
SET collected = ?, collectedUserId = ?
WHERE uid = ?
SQL
@db.execute sql, [ 1, user, uid ]
true;
end
def getUser(id)
## Get the info on a user
user = @db.execute(
"SELECT `id`, `email`, `firstName`, `lastName` FROM `User` WHERE `id` = ?",
[ id ]
).first;
return {
:id => user["id"],
:email => user["email"],
:firstName => user["firstName"],
:lastName => user["lastName"]
};
end
def authenticateUser(email, password)
## Hash the pw and authenticate it
## Also returns the user data
user = @db.execute(
"SELECT `id`, `email`, `password` FROM `User` WHERE `email` = ?",
[ email ]
).first;
if !user then
return false
end
pw = BCrypt::Password.new(user["password"]);
authenticated = (user != nil && pw == password);
if authenticated then
return { id: user["id"], email: user["email"] };
else
return false;
end
end
def createUser(email, password, firstName = nil, lastName = nil)
# Create user col in the DB
## Hash the password
hash = BCrypt::Password.create(password);
exist = @db.execute(
"SELECT `id` FROM `User` WHERE `email` = ?",
[ email ]
).count > 0;
return false if exist;
@db.execute(
"INSERT INTO `User` (`email`, `password`, `firstName`, `lastName`) VALUES (?,?,?,?)",
[ email, hash, firstName, lastName ]
);
rowid = @db.last_insert_row_id;
return getUser rowid;
end
def uidInUse?(uid)
@db.execute(
"SELECT `id` FROM `OneOffFiles` WHERE `uid` = ?",
[ uid ]
).count > 0;
end
###### Private methods #######
private
def createTables
# Create the sqlite tables here using @db.execute
## User table
@db.execute <<-SQL
CREATE TABLE IF NOT EXISTS User (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email STRING UNIQUE,
password STRING NOT NULL,
firstName STRING,
lastName STRING
);
SQL
## Table for files that can only be picked up once
@db.execute <<-SQL
CREATE TABLE IF NOT EXISTS OneOffFiles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
userId INTEGER REFERENCES User (id) ON DELETE CASCADE
ON UPDATE CASCADE,
uid STRING NOT NULL
UNIQUE,
filename NOT NULL,
expire DATETIME,
collected BOOLEAN DEFAULT (0)
NOT NULL,
collectedUserId INTEGER REFERENCES User (id),
password STRING,
requireLogin BOOLEAN NOT NULL
DEFAULT (0)
);
SQL
true;
end
def tablesExist?
# Do some SQL checks here to see if the db is set up
tables = ['User', 'OneOffFiles'];
sql = "SELECT name FROM sqlite_master WHERE type='table' AND name in (#{ Array.new(tables.count).fill('?').join(',') });"
result = @db.execute sql, tables
return result.count == tables.count;
end
end