aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/pinapelz/FileSystem.java
blob: e86cf6e9e10cf18da32c36802bef12a0fc82bebe (plain) (blame)
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
package com.pinapelz;

import net.dv8tion.jda.api.entities.Message;

import java.sql.ResultSet;
import java.sql.SQLException;


public class FileSystem {
    private Database database;
    public FileSystem(String dbHost, String dbUser, String dbPass, String dbName){
        database = new Database(dbHost, dbUser, dbPass, dbName);
    }

    public DiscordFilePath getFileById(int fileId){
        String[] rawDiscordFilePath = database.getFileById(fileId);
        DiscordFilePath discPath = new DiscordFilePath();
        discPath.channelId = Long.parseLong(rawDiscordFilePath[0]);
        discPath.messageId = Long.parseLong(rawDiscordFilePath[1]);
        discPath.fileName = rawDiscordFilePath[2];
        return discPath;
    }

    public void createNewFile(String channelId, String messageId, int directoryId, String description, Message.Attachment attachment){
        int fileSize = attachment.getSize();
        String filename = attachment.getFileName();
        String mimeType = attachment.getContentType();
        try {
            database.recordFileMetadata(channelId, messageId, directoryId, filename, description, fileSize, mimeType );
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public ResultSet getFilesByDirectoryId(int directoryId, String search, String mimeTypeFilter, String sortBy) {
        return database.getFilesByDirectoryId(directoryId, search, mimeTypeFilter, sortBy);
    }

    public int findOrCreateDirectory(String path) throws SQLException {
        ResultSet rs = getAllDirectories();
        while (rs.next()) {
            if (path.equals(rs.getString("path"))) {
                int id = rs.getInt("directory_id");
                rs.close();
                return id;
            }
        }
        rs.close();

        return createDirectory(path);
    }

    public ResultSet getAllDirectories() {
        return database.getAllDirectories();
    }

    public ResultSet getDirectoryById(int directoryId) {
        return database.getDirectoryById(directoryId);
    }

    public int createDirectory(String path) throws SQLException {
        return database.createDirectory(path);
    }

    public boolean deleteFile(int fileId) throws SQLException {
        return database.deleteFile(fileId);
    }

    public boolean deleteDirectory(int directoryId) throws SQLException {
        return database.deleteDirectory(directoryId);
    }

    public long createFilePartial(String channelId, String messageId, int directoryId, 
                                 String partName, int partNumber, long partSize, 
                                 String originalFilename, String description, String mimeType) throws SQLException {
        return database.recordFilePartial(channelId, messageId, directoryId, partName, 
                                        partNumber, partSize, originalFilename, description, mimeType);
    }

    public ResultSet getFilePartialsByOriginalFilename(String originalFilename, int directoryId) {
        return database.getFilePartialsByOriginalFilename(originalFilename, directoryId);
    }

    public ResultSet getGroupedPartials(int directoryId, String search) {
        return database.getUniqueOriginalFilesFromPartials(directoryId, search);
    }

    public boolean deleteFilePartials(String originalFilename, int directoryId) throws SQLException {
        return database.deleteFilePartials(originalFilename, directoryId);
    }

    public boolean checkPartialNameConstraint(String partName, int directoryId) throws SQLException {
        return database.checkPartialExists(partName, directoryId);
    }
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage