Skip to content

Commit c21a4f5

Browse files
committed
Changed file request ID to string type, updated openapi
1 parent 4a2abde commit c21a4f5

File tree

20 files changed

+147
-132
lines changed

20 files changed

+147
-132
lines changed

internal/configuration/database/Database.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ func GetApiKey(id string) (models.ApiKey, bool) {
128128
return db.GetApiKey(id)
129129
}
130130

131+
// GetApiKeyByPublicKey returns an API key by using the public key
132+
func GetApiKeyByPublicKey(publicKey string) (string, bool) {
133+
return db.GetApiKeyByPublicKey(publicKey)
134+
}
135+
136+
// GetApiKeyByFileRequest returns an API key used for a file request
137+
func GetApiKeyByFileRequest(request models.FileRequest) (string, bool) {
138+
return db.GetApiKeyByFileRequest(request)
139+
}
140+
131141
// SaveApiKey saves the API key to the database
132142
func SaveApiKey(apikey models.ApiKey) {
133143
db.SaveApiKey(apikey)
@@ -143,11 +153,6 @@ func DeleteApiKey(id string) {
143153
db.DeleteApiKey(id)
144154
}
145155

146-
// GetApiKeyByPublicKey returns an API key by using the public key
147-
func GetApiKeyByPublicKey(publicKey string) (string, bool) {
148-
return db.GetApiKeyByPublicKey(publicKey)
149-
}
150-
151156
// E2E Section
152157

153158
// SaveEnd2EndInfo stores the encrypted e2e info
@@ -323,7 +328,7 @@ func EditSuperAdmin(username, passwordHash string) error {
323328
// File Requests
324329

325330
// GetFileRequest returns the FileRequest or false if not found
326-
func GetFileRequest(id int) (models.FileRequest, bool) {
331+
func GetFileRequest(id string) (models.FileRequest, bool) {
327332
return db.GetFileRequest(id)
328333
}
329334

@@ -332,10 +337,9 @@ func GetAllFileRequests() []models.FileRequest {
332337
return db.GetAllFileRequests()
333338
}
334339

335-
// SaveFileRequest stores the hotlink associated with the file in the database
336-
// Returns the ID of the new request
337-
func SaveFileRequest(request models.FileRequest) int {
338-
return db.SaveFileRequest(request)
340+
// SaveFileRequest stores the file request associated with the file in the database
341+
func SaveFileRequest(request models.FileRequest) {
342+
db.SaveFileRequest(request)
339343
}
340344

341345
// DeleteFileRequest deletes a file request with the given ID

internal/configuration/database/dbabstraction/DbAbstraction.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type Database interface {
3838
GetAllApiKeys() map[string]models.ApiKey
3939
// GetApiKey returns a models.ApiKey if valid or false if the ID is not valid
4040
GetApiKey(id string) (models.ApiKey, bool)
41+
// GetApiKeyByFileRequest returns an API key used for a file request
42+
GetApiKeyByFileRequest(request models.FileRequest) (string, bool)
4143
// SaveApiKey saves the API key to the database
4244
SaveApiKey(apikey models.ApiKey)
4345
// UpdateTimeApiKey writes the content of LastUsage to the database
@@ -99,12 +101,11 @@ type Database interface {
99101
DeleteUser(id int)
100102

101103
// GetFileRequest returns the FileRequest or false if not found
102-
GetFileRequest(id int) (models.FileRequest, bool)
104+
GetFileRequest(id string) (models.FileRequest, bool)
103105
// GetAllFileRequests returns an array with all file requests, ordered by creation date
104106
GetAllFileRequests() []models.FileRequest
105-
// SaveFileRequest stores the hotlink associated with the file in the database
106-
// Returns the ID of the new request
107-
SaveFileRequest(request models.FileRequest) int
107+
// SaveFileRequest stores the file request associated with the file in the database
108+
SaveFileRequest(request models.FileRequest)
108109
// DeleteFileRequest deletes a file request with the given ID
109110
DeleteFileRequest(request models.FileRequest)
110111

internal/configuration/database/provider/redis/apikeys.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ func (p DatabaseProvider) GetApiKeyByPublicKey(publicKey string) (string, bool)
5353
return "", false
5454
}
5555

56+
// GetApiKeyByFileRequest returns an API key used for a file request
57+
func (p DatabaseProvider) GetApiKeyByFileRequest(request models.FileRequest) (string, bool) {
58+
keys := p.GetAllApiKeys()
59+
for _, key := range keys {
60+
if key.UploadRequestId == request.Id {
61+
return key.Id, true
62+
}
63+
}
64+
return "", false
65+
}
66+
5667
// SaveApiKey saves the API key to the database
5768
func (p DatabaseProvider) SaveApiKey(apikey models.ApiKey) {
5869
p.setHashMap(p.buildArgs(prefixApiKeys + apikey.Id).AddFlat(apikey))

internal/configuration/database/provider/redis/filerequests.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ package redis
33
import (
44
"cmp"
55
"slices"
6-
"strconv"
76

87
"github.com/forceu/gokapi/internal/helper"
98
"github.com/forceu/gokapi/internal/models"
109
redigo "github.com/gomodule/redigo/redis"
1110
)
1211

1312
const (
14-
prefixFileRequests = "frq:"
15-
prefixFileRequestCounter = "frq_max"
13+
prefixFileRequests = "frq:"
1614
)
1715

1816
func dbToFileRequest(input []any) (models.FileRequest, error) {
@@ -25,8 +23,8 @@ func dbToFileRequest(input []any) (models.FileRequest, error) {
2523
}
2624

2725
// GetFileRequest returns the FileRequest or false if not found
28-
func (p DatabaseProvider) GetFileRequest(id int) (models.FileRequest, bool) {
29-
result, ok := p.getHashMap(prefixFileRequests + strconv.Itoa(id))
26+
func (p DatabaseProvider) GetFileRequest(id string) (models.FileRequest, bool) {
27+
result, ok := p.getHashMap(prefixFileRequests + id)
3028
if !ok {
3129
return models.FileRequest{}, false
3230
}
@@ -57,23 +55,12 @@ func sortFilerequests(users []models.FileRequest) []models.FileRequest {
5755
return users
5856
}
5957

60-
// SaveFileRequest stores the hotlink associated with the file in the database
61-
// Returns the ID of the new request
62-
func (p DatabaseProvider) SaveFileRequest(request models.FileRequest) int {
63-
if request.Id == 0 {
64-
id := p.getIncreasedInt(prefixFileRequestCounter)
65-
request.Id = id
66-
} else {
67-
counter, _ := p.getKeyInt(prefixFileRequestCounter)
68-
if counter < request.Id {
69-
p.setKey(prefixFileRequestCounter, request.Id)
70-
}
71-
}
72-
p.setHashMap(p.buildArgs(prefixUsers + strconv.Itoa(request.Id)).AddFlat(request))
73-
return request.Id
58+
// SaveFileRequest stores the file request associated with the file in the database
59+
func (p DatabaseProvider) SaveFileRequest(request models.FileRequest) {
60+
p.setHashMap(p.buildArgs(prefixUsers + request.Id).AddFlat(request))
7461
}
7562

7663
// DeleteFileRequest deletes a file request with the given ID
7764
func (p DatabaseProvider) DeleteFileRequest(request models.FileRequest) {
78-
p.deleteKey(prefixFileRequests + strconv.Itoa(request.Id))
65+
p.deleteKey(prefixFileRequests + request.Id)
7966
}

internal/configuration/database/provider/sqlite/Sqlite.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ func (p DatabaseProvider) Upgrade(currentDbVersion int) {
4949
// pre upload requests
5050
if currentDbVersion < 12 {
5151

52-
err := p.rawSqlite(`ALTER TABLE FileMetaData ADD COLUMN "UploadRequestId" INTEGER NOT NULL DEFAULT 0;
53-
ALTER TABLE ApiKeys ADD COLUMN "UploadRequestId" INTEGER NOT NULL DEFAULT 0;
52+
err := p.rawSqlite(`ALTER TABLE FileMetaData ADD COLUMN "UploadRequestId" TEXT NOT NULL DEFAULT '';
53+
ALTER TABLE ApiKeys ADD COLUMN "UploadRequestId" TEXT NOT NULL DEFAULT '';
5454
CREATE TABLE "UploadRequests" (
55-
"id" INTEGER NOT NULL UNIQUE,
55+
"id" TEXT NOT NULL UNIQUE,
5656
"name" TEXT NOT NULL,
5757
"userid" INTEGER NOT NULL,
5858
"expiry" INTEGER NOT NULL,
5959
"maxFiles" INTEGER NOT NULL,
6060
"maxSize" INTEGER NOT NULL,
6161
"creation" INTEGER NOT NULL,
6262
"apiKey" TEXT NOT NULL UNIQUE,
63-
PRIMARY KEY("id" AUTOINCREMENT)
63+
PRIMARY KEY("id")
6464
);
6565
CREATE TABLE "Presign" (
6666
"id" TEXT NOT NULL UNIQUE,
@@ -165,7 +165,7 @@ func (p DatabaseProvider) createNewDatabase() error {
165165
"IsSystemKey" INTEGER,
166166
"UserId" INTEGER NOT NULL,
167167
"PublicId" TEXT NOT NULL UNIQUE ,
168-
"UploadRequestId" INTEGER NOT NULL,
168+
"UploadRequestId" TEXT NOT NULL,
169169
PRIMARY KEY("Id")
170170
) WITHOUT ROWID;
171171
CREATE TABLE "E2EConfig" (
@@ -193,7 +193,7 @@ func (p DatabaseProvider) createNewDatabase() error {
193193
"UserId" INTEGER NOT NULL,
194194
"UploadDate" INTEGER NOT NULL,
195195
"PendingDeletion" INTEGER NOT NULL,
196-
"UploadRequestId" INTEGER NOT NULL,
196+
"UploadRequestId" TEXT NOT NULL,
197197
PRIMARY KEY("Id")
198198
);
199199
CREATE TABLE "Hotlinks" (
@@ -219,15 +219,15 @@ func (p DatabaseProvider) createNewDatabase() error {
219219
PRIMARY KEY("Id" AUTOINCREMENT)
220220
);
221221
CREATE TABLE "UploadRequests" (
222-
"id" INTEGER NOT NULL UNIQUE,
222+
"id" TEXT NOT NULL UNIQUE,
223223
"name" TEXT,
224224
"userid" INTEGER NOT NULL,
225225
"expiry" INTEGER NOT NULL,
226226
"maxFiles" INTEGER NOT NULL,
227227
"maxSize" INTEGER NOT NULL,
228228
"creation" INTEGER NOT NULL,
229229
"apiKey" TEXT NOT NULL UNIQUE,
230-
PRIMARY KEY("id" AUTOINCREMENT)
230+
PRIMARY KEY("id")
231231
);
232232
CREATE TABLE "Presign" (
233233
"id" TEXT NOT NULL UNIQUE,

internal/configuration/database/provider/sqlite/apikeys.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type schemaApiKeys struct {
1818
IsSystemKey int
1919
UserId int
2020
PublicId string
21-
UploadRequestId int
21+
UploadRequestId string
2222
}
2323

2424
// currentTime is used in order to modify the current time for testing purposes in unit tests
@@ -97,6 +97,21 @@ func (p DatabaseProvider) GetApiKeyByPublicKey(publicKey string) (string, bool)
9797
return rowResult.Id, true
9898
}
9999

100+
// GetApiKeyByFileRequest returns an API key used for a file request
101+
func (p DatabaseProvider) GetApiKeyByFileRequest(request models.FileRequest) (string, bool) {
102+
var rowResult schemaApiKeys
103+
row := p.sqliteDb.QueryRow("SELECT Id FROM ApiKeys WHERE UploadRequestId = ? LIMIT 1", request.Id)
104+
err := row.Scan(&rowResult.Id)
105+
if err != nil {
106+
if errors.Is(err, sql.ErrNoRows) {
107+
return "", false
108+
}
109+
helper.Check(err)
110+
return "", false
111+
}
112+
return rowResult.Id, true
113+
}
114+
100115
// SaveApiKey saves the API key to the database
101116
func (p DatabaseProvider) SaveApiKey(apikey models.ApiKey) {
102117
isSystemKey := 0

internal/configuration/database/provider/sqlite/filerequests.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
type schemaFileRequests struct {
12-
Id int
12+
Id string
1313
Name string
1414
UserId int
1515
Expiry int64
@@ -20,7 +20,7 @@ type schemaFileRequests struct {
2020
}
2121

2222
// GetFileRequest returns the FileRequest or false if not found
23-
func (p DatabaseProvider) GetFileRequest(id int) (models.FileRequest, bool) {
23+
func (p DatabaseProvider) GetFileRequest(id string) (models.FileRequest, bool) {
2424
var rowResult schemaFileRequests
2525
row := p.sqliteDb.QueryRow("SELECT * FROM UploadRequests WHERE Id = ?", id)
2626
err := row.Scan(&rowResult.Id, &rowResult.Name, &rowResult.UserId, &rowResult.Expiry,
@@ -70,9 +70,8 @@ func (p DatabaseProvider) GetAllFileRequests() []models.FileRequest {
7070
return result
7171
}
7272

73-
// SaveFileRequest stores the hotlink associated with the file in the database
74-
// Returns the ID of the new request
75-
func (p DatabaseProvider) SaveFileRequest(request models.FileRequest) int {
73+
// SaveFileRequest stores the file request associated with the file in the database
74+
func (p DatabaseProvider) SaveFileRequest(request models.FileRequest) {
7675
newData := schemaFileRequests{
7776
Id: request.Id,
7877
Name: request.Name,
@@ -84,25 +83,14 @@ func (p DatabaseProvider) SaveFileRequest(request models.FileRequest) int {
8483
ApiKey: request.ApiKey,
8584
}
8685

87-
// If ID is not 0, then an existing file request is being saved and needs to be
88-
// replaced in the database
89-
if newData.Id != 0 {
90-
_, err := p.sqliteDb.Exec("INSERT OR REPLACE INTO UploadRequests (id, name, userid, expiry, maxFiles, maxSize, creation, apiKey) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
91-
newData.Id, newData.Name, newData.UserId, newData.Expiry, newData.MaxFiles, newData.MaxSize, newData.Creation, newData.ApiKey)
92-
helper.Check(err)
93-
return newData.Id
94-
}
95-
res, err := p.sqliteDb.Exec("INSERT INTO UploadRequests (name, userid, expiry, maxFiles, maxSize, creation, apiKey) VALUES (?, ?, ?, ?, ?, ?, ?)",
96-
newData.Name, newData.UserId, newData.Expiry, newData.MaxFiles, newData.MaxSize, newData.Creation, newData.ApiKey)
97-
helper.Check(err)
98-
id, err := res.LastInsertId()
86+
_, err := p.sqliteDb.Exec("INSERT OR REPLACE INTO UploadRequests (id, name, userid, expiry, maxFiles, maxSize, creation, apiKey) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
87+
newData.Id, newData.Name, newData.UserId, newData.Expiry, newData.MaxFiles, newData.MaxSize, newData.Creation, newData.ApiKey)
9988
helper.Check(err)
100-
return int(id)
10189
}
10290

10391
// DeleteFileRequest deletes a file request with the given ID
10492
func (p DatabaseProvider) DeleteFileRequest(request models.FileRequest) {
105-
if request.Id == 0 {
93+
if request.Id == "" {
10694
return
10795
}
10896
_, err := p.sqliteDb.Exec("DELETE FROM UploadRequests WHERE Id = ?", request.Id)

internal/configuration/database/provider/sqlite/metadata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type schemaMetaData struct {
2929
UserId int
3030
UploadDate int64
3131
PendingDeletion int64
32-
UploadRequestId int
32+
UploadRequestId string
3333
}
3434

3535
func (rowData schemaMetaData) ToFileModel() (models.File, error) {

internal/models/ApiKey.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type ApiKey struct {
4444
Expiry int64 `json:"Expiry" redis:"Expiry"` // Does not expire if 0
4545
IsSystemKey bool `json:"IsSystemKey" redis:"IsSystemKey"`
4646
UserId int `json:"UserId" redis:"UserId"`
47-
UploadRequestId int `json:"UploadRequestId" redis:"UploadRequestId"`
47+
UploadRequestId string `json:"UploadRequestId" redis:"UploadRequestId"`
4848
}
4949

5050
// ApiPermission contains zero or more permissions as an uint16 format
@@ -159,5 +159,5 @@ type ApiKeyOutput struct {
159159

160160
// IsUploadRequestKey returns true if it is used for file requests
161161
func (key *ApiKey) IsUploadRequestKey() bool {
162-
return key.UploadRequestId != 0
162+
return key.UploadRequestId != ""
163163
}

internal/models/FileList.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ type File struct {
1919
HotlinkId string `json:"HotlinkId" redis:"HotlinkId"` // If file is a picture file and can be hotlinked, this is the ID for the hotlink
2020
ContentType string `json:"ContentType" redis:"ContentType"` // The MIME type for the file
2121
AwsBucket string `json:"AwsBucket" redis:"AwsBucket"` // If the file is stored in the cloud, this is the bucket that is being used
22+
UploadRequestId string `json:"FileRequestId" redis:"FileRequestId"` // If the file belongs to a file request, this is the ID of the file request
2223
ExpireAt int64 `json:"ExpireAt" redis:"ExpireAt"` // UTC timestamp of file expiry
2324
PendingDeletion int64 `json:"PendingDeletion" redis:"PendingDeletion"` // UTC timestamp when the file will be deleted, if pending. Otherwise 0
2425
SizeBytes int64 `json:"SizeBytes" redis:"SizeBytes"` // Filesize in bytes
2526
UploadDate int64 `json:"UploadDate" redis:"UploadDate"` // UTC timestamp of upload time
2627
DownloadsRemaining int `json:"DownloadsRemaining" redis:"DownloadsRemaining"` // The remaining downloads for this file
2728
DownloadCount int `json:"DownloadCount" redis:"DownloadCount"` // The number of times the file has been downloaded
2829
UserId int `json:"UserId" redis:"UserId"` // The user ID of the uploader
29-
UploadRequestId int `json:"FileRequestId" redis:"FileRequestId"` // If the file belongs to a file request, this is the ID of the file request
3030
Encryption EncryptionInfo `json:"Encryption" redis:"-"` // If the file is encrypted, this stores all info for decrypting
3131
UnlimitedDownloads bool `json:"UnlimitedDownloads" redis:"UnlimitedDownloads"` // True if the uploader did not limit the downloads
3232
UnlimitedTime bool `json:"UnlimitedTime" redis:"UnlimitedTime"` // True if the uploader did not limit the time
@@ -43,6 +43,7 @@ type FileApiOutput struct {
4343
ExpireAtString string `json:"ExpireAtString"` // Time expiry in a human-readable format in UTC
4444
UrlDownload string `json:"UrlDownload"` // The public download URL for the file
4545
UrlHotlink string `json:"UrlHotlink"` // The public hotlink URL for the file
46+
FileRequestId string `json:"FileRequestId"` // The ID of the file request
4647
UploadDate int64 `json:"UploadDate"` // UTC timestamp of upload time
4748
ExpireAt int64 `json:"ExpireAt"` // UTC timestamp of file expiry
4849
SizeBytes int64 `json:"SizeBytes"` // Filesize in bytes
@@ -58,7 +59,6 @@ type FileApiOutput struct {
5859
IsPendingDeletion bool `json:"IsPendingDeletion"` // True if the file is about to be deleted
5960
IsFileRequest bool `json:"IsFileRequest"` // True if the file belongs to a file request
6061
UploaderId int `json:"UploaderId"` // The user ID of the uploader
61-
FileRequestId int `json:"FileRequestId"` // The ID of the file request
6262
}
6363

6464
// EncryptionInfo holds information about the encryption used on the file
@@ -86,7 +86,7 @@ func (f *File) ToFileApiOutput(serverUrl string, useFilenameInUrl bool) (FileApi
8686
if err != nil {
8787
return FileApiOutput{}, err
8888
}
89-
result.IsFileRequest = f.UploadRequestId != 0
89+
result.IsFileRequest = f.UploadRequestId != ""
9090
result.IsPasswordProtected = f.PasswordHash != ""
9191
result.IsEncrypted = f.Encryption.IsEncrypted
9292
result.IsSavedOnLocalStorage = f.AwsBucket == ""
@@ -155,7 +155,7 @@ func (f *File) RequiresClientDecryption() bool {
155155

156156
// IsFileRequest checks if the file is uploaded for an upload request
157157
func (f *File) IsFileRequest() bool {
158-
return f.UploadRequestId != 0
158+
return f.UploadRequestId != ""
159159
}
160160
func errorAsJson(err error) string {
161161
fmt.Println(err)

0 commit comments

Comments
 (0)