Skip to content

Commit

Permalink
debug utils
Browse files Browse the repository at this point in the history
  • Loading branch information
sayem314 committed Sep 27, 2024
1 parent 58ccede commit 6ac94d8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
9 changes: 9 additions & 0 deletions utils/filename.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@ package utils
import (
"regexp"
"strings"

"github.com/d-fi/GoFi/logger"
)

// SanitizeFileName replaces characters that are not allowed in file names.
func SanitizeFileName(name string) string {
logger.Debug("Sanitizing file name: %s", name)

// Define a regex to match invalid characters
reg := regexp.MustCompile(`[<>:"/\\|?*\x00-\x1F]`)

// Replace invalid characters with underscores
safeName := reg.ReplaceAllString(name, "_")
logger.Debug("Replaced invalid characters in file name: %s", safeName)

// Trim trailing periods and spaces which are also problematic
safeName = strings.TrimRight(safeName, ". ")
logger.Debug("Trimmed trailing periods and spaces: %s", safeName)

return safeName
}
14 changes: 12 additions & 2 deletions utils/query.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package utils

import "fmt"
import (
"fmt"

"github.com/d-fi/GoFi/logger"
)

// ConvertToQueryParams converts map[string]interface{} to map[string]string
func ConvertToQueryParams(params map[string]interface{}) map[string]string {
logger.Debug("Converting parameters to query params: %v", params)

queryParams := make(map[string]string)
for key, value := range params {
queryParams[key] = fmt.Sprintf("%v", value)
convertedValue := fmt.Sprintf("%v", value)
queryParams[key] = convertedValue
logger.Debug("Converted key: %s, value: %s", key, convertedValue)
}

logger.Debug("Converted query params: %v", queryParams)
return queryParams
}
11 changes: 10 additions & 1 deletion utils/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@ import (
"errors"
"fmt"
"net/http"

"github.com/d-fi/GoFi/logger"
)

// CheckURLFileSize performs a HEAD request to check the availability of a URL
// and returns the content length if available.
func CheckURLFileSize(url string) (int, error) {
logger.Debug("Checking URL file size for: %s", url)

resp, err := http.Head(url)
if err != nil {
logger.Debug("Error during HEAD request: %v", err)
return 0, err
}
defer resp.Body.Close()

if contentLength := resp.Header.Get("Content-Length"); contentLength != "" {
contentLength := resp.Header.Get("Content-Length")
if contentLength != "" {
var size int
fmt.Sscanf(contentLength, "%d", &size)
logger.Debug("Determined file size: %d bytes", size)
return size, nil
}

logger.Debug("Unable to determine file size for URL: %s", url)
return 0, errors.New("unable to determine file size")
}

0 comments on commit 6ac94d8

Please sign in to comment.