Skip to content

Commit

Permalink
Source validation
Browse files Browse the repository at this point in the history
  • Loading branch information
get-got committed Jan 25, 2021
1 parent e8a4361 commit fe53f14
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
downloadSkippedDetectedDuplicate downloadStatus = iota

downloadFailed downloadStatus = iota
downloadFailedInvalidSource downloadStatus = iota
downloadFailedCreatingFolder downloadStatus = iota
downloadFailedRequesting downloadStatus = iota
downloadFailedDownloadingResponse downloadStatus = iota
Expand Down Expand Up @@ -90,6 +91,8 @@ func getDownloadStatusString(status downloadStatus) string {
//
case downloadFailed:
return "Download Failed"
case downloadFailedInvalidSource:
return "Download Failed - Invalid Source"
case downloadFailedCreatingFolder:
return "Download Failed - Error Creating Folder"
case downloadFailedRequesting:
Expand Down Expand Up @@ -440,13 +443,21 @@ func tryDownload(inputURL string, filename string, path string, message *discord
if isChannelRegistered(message.ChannelID) {
channelConfig := getChannelConfig(message.ChannelID)

var err error

// Source validation
_, err = url.ParseRequestURI(inputURL)
if err != nil {
return mDownloadStatus(downloadFailedInvalidSource, err)
}

// Clean/fix path
if !strings.HasSuffix(path, string(os.PathSeparator)) {
path = path + string(os.PathSeparator)
}

// Create folder
err := os.MkdirAll(path, 0755)
err = os.MkdirAll(path, 0755)
if err != nil {
log.Println(logPrefixErrorHere, color.HiRedString("Error while creating destination folder \"%s\": %s", path, err))
return mDownloadStatus(downloadFailedCreatingFolder, err)
Expand Down

0 comments on commit fe53f14

Please sign in to comment.