浏览代码

goification3

Drn 2 年之前
父节点
当前提交
b93abc65b9
共有 4 个文件被更改,包括 46 次插入49 次删除
  1. 10 11
      commands.go
  2. 0 24
      database.go
  3. 26 3
      downloads.go
  4. 10 11
      handlers.go

+ 10 - 11
commands.go

@@ -539,17 +539,16 @@ func handleCommands() *exrouter.Route {
 									message.ChannelID = ctx.Msg.ChannelID
 									url := "https://cdn.discordapp.com/emojis/" + emoji.ID
 
-									status, _ := handleDownload(
-										downloadRequestStruct{
-											InputURL:   url,
-											Filename:   emoji.ID,
-											Path:       destination,
-											Message:    &message,
-											FileTime:   time.Now(),
-											HistoryCmd: false,
-											EmojiCmd:   true,
-											StartTime:  time.Now(),
-										})
+									status, _ := downloadRequestStruct{
+										InputURL:   url,
+										Filename:   emoji.ID,
+										Path:       destination,
+										Message:    &message,
+										FileTime:   time.Now(),
+										HistoryCmd: false,
+										EmojiCmd:   true,
+										StartTime:  time.Now(),
+									}.handleDownload()
 
 									if status.Status == downloadSuccess {
 										i++

+ 0 - 24
database.go

@@ -7,33 +7,9 @@ import (
 	"time"
 
 	"github.com/HouzuoGuo/tiedot/db"
-	"github.com/bwmarrin/discordgo"
 	"github.com/fatih/color"
 )
 
-// Trim files already downloaded and stored in database
-func trimDownloadedLinks(linkList map[string]string, m *discordgo.Message) map[string]string {
-	channelConfig := getSource(m)
-
-	newList := make(map[string]string, 0)
-	for link, filename := range linkList {
-		downloadedFiles := dbFindDownloadByURL(link)
-		alreadyDownloaded := false
-		for _, downloadedFile := range downloadedFiles {
-			if downloadedFile.ChannelID == m.ChannelID {
-				alreadyDownloaded = true
-			}
-		}
-
-		if !alreadyDownloaded || *channelConfig.SavePossibleDuplicates {
-			newList[link] = filename
-		} else if config.Debug {
-			log.Println(lg("Download", "SKIP", color.GreenString, "Found URL has already been downloaded for this channel: %s", link))
-		}
-	}
-	return newList
-}
-
 func dbInsertDownload(download *downloadItem) error {
 	_, err := myDB.Use("Downloads").Insert(map[string]interface{}{
 		"URL":         download.URL,

+ 26 - 3
downloads.go

@@ -144,6 +144,29 @@ func trimDuplicateLinks(fileItems []*fileItem) []*fileItem {
 	return result
 }
 
+// Trim files already downloaded and stored in database
+func trimDownloadedLinks(linkList map[string]string, m *discordgo.Message) map[string]string {
+	channelConfig := getSource(m)
+
+	newList := make(map[string]string, 0)
+	for link, filename := range linkList {
+		downloadedFiles := dbFindDownloadByURL(link)
+		alreadyDownloaded := false
+		for _, downloadedFile := range downloadedFiles {
+			if downloadedFile.ChannelID == m.ChannelID {
+				alreadyDownloaded = true
+			}
+		}
+
+		if !alreadyDownloaded || *channelConfig.SavePossibleDuplicates {
+			newList[link] = filename
+		} else if config.Debug {
+			log.Println(lg("Download", "SKIP", color.GreenString, "Found URL has already been downloaded for this channel: %s", link))
+		}
+	}
+	return newList
+}
+
 func getRawLinks(m *discordgo.Message) []*fileItem {
 	var links []*fileItem
 
@@ -405,11 +428,11 @@ type downloadRequestStruct struct {
 	StartTime      time.Time
 }
 
-func handleDownload(download downloadRequestStruct) (downloadStatusStruct, int64) {
+func (download downloadRequestStruct) handleDownload() (downloadStatusStruct, int64) {
 	status := mDownloadStatus(downloadFailed)
 	var tempfilesize int64 = -1
 	for i := 0; i < config.DownloadRetryMax; i++ {
-		status, tempfilesize = tryDownload(download)
+		status, tempfilesize = download.tryDownload()
 		if status.Status < downloadFailed || status.Status == downloadFailedCode404 { // Success or Skip
 			break
 		} else {
@@ -578,7 +601,7 @@ func handleDownload(download downloadRequestStruct) (downloadStatusStruct, int64
 	return status, tempfilesize
 }
 
-func tryDownload(download downloadRequestStruct) (downloadStatusStruct, int64) {
+func (download downloadRequestStruct) tryDownload() (downloadStatusStruct, int64) {
 	var err error
 
 	cachedDownloadID++

+ 10 - 11
handlers.go

@@ -316,17 +316,16 @@ func handleMessage(m *discordgo.Message, edited bool, history bool) (int64, int6
 			if config.Debug && !history {
 				log.Println(lg("Debug", "Message", color.CyanString, "FOUND FILE: "+file.Link))
 			}
-			status, filesize := handleDownload(
-				downloadRequestStruct{
-					InputURL:   file.Link,
-					Filename:   file.Filename,
-					Path:       channelConfig.Destination,
-					Message:    m,
-					FileTime:   file.Time,
-					HistoryCmd: history,
-					EmojiCmd:   false,
-					StartTime:  time.Now(),
-				})
+			status, filesize := downloadRequestStruct{
+				InputURL:   file.Link,
+				Filename:   file.Filename,
+				Path:       channelConfig.Destination,
+				Message:    m,
+				FileTime:   file.Time,
+				HistoryCmd: history,
+				EmojiCmd:   false,
+				StartTime:  time.Now(),
+			}.handleDownload()
 			if status.Status == downloadSuccess {
 				downloadCount++
 				totalfilesize += filesize