Skip to content

Commit

Permalink
implemented file/folder response validation
Browse files Browse the repository at this point in the history
  • Loading branch information
beebeeoii committed Jan 21, 2022
1 parent 48ccb64 commit 3ab8222
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion FyneApp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Website = "https://github.com/beebeeoii/lominus"
Name = "Lominus"
ID = "com.beebeeoii.lominus"
Version = "1.2.0"
Build = 108
Build = 113
39 changes: 39 additions & 0 deletions pkg/api/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/beebeeoii/lominus/internal/file"
logs "github.com/beebeeoii/lominus/internal/log"
)

// Folder struct is the datapack for containing details about a Folder
Expand Down Expand Up @@ -37,6 +38,18 @@ const FOLDER_URL_ENDPOINT = "https://luminus.nus.edu.sg/v2/api/files/?populate=t
const FILE_URL_ENDPOINT = "https://luminus.nus.edu.sg/v2/api/files/%s/file?populate=Creator,lastUpdatedUser,comment"
const DOWNLOAD_URL_ENDPOINT = "https://luminus.nus.edu.sg/v2/api/files/file/%s/downloadurl"

// getFolderFieldsRequired is a helper function that returns a constant array with fields that a Folder response
// returned by Luminus needs
func getFolderFieldsRequired() []string {
return []string{"access", "id", "name", "isActive", "allowUpload", "subFolderCount"}
}

// getFileFieldsRequired is a helper function that returns a constant array with fields that a File response
// returned by Luminus needs
func getFileFieldsRequired() []string {
return []string{"id", "name", "lastUpdatedDate"}
}

// GetAllFolders returns a slice of Folder objects from a DocumentRequest.
// It will only return folders in the current folder.
// Nested folders will not be returned.
Expand All @@ -55,6 +68,10 @@ func (req DocumentRequest) GetAllFolders() ([]Folder, error) {
}

for _, content := range rawResponse.Data {
if !isResponseValid(getFolderFieldsRequired(), content) {
continue
}

if _, exists := content["access"]; exists { // only folder that can be accessed will be placed in folders slice
newFolder := Folder{
Id: content["id"].(string),
Expand Down Expand Up @@ -87,6 +104,10 @@ func (req DocumentRequest) GetAllFiles() ([]File, error) {
}

for _, content := range rawResponse.Data {
if !isResponseValid(getFileFieldsRequired(), content) {
continue
}

lastUpdated, timeParseErr := time.Parse(time.RFC3339, content["lastUpdatedDate"].(string))

if timeParseErr != nil {
Expand Down Expand Up @@ -194,3 +215,21 @@ func (req DocumentRequest) Download(filePath string) error {

return nil
}

// isResponseValid is a helper function that checks if a Folder/File response is valid.
// It checks if the response contains the required fields required.
func isResponseValid(fieldsRequired []string, response map[string]interface{}) bool {
isValid := true
for _, field := range fieldsRequired {
_, exists := response[field]

if !exists {
isValid = false
break
}
}

logs.Logger.Debugln(isValid, fieldsRequired)

return isValid
}

1 comment on commit 3ab8222

@Beebeeoii
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes #25

Please sign in to comment.