Skip to content

Commit

Permalink
Merge pull request #110 from pzaino/develop
Browse files Browse the repository at this point in the history
Some fixes for the console feature
  • Loading branch information
pzaino authored Feb 19, 2024
2 parents 344b6ae + 3842f89 commit 41e1ba6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
28 changes: 27 additions & 1 deletion services/api/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions services/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
14 changes: 7 additions & 7 deletions services/api/search_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 41e1ba6

Please sign in to comment.