Skip to content

Commit 96f975d

Browse files
committed
Refactoring JS, adding note for upload requests, better view for public upload
1 parent f315b3b commit 96f975d

File tree

23 files changed

+408
-166
lines changed

23 files changed

+408
-166
lines changed

build/go-generate/minifyStaticContent.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,10 @@ func getPaths() []converter {
7272
Name: "wasm_exec JS",
7373
})
7474
result = append(result, converter{
75-
InputPath: pathPrefix + "js/dateformat.js",
76-
OutputPath: pathPrefix + "js/min/dateformat.min.js",
75+
InputPath: pathPrefix + "js/all_public.js",
76+
OutputPath: pathPrefix + "js/min/all_public.min.js",
7777
Type: "text/javascript",
78-
Name: "Dateformat JS",
79-
})
80-
result = append(result, converter{
81-
InputPath: pathPrefix + "js/uuid.js",
82-
OutputPath: pathPrefix + "js/min/uuid.min.js",
83-
Type: "text/javascript",
84-
Name: "UUID JS",
78+
Name: "Public functions JS",
8579
})
8680
return result
8781
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func (p DatabaseProvider) Upgrade(currentDbVersion int) {
6060
"maxSize" INTEGER NOT NULL,
6161
"creation" INTEGER NOT NULL,
6262
"apiKey" TEXT NOT NULL UNIQUE,
63+
"note" TEXT NOT NULL,
6364
PRIMARY KEY("id")
6465
);
6566
CREATE TABLE "Presign" (
@@ -227,6 +228,7 @@ func (p DatabaseProvider) createNewDatabase() error {
227228
"maxSize" INTEGER NOT NULL,
228229
"creation" INTEGER NOT NULL,
229230
"apiKey" TEXT NOT NULL UNIQUE,
231+
"note" TEXT NOT NULL,
230232
PRIMARY KEY("id")
231233
);
232234
CREATE TABLE "Presign" (

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ type schemaFileRequests struct {
1717
MaxSize int
1818
Creation int64
1919
ApiKey string
20+
Note string
2021
}
2122

2223
// GetFileRequest returns the FileRequest or false if not found
2324
func (p DatabaseProvider) GetFileRequest(id string) (models.FileRequest, bool) {
2425
var rowResult schemaFileRequests
2526
row := p.sqliteDb.QueryRow("SELECT * FROM UploadRequests WHERE Id = ?", id)
2627
err := row.Scan(&rowResult.Id, &rowResult.Name, &rowResult.UserId, &rowResult.Expiry,
27-
&rowResult.MaxFiles, &rowResult.MaxSize, &rowResult.Creation, &rowResult.ApiKey)
28+
&rowResult.MaxFiles, &rowResult.MaxSize, &rowResult.Creation, &rowResult.ApiKey, &rowResult.Note)
2829
if err != nil {
2930
if errors.Is(err, sql.ErrNoRows) {
3031
return models.FileRequest{}, false
@@ -41,6 +42,7 @@ func (p DatabaseProvider) GetFileRequest(id string) (models.FileRequest, bool) {
4142
Expiry: rowResult.Expiry,
4243
CreationDate: rowResult.Creation,
4344
ApiKey: rowResult.ApiKey,
45+
Notes: rowResult.Note,
4446
}
4547
return result, true
4648
}
@@ -54,7 +56,7 @@ func (p DatabaseProvider) GetAllFileRequests() []models.FileRequest {
5456
for rows.Next() {
5557
rowData := schemaFileRequests{}
5658
err = rows.Scan(&rowData.Id, &rowData.Name, &rowData.UserId, &rowData.Expiry, &rowData.MaxFiles,
57-
&rowData.MaxSize, &rowData.Creation, &rowData.ApiKey)
59+
&rowData.MaxSize, &rowData.Creation, &rowData.ApiKey, &rowData.Note)
5860
helper.Check(err)
5961
result = append(result, models.FileRequest{
6062
Id: rowData.Id,
@@ -65,6 +67,7 @@ func (p DatabaseProvider) GetAllFileRequests() []models.FileRequest {
6567
Expiry: rowData.Expiry,
6668
CreationDate: rowData.Creation,
6769
ApiKey: rowData.ApiKey,
70+
Notes: rowData.Note,
6871
})
6972
}
7073
return result
@@ -81,10 +84,13 @@ func (p DatabaseProvider) SaveFileRequest(request models.FileRequest) {
8184
Expiry: request.Expiry,
8285
Creation: request.CreationDate,
8386
ApiKey: request.ApiKey,
87+
Note: request.Notes,
8488
}
8589

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)
90+
_, err := p.sqliteDb.Exec(`INSERT OR REPLACE INTO UploadRequests
91+
(id, name, userid, expiry, maxFiles, maxSize, creation, apiKey, note)
92+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
93+
newData.Id, newData.Name, newData.UserId, newData.Expiry, newData.MaxFiles, newData.MaxSize, newData.Creation, newData.ApiKey, newData.Note)
8894
helper.Check(err)
8995
}
9096

internal/models/FileRequest.go

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ import (
88
)
99

1010
type FileRequest struct {
11-
Id string `json:"id" redis:"id"` // The internal ID of the file request
12-
UserId int `json:"userid" redis:"userid"` // The user ID of the owner
13-
MaxFiles int `json:"maxfiles" redis:"maxfiles"` // The maximum number of files allowed
14-
MaxSize int `json:"maxsize" redis:"maxsize"` // The maximum file size allowed in MB
15-
Expiry int64 `json:"expiry" redis:"expiry"` // The expiry time of the file request
16-
CreationDate int64 `json:"creationdate" redis:"creationdate"` // The timestamp of the file request creation
17-
Name string `json:"name" redis:"name"` // The given name for the file request
18-
ApiKey string `json:"apikey" redis:"apikey"` // The API key related to the file request
19-
UploadedFiles int `json:"uploadedfiles" redis:"-"` // Contains the number of uploaded files for this request. Needs to be calculated with Populate()
20-
LastUpload int64 `json:"lastupload" redis:"-"` // Contains the timestamp of the last upload for this request. Needs to be calculated with Populate()
21-
TotalFileSize int64 `json:"totalfilesize" redis:"-"` // Contains the file size of all uploaded files. Needs to be calculated with Populate()
22-
FileIdList []string `json:"fileidlist" redis:"-"` // Contains an array of the IDs of all uploaded files. Needs to be calculated with Populate()
23-
Files []File `json:"-" redis:"-"` // Contains an array of the IDs of all uploaded files. Needs to be calculated with Populate()
11+
Id string `json:"id" redis:"id"` // The internal ID of the file request
12+
UserId int `json:"userid" redis:"userid"` // The user ID of the owner
13+
MaxFiles int `json:"maxfiles" redis:"maxfiles"` // The maximum number of files allowed
14+
MaxSize int `json:"maxsize" redis:"maxsize"` // The maximum file size allowed in MB
15+
Expiry int64 `json:"expiry" redis:"expiry"` // The expiry time of the file request
16+
CreationDate int64 `json:"creationdate" redis:"creationdate"` // The timestamp of the file request creation
17+
Name string `json:"name" redis:"name"` // The given name for the file request
18+
ApiKey string `json:"apikey" redis:"apikey"` // The API key related to the file request
19+
Notes string `json:"notes" redis:"notes"` // The custom note that was set for this file request
20+
UploadedFiles int `json:"uploadedfiles" redis:"-"` // Contains the number of uploaded files for this request. Needs to be calculated with Populate()
21+
CombinedMaxSize int `json:"combinedmaxsize" redis:"-"` // The lesser of MaxSize and the server's max upload size. Needs to be calculated with Populate()
22+
LastUpload int64 `json:"lastupload" redis:"-"` // Contains the timestamp of the last upload for this request. Needs to be calculated with Populate()
23+
TotalFileSize int64 `json:"totalfilesize" redis:"-"` // Contains the file size of all uploaded files. Needs to be calculated with Populate()
24+
FileIdList []string `json:"fileidlist" redis:"-"` // Contains an array of the IDs of all uploaded files. Needs to be calculated with Populate()
25+
Files []File `json:"-" redis:"-"` // Contains an array of the IDs of all uploaded files. Needs to be calculated with Populate()
2426
}
2527

2628
// Populate inserts the number of uploaded files and the last upload date
27-
func (f *FileRequest) Populate(files map[string]File) {
29+
func (f *FileRequest) Populate(files map[string]File, maxServerSize int) {
2830
f.FileIdList = make([]string, 0)
2931
f.Files = make([]File, 0)
3032
for _, file := range files {
@@ -38,6 +40,10 @@ func (f *FileRequest) Populate(files map[string]File) {
3840
}
3941
}
4042
}
43+
f.CombinedMaxSize = f.MaxSize
44+
if f.MaxSize == 0 || f.MaxSize > maxServerSize {
45+
f.CombinedMaxSize = maxServerSize
46+
}
4147
}
4248

4349
// GetReadableDateLastUpdate returns the last update date as YYYY-MM-DD HH:MM:SS
@@ -67,3 +73,19 @@ func (f *FileRequest) IsUnlimitedFiles() bool {
6773
func (f *FileRequest) IsUnlimitedTime() bool {
6874
return f.Expiry == 0
6975
}
76+
77+
func (f *FileRequest) IsExpired() bool {
78+
return !f.IsUnlimitedTime() && time.Now().Unix() > f.Expiry
79+
}
80+
81+
func (f *FileRequest) HasRestrictions() bool {
82+
return !(f.IsUnlimitedSize() && f.IsUnlimitedFiles() && f.IsUnlimitedTime())
83+
}
84+
85+
func (f *FileRequest) FilesRemaining() int {
86+
result := f.MaxFiles - f.UploadedFiles
87+
if result < 0 {
88+
return 0
89+
}
90+
return result
91+
}

internal/storage/filerequest/Filerequest.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package filerequest
33
import (
44
"time"
55

6+
"github.com/forceu/gokapi/internal/configuration"
67
"github.com/forceu/gokapi/internal/configuration/database"
78
"github.com/forceu/gokapi/internal/helper"
89
"github.com/forceu/gokapi/internal/models"
@@ -25,7 +26,7 @@ func Get(id string) (models.FileRequest, bool) {
2526
if !ok {
2627
return models.FileRequest{}, false
2728
}
28-
result.Populate(database.GetAllMetadata())
29+
result.Populate(database.GetAllMetadata(), configuration.Get().MaxFileSizeMB)
2930
return result, true
3031
}
3132

@@ -35,8 +36,9 @@ func GetAll() []models.FileRequest {
3536
return result
3637
}
3738
allFiles := database.GetAllMetadata()
39+
maxServerSize := configuration.Get().MaxFileSizeMB
3840
for i, request := range result {
39-
request.Populate(allFiles)
41+
request.Populate(allFiles, maxServerSize)
4042
result[i] = request
4143
}
4244
return result

internal/webserver/Webserver.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -916,11 +916,11 @@ func showPublicUpload(w http.ResponseWriter, r *http.Request) {
916916

917917
config := configuration.Get()
918918

919-
view := filerequestView{
919+
view := publicUploadView{
920920
PublicName: config.PublicName,
921-
ApiKey: apiKey,
922-
FileRequestId: fileRequestId,
923-
ChunkSize: configuration.Get().ChunkSize,
921+
ChunkSize: config.ChunkSize,
922+
MaxServerSize: config.MaxFileSizeMB,
923+
FileRequest: &request,
924924
CustomContent: customStaticInfo,
925925
}
926926

@@ -1111,13 +1111,13 @@ type oauthErrorView struct {
11111111
CustomContent customStatic
11121112
}
11131113

1114-
// A view containing parameters for a generic template
1115-
type filerequestView struct {
1114+
// A view containing parameters for the public upload page
1115+
type publicUploadView struct {
11161116
IsAdminView bool
11171117
IsDownloadView bool
11181118
PublicName string
1119-
ApiKey string
1120-
FileRequestId string
11211119
ChunkSize int
1120+
MaxServerSize int
11221121
CustomContent customStatic
1122+
FileRequest *models.FileRequest
11231123
}

internal/webserver/api/Api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,9 @@ func apiURequestSave(w http.ResponseWriter, r requestParser, user models.User) {
976976
if request.IsMaxSizeSet {
977977
uploadRequest.MaxSize = request.MaxSize
978978
}
979+
if request.IsNotesSet {
980+
uploadRequest.Notes = request.Notes
981+
}
979982
database.SaveFileRequest(uploadRequest)
980983
uploadRequest, ok = filerequest.Get(uploadRequest.Id)
981984
if isNewRequest {

internal/webserver/api/routing.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,13 +681,15 @@ func (p *paramURequestDelete) ProcessParameter(_ *http.Request) error {
681681
type paramURequestSave struct {
682682
Id string `header:"id"`
683683
Name string `header:"name" supportBase64:"true"`
684+
Notes string `header:"notes" supportBase64:"true"`
684685
Expiry int64 `header:"expiry"`
685686
MaxFiles int `header:"maxfiles"`
686687
MaxSize int `header:"maxsize"`
687688
IsNameSet bool
688689
IsExpirySet bool
689690
IsMaxFilesSet bool
690691
IsMaxSizeSet bool
692+
IsNotesSet bool
691693

692694
foundHeaders map[string]bool
693695
}
@@ -705,6 +707,9 @@ func (p *paramURequestSave) ProcessParameter(_ *http.Request) error {
705707
if p.foundHeaders["maxsize"] {
706708
p.IsMaxSizeSet = true
707709
}
710+
if p.foundHeaders["notes"] {
711+
p.IsNotesSet = true
712+
}
708713
return nil
709714
}
710715

internal/webserver/api/routingParsing.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/webserver/web/static/apidocumentation/openapi.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,17 @@
13911391
"type": "string"
13921392
}
13931393
},
1394+
{
1395+
"name": "notes",
1396+
"in": "header",
1397+
"description": "The public notes for the request. If the notes includes non-ANSI characters, you can encode them with base64, by adding 'base64:' at the beginning, e.g. 'base64:ZmlsZW5hbWU='",
1398+
"required": false,
1399+
"style": "simple",
1400+
"explode": false,
1401+
"schema": {
1402+
"type": "string"
1403+
}
1404+
},
13941405
{
13951406
"name": "expiry",
13961407
"in": "header",
@@ -1936,7 +1947,7 @@
19361947
"id": {
19371948
"type": "string",
19381949
"description": "The internal ID of the file request",
1939-
"example": "137"
1950+
"example": "caep3Ooquu6phoo"
19401951
},
19411952
"userid": {
19421953
"type": "integer",
@@ -1973,6 +1984,11 @@
19731984
"description": "The given name for the file request",
19741985
"example": "Book list entries"
19751986
},
1987+
"notes": {
1988+
"type": "string",
1989+
"description": "The public notes for the file request",
1990+
"example": "Please make sure to upload revision 1 files"
1991+
},
19761992
"apikey": {
19771993
"type": "string",
19781994
"description": "The API key that is used for uploading files for this request",

0 commit comments

Comments
 (0)