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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
package com.pinapelz;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.*;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
public class Database {
private final DataSource ds;
public record FileEntry(
int fileId,
String fileName,
String description,
long size,
String mimeType,
OffsetDateTime createdAt
) {}
public record DirectoryEntry(
int directoryId,
String path,
OffsetDateTime createdAt,
int fileCount
) {}
public record FilePartialEntry(
long partialId,
String channelId,
String messageId,
String partName,
int partNumber,
long partSize,
String originalFilename,
String mimeType,
boolean uploadedViaWebhook,
OffsetDateTime createdAt
) {}
public record PartialGroupEntry(
String originalFilename,
String mimeType,
int directoryId,
OffsetDateTime createdAt,
long size,
String description
) {}
public Database(String host, String user, String password, String db) {
try {
ds = createDataSource(host, user, password, db);
try (Connection c = ds.getConnection();
Statement s = c.createStatement()) {
s.execute(Files.readString(Path.of("schema.sql")));
}
} catch (IOException | SQLException e) {
throw new RuntimeException(e);
}
}
private static DataSource createDataSource(String host, String user, String pass, String db) {
HikariConfig c = new HikariConfig();
c.setJdbcUrl("jdbc:postgresql://" + host + "/" + db + "?sslmode=require&channel_binding=require");
c.setUsername(user);
c.setPassword(pass);
c.setMaximumPoolSize(3);
c.setMinimumIdle(1);
c.setIdleTimeout(60_000);
c.setMaxLifetime(600_000);
c.addDataSourceProperty("reWriteBatchedInserts", "true");
return new HikariDataSource(c);
}
public void recordFileMetadata(
String channelId, String messageId, int dirId,
String name, String desc, int size, String mime
) throws SQLException {
String sql = """
INSERT INTO files
(disc_channel_id, disc_message_id, directory_id,
file_name, file_description, size, mime_type)
VALUES (?, ?, ?, ?, ?, ?, ?)
""";
try (Connection c = ds.getConnection();
PreparedStatement p = c.prepareStatement(sql)) {
p.setString(1, channelId);
p.setString(2, messageId);
p.setInt(3, dirId);
p.setString(4, name);
p.setString(5, desc);
p.setInt(6, size);
p.setString(7, mime);
p.executeUpdate();
}
}
public String[] getFileById(int fileId) {
String sql = """
SELECT disc_channel_id, disc_message_id, file_name
FROM files WHERE file_id = ?
""";
try (Connection c = ds.getConnection();
PreparedStatement p = c.prepareStatement(sql)) {
p.setInt(1, fileId);
try (ResultSet r = p.executeQuery()) {
if (!r.next()) throw new RuntimeException();
return new String[]{ r.getString(1), r.getString(2), r.getString(3) };
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public List<FileEntry> getFilesByDirectoryId(
int dirId, String search, String mime, String sort
) {
StringBuilder q = new StringBuilder("""
SELECT file_id, file_name, file_description,
size, mime_type, created_at
FROM files WHERE directory_id = ?
""");
if (search != null && !search.isBlank())
q.append(" AND (LOWER(file_name) LIKE ? OR LOWER(file_description) LIKE ?)");
if (mime != null && !mime.isBlank())
q.append(" AND mime_type LIKE ?");
q.append(" ORDER BY ")
.append("size".equals(sort) ? "size DESC" :
"file_name".equals(sort) ? "file_name ASC" :
"created_at DESC");
try (Connection c = ds.getConnection();
PreparedStatement p = c.prepareStatement(q.toString())) {
int i = 1;
p.setInt(i++, dirId);
if (search != null && !search.isBlank()) {
String s = "%" + search.toLowerCase() + "%";
p.setString(i++, s);
p.setString(i++, s);
}
if (mime != null && !mime.isBlank())
p.setString(i++, mime + "%");
List<FileEntry> out = new ArrayList<>();
try (ResultSet r = p.executeQuery()) {
while (r.next())
out.add(new FileEntry(
r.getInt(1), r.getString(2), r.getString(3),
r.getLong(4), r.getString(5),
r.getObject(6, OffsetDateTime.class)
));
}
return out;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public boolean deleteFile(int fileId) throws SQLException {
try (Connection c = ds.getConnection();
PreparedStatement p = c.prepareStatement("DELETE FROM files WHERE file_id = ?")) {
p.setInt(1, fileId);
return p.executeUpdate() > 0;
}
}
public List<DirectoryEntry> getAllDirectories() {
String sql = """
SELECT d.directory_id, d.path, d.created_at,
COUNT(f.file_id)
FROM directories d
LEFT JOIN files f USING (directory_id)
GROUP BY d.directory_id
ORDER BY d.path
""";
try (Connection c = ds.getConnection();
PreparedStatement p = c.prepareStatement(sql);
ResultSet r = p.executeQuery()) {
List<DirectoryEntry> out = new ArrayList<>();
while (r.next())
out.add(new DirectoryEntry(
r.getInt(1), r.getString(2),
r.getObject(3, OffsetDateTime.class),
r.getInt(4)
));
return out;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public DirectoryEntry getDirectoryById(int id) {
String sql = """
SELECT d.directory_id, d.path, d.created_at,
COUNT(f.file_id)
FROM directories d
LEFT JOIN files f USING (directory_id)
WHERE d.directory_id = ?
GROUP BY d.directory_id
""";
try (Connection c = ds.getConnection();
PreparedStatement p = c.prepareStatement(sql)) {
p.setInt(1, id);
try (ResultSet r = p.executeQuery()) {
if (!r.next()) throw new RuntimeException();
return new DirectoryEntry(
r.getInt(1), r.getString(2),
r.getObject(3, OffsetDateTime.class),
r.getInt(4)
);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public int createDirectory(String path) throws SQLException {
try (Connection c = ds.getConnection();
PreparedStatement p = c.prepareStatement("""
INSERT INTO directories (path)
VALUES (?)
ON CONFLICT (path) DO UPDATE SET path = EXCLUDED.path
RETURNING directory_id
""")) {
p.setString(1, path);
try (ResultSet r = p.executeQuery()) {
r.next();
return r.getInt(1);
}
}
}
public boolean deleteDirectory(int directoryId) throws SQLException {
if (directoryId == 1) throw new SQLException();
try (Connection c = ds.getConnection()) {
try (PreparedStatement check = c.prepareStatement(
"SELECT COUNT(*) FROM files WHERE directory_id = ?")) {
check.setInt(1, directoryId);
try (ResultSet r = check.executeQuery()) {
r.next();
if (r.getInt(1) > 0) throw new SQLException();
}
}
try (PreparedStatement del = c.prepareStatement(
"DELETE FROM directories WHERE directory_id = ?")) {
del.setInt(1, directoryId);
return del.executeUpdate() > 0;
}
}
}
public long recordFilePartial(
String channelId, String messageId, int dirId,
String partName, int partNumber, long partSize,
String original, String desc, String mime
) throws SQLException {
String sql = """
INSERT INTO file_partials
(disc_channel_id, disc_message_id, directory_id,
part_name, part_number, part_size,
original_filename, file_description, mime_type,
uploaded_via_webhook)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, true)
RETURNING partial_id
""";
try (Connection c = ds.getConnection();
PreparedStatement p = c.prepareStatement(sql)) {
p.setString(1, channelId);
p.setString(2, messageId);
p.setInt(3, dirId);
p.setString(4, partName);
p.setInt(5, partNumber);
p.setLong(6, partSize);
p.setString(7, original);
p.setString(8, desc);
p.setString(9, mime);
try (ResultSet r = p.executeQuery()) {
r.next();
return r.getLong(1);
}
}
}
public boolean checkPartialExists(String partName, int dirId) throws SQLException {
try (Connection c = ds.getConnection();
PreparedStatement p = c.prepareStatement(
"SELECT COUNT(*) FROM file_partials WHERE part_name = ? AND directory_id = ?")) {
p.setString(1, partName);
p.setInt(2, dirId);
try (ResultSet r = p.executeQuery()) {
r.next();
return r.getInt(1) > 0;
}
}
}
public boolean deleteFilePartials(String original, int dirId) throws SQLException {
try (Connection c = ds.getConnection();
PreparedStatement p = c.prepareStatement(
"DELETE FROM file_partials WHERE original_filename = ? AND directory_id = ?")) {
p.setString(1, original);
p.setInt(2, dirId);
return p.executeUpdate() > 0;
}
}
public List<FilePartialEntry> getFilePartialsByOriginalFilename(String original, int dirId) {
String sql = """
SELECT partial_id, disc_channel_id, disc_message_id,
part_name, part_number, part_size,
original_filename, mime_type,
uploaded_via_webhook, created_at
FROM file_partials
WHERE original_filename = ? AND directory_id = ?
ORDER BY part_number
""";
try (Connection c = ds.getConnection();
PreparedStatement p = c.prepareStatement(sql)) {
p.setString(1, original);
p.setInt(2, dirId);
List<FilePartialEntry> out = new ArrayList<>();
try (ResultSet r = p.executeQuery()) {
while (r.next())
out.add(new FilePartialEntry(
r.getLong(1), r.getString(2), r.getString(3),
r.getString(4), r.getInt(5), r.getLong(6),
r.getString(7), r.getString(8),
r.getBoolean(9),
r.getObject(10, OffsetDateTime.class)
));
}
return out;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public List<PartialGroupEntry> getUniqueOriginalFilesFromPartials(int dirId, String search) {
StringBuilder q = new StringBuilder("""
SELECT original_filename, mime_type, directory_id,
MAX(created_at), SUM(part_size), MAX(file_description)
FROM file_partials
WHERE directory_id = ?
""");
if (search != null && !search.isBlank())
q.append(" AND LOWER(original_filename) LIKE ?");
q.append(" GROUP BY original_filename, mime_type, directory_id ORDER BY original_filename");
try (Connection c = ds.getConnection();
PreparedStatement p = c.prepareStatement(q.toString())) {
int i = 1;
p.setInt(i++, dirId);
if (search != null && !search.isBlank())
p.setString(i++, "%" + search.toLowerCase() + "%");
List<PartialGroupEntry> out = new ArrayList<>();
try (ResultSet r = p.executeQuery()) {
while (r.next())
out.add(new PartialGroupEntry(
r.getString(1),
r.getString(2),
r.getInt(3),
r.getObject(4, OffsetDateTime.class),
r.getLong(5),
r.getString(6)
));
}
return out;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
|