diff --git a/src/media/fetch.go b/src/media/fetch.go index fa71d67..7b6ad56 100644 --- a/src/media/fetch.go +++ b/src/media/fetch.go @@ -8,6 +8,7 @@ import ( "html/template" "net/http" "path/filepath" + "regexp" "strings" ) @@ -24,8 +25,6 @@ import ( "sync" ) -const downloadDir = "downloads/" - type Media struct { Id string Name string @@ -41,6 +40,10 @@ type MediaResults struct { var fetchResponseTmpl = template.Must(template.ParseFiles("templates/media/response.html")) var fetchIndexTmpl = template.Must(template.ParseFiles("templates/media/index.html")) +// Where the media files are saved. Always has a trailing slash +var downloadDir = getDownloadDir() +var idCharSet = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString + func Index(w http.ResponseWriter, _ *http.Request) { if err := fetchIndexTmpl.Execute(w, nil); err != nil { log.Error().Msgf("Error rendering template: %v", err) @@ -213,6 +216,17 @@ func GetMD5Hash(url string) string { } func isValidId(id string) bool { - // TODO: Finish this - return true + // TODO: Finish this. Should only be alpha numeric + return idCharSet(id) +} + +func getDownloadDir() string { + dir := os.Getenv("DOWNLOAD_DIR") + if dir != "" { + if !strings.HasSuffix(dir, "/") { + return dir + "/" + } + return dir + } + return "downloads/" }