diff --git a/services/api/console.go b/services/api/console.go index 1afc0534..9d294908 100644 --- a/services/api/console.go +++ b/services/api/console.go @@ -18,6 +18,8 @@ package main import ( "database/sql" "encoding/json" + "fmt" + "strings" cmn "github.com/pzaino/thecrowler/pkg/common" cdb "github.com/pzaino/thecrowler/pkg/database" @@ -71,6 +73,30 @@ func addSource(sqlQuery string, params addSourceRequest) (ConsoleResponse, error var results ConsoleResponse results.Message = "Failed to add the source" + // Check if Config is empty and set to default JSON if it is + if params.Config == "" || strings.ToLower(params.Config) == "null" { + defaultConfig := map[string]string{} + defaultConfigJSON, err := json.Marshal(defaultConfig) + if err != nil { + return results, fmt.Errorf("Failed to marshal default Config: %w", err) + } + params.Config = string(defaultConfigJSON) + } else { + fmt.Println("params.Config:", params.Config) + // Validate and potentially reformat the existing Config JSON + var jsonRaw map[string]interface{} + if err := json.Unmarshal([]byte(params.Config), &jsonRaw); err != nil { + // Handle invalid JSON + return results, fmt.Errorf("Config field contains invalid JSON: %w", err) + } + // Re-marshal to ensure the JSON is in a standardized format (optional) + configJSON, err := json.Marshal(jsonRaw) + if err != nil { + return results, fmt.Errorf("Failed to marshal Config field: %w", err) + } + params.Config = string(configJSON) + } + // Initialize the database handler db, err := cdb.NewHandler(config) if err != nil { @@ -86,7 +112,7 @@ func addSource(sqlQuery string, params addSourceRequest) (ConsoleResponse, error defer db.Close() // Execute the SQL statement - _, err = db.Exec(sqlQuery, params) + _, err = db.Exec(sqlQuery, params.URL, params.Status, params.Restricted, params.Disabled, params.Flags, params.Config) if err != nil { return results, err } diff --git a/services/api/main.go b/services/api/main.go index 9fb77d51..4c6e516b 100644 --- a/services/api/main.go +++ b/services/api/main.go @@ -139,6 +139,7 @@ func scrImgSrchHandler(w http.ResponseWriter, r *http.Request) { } results, err := performScreenshotSearch(query, getQType(r.Method != "POST")) + handleErrorAndRespond(w, err, results, "Error performing screenshot search: %v", http.StatusInternalServerError) } diff --git a/services/api/search_engine.go b/services/api/search_engine.go index 78c8a8aa..dedb5ec6 100644 --- a/services/api/search_engine.go +++ b/services/api/search_engine.go @@ -426,9 +426,9 @@ func parseScreenshotGetQuery(input string) (string, []interface{}, error) { s.height, s.byte_size FROM - Screenshots s + Screenshots AS s JOIN - SearchIndex si ON s.index_id = si.index_id + SearchIndex AS si ON s.index_id = si.index_id LEFT JOIN KeywordIndex ki ON si.index_id = ki.index_id LEFT JOIN @@ -457,7 +457,7 @@ func parseScreenshotQuery(input string) (string, []interface{}, error) { // Extract the query from the request if len(req.URL) > 0 { - query = req.URL + query = "%" + req.URL + "%" } else { return "", nil, errors.New("no query provided") } @@ -474,11 +474,11 @@ func parseScreenshotQuery(input string) (string, []interface{}, error) { s.height, s.byte_size FROM - Screenshots s + Screenshots AS s JOIN - SearchIndex si ON s.index_id = si.index_id + SearchIndex AS si ON s.index_id = si.index_id WHERE - si.page_url = $1; + LOWER(si.page_url) LIKE LOWER($1); ` sqlParams = append(sqlParams, query) @@ -616,7 +616,7 @@ func parseNetInfoQuery(input string) (string, []interface{}, error) { JOIN SearchIndex si ON nii.index_id = si.index_id WHERE - si.page_url = $1; + si.page_url LIKE $1; ` sqlParams = append(sqlParams, query)