123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191 |
- package main
- import (
- "bytes"
- "fmt"
- "io/fs"
- "io/ioutil"
- "log"
- "math/rand"
- "mime"
- "net/http"
- "net/url"
- "os"
- "path/filepath"
- "strconv"
- "strings"
- "time"
- "github.com/bwmarrin/discordgo"
- "github.com/dustin/go-humanize"
- "github.com/fatih/color"
- "github.com/hako/durafmt"
- "mvdan.cc/xurls/v2"
- )
- type downloadItem struct {
- URL string
- Time time.Time
- Destination string
- Filename string
- ChannelID string
- UserID string
- }
- type downloadStatus int
- const (
- downloadSuccess downloadStatus = iota
- downloadIgnored
- downloadSkipped
- downloadSkippedDuplicate
- downloadSkippedUnpermittedDomain
- downloadSkippedUnpermittedType
- downloadSkippedUnpermittedExtension
- downloadSkippedDetectedDuplicate
- downloadFailed
- downloadFailedCode
- downloadFailedCode404
- downloadFailedInvalidSource
- downloadFailedInvalidPath
- downloadFailedCreatingFolder
- downloadFailedRequesting
- downloadFailedDownloadingResponse
- downloadFailedReadResponse
- downloadFailedCreatingSubfolder
- downloadFailedWritingFile
- downloadFailedWritingDatabase
- )
- type downloadStatusStruct struct {
- Status downloadStatus
- Error error
- }
- func mDownloadStatus(status downloadStatus, _error ...error) downloadStatusStruct {
- if len(_error) == 0 {
- return downloadStatusStruct{
- Status: status,
- Error: nil,
- }
- }
- return downloadStatusStruct{
- Status: status,
- Error: _error[0],
- }
- }
- func getDownloadStatusString(status downloadStatus) string {
- switch status {
- case downloadSuccess:
- return "Download Succeeded"
- //
- case downloadIgnored:
- return "Download Ignored"
- //
- case downloadSkipped:
- return "Download Skipped"
- case downloadSkippedDuplicate:
- return "Download Skipped - Duplicate"
- case downloadSkippedUnpermittedDomain:
- return "Download Skipped - Unpermitted Domain"
- case downloadSkippedUnpermittedType:
- return "Download Skipped - Unpermitted File Type"
- case downloadSkippedUnpermittedExtension:
- return "Download Skipped - Unpermitted File Extension"
- case downloadSkippedDetectedDuplicate:
- return "Download Skipped - Detected Duplicate"
- //
- case downloadFailed:
- return "Download Failed"
- case downloadFailedCode:
- return "Download Failed - BAD CONNECTION"
- case downloadFailedCode404:
- return "Download Failed - 404 NOT FOUND"
- case downloadFailedInvalidSource:
- return "Download Failed - Invalid Source"
- case downloadFailedInvalidPath:
- return "Download Failed - Invalid Path"
- case downloadFailedCreatingFolder:
- return "Download Failed - Error Creating Folder"
- case downloadFailedRequesting:
- return "Download Failed - Error Requesting URL Data"
- case downloadFailedDownloadingResponse:
- return "Download Failed - Error Downloading URL Response"
- case downloadFailedReadResponse:
- return "Download Failed - Error Reading URL Response"
- case downloadFailedCreatingSubfolder:
- return "Download Failed - Error Creating Subfolder for Type"
- case downloadFailedWritingFile:
- return "Download Failed - Error Writing File"
- case downloadFailedWritingDatabase:
- return "Download Failed - Error Writing to Database"
- }
- return "Unknown Error"
- }
- // Trim duplicate links in link list
- func trimDuplicateLinks(fileItems []*fileItem) []*fileItem {
- var result []*fileItem
- seen := map[string]bool{}
- for _, item := range fileItems {
- if seen[item.Link] {
- continue
- }
- seen[item.Link] = true
- result = append(result, item)
- }
- 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
- if m.Author == nil {
- m.Author = new(discordgo.User)
- }
- for _, attachment := range m.Attachments {
- links = append(links, &fileItem{
- Link: attachment.URL,
- Filename: attachment.Filename,
- })
- }
- foundLinks := xurls.Strict().FindAllString(m.Content, -1)
- for _, foundLink := range foundLinks {
- links = append(links, &fileItem{
- Link: foundLink,
- })
- }
- for _, embed := range m.Embeds {
- if embed.URL != "" {
- links = append(links, &fileItem{
- Link: embed.URL,
- })
- }
- // Removing for now as this causes it to try and pull shit from things like YouTube descriptions
- /*if embed.Description != "" {
- foundLinks = xurls.Strict().FindAllString(embed.Description, -1)
- for _, foundLink := range foundLinks {
- links = append(links, &fileItem{
- Link: foundLink,
- })
- }
- }*/
- if embed.Image != nil && embed.Image.URL != "" {
- links = append(links, &fileItem{
- Link: embed.Image.URL,
- })
- }
- if embed.Video != nil && embed.Video.URL != "" {
- links = append(links, &fileItem{
- Link: embed.Video.URL,
- })
- }
- }
- return links
- }
- func getDownloadLinks(inputURL string, m *discordgo.Message) map[string]string {
- /* TODO: Download Support...
- - TikTok: Tried, once the connection is closed the cdn URL is rendered invalid
- - Facebook Photos: Tried, it doesn't preload image data, it's loaded in after. Would have to keep connection open, find alternative way to grab, or use api.
- - Facebook Videos: Previously supported but they split mp4 into separate audio and video streams
- */
- inputURL = strings.ReplaceAll(inputURL, "mobile.twitter", "twitter")
- inputURL = strings.ReplaceAll(inputURL, "fxtwitter.com", "twitter.com")
- inputURL = strings.ReplaceAll(inputURL, "c.vxtwitter.com", "twitter.com")
- inputURL = strings.ReplaceAll(inputURL, "vxtwitter.com", "twitter.com")
- if regexUrlTwitter.MatchString(inputURL) {
- links, err := getTwitterUrls(inputURL)
- if err != nil {
- if !strings.Contains(err.Error(), "suspended") {
- log.Println(lg("Download", "", color.RedString, "Twitter Media fetch failed for %s -- %s", inputURL, err))
- }
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if regexUrlTwitterStatus.MatchString(inputURL) {
- links, err := getTwitterStatusUrls(inputURL, m)
- if err != nil {
- if !strings.Contains(err.Error(), "suspended") && !strings.Contains(err.Error(), "No status found") {
- log.Println(lg("Download", "", color.RedString, "Twitter Status fetch failed for %s -- %s", inputURL, err))
- }
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if regexUrlInstagram.MatchString(inputURL) {
- links, err := getInstagramUrls(inputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Instagram fetch failed for %s -- %s", inputURL, err))
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if regexUrlImgurSingle.MatchString(inputURL) {
- links, err := getImgurSingleUrls(inputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Imgur Media fetch failed for %s -- %s", inputURL, err))
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if regexUrlImgurAlbum.MatchString(inputURL) {
- links, err := getImgurAlbumUrls(inputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Imgur Album fetch failed for %s -- %s", inputURL, err))
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if regexUrlStreamable.MatchString(inputURL) {
- links, err := getStreamableUrls(inputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Streamable fetch failed for %s -- %s", inputURL, err))
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if regexUrlGfycat.MatchString(inputURL) {
- links, err := getGfycatUrls(inputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Gfycat fetch failed for %s -- %s", inputURL, err))
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if regexUrlFlickrPhoto.MatchString(inputURL) {
- links, err := getFlickrPhotoUrls(inputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Flickr Photo fetch failed for %s -- %s", inputURL, err))
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if regexUrlFlickrAlbum.MatchString(inputURL) {
- links, err := getFlickrAlbumUrls(inputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Flickr Album fetch failed for %s -- %s", inputURL, err))
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if regexUrlFlickrAlbumShort.MatchString(inputURL) {
- links, err := getFlickrAlbumShortUrls(inputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Flickr Album (short) fetch failed for %s -- %s", inputURL, err))
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if regexUrlTistory.MatchString(inputURL) {
- links, err := getTistoryUrls(inputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Tistory URL failed for %s -- %s", inputURL, err))
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if regexUrlTistoryLegacy.MatchString(inputURL) {
- links, err := getLegacyTistoryUrls(inputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Legacy Tistory URL failed for %s -- %s", inputURL, err))
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if regexUrlRedditPost.MatchString(inputURL) {
- links, err := getRedditPostUrls(inputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Reddit Post URL failed for %s -- %s", inputURL, err))
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if regexUrlMastodonPost1.MatchString(inputURL) || regexUrlMastodonPost2.MatchString(inputURL) {
- links, err := getMastodonPostUrls(inputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Mastodon Post URL failed for %s -- %s", inputURL, err))
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- // The original project has this as an option,
- if regexUrlPossibleTistorySite.MatchString(inputURL) {
- links, err := getPossibleTistorySiteUrls(inputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Checking for Tistory site failed for %s -- %s", inputURL, err))
- } else if len(links) > 0 {
- return trimDownloadedLinks(links, m)
- }
- }
- if strings.HasPrefix(inputURL, "https://cdn.discordapp.com/emojis/") {
- return nil
- }
- // Try without queries
- parsedURL, err := url.Parse(inputURL)
- if err == nil {
- if strings.Contains(parsedURL.String(), "format=") {
- parsedURL.RawQuery = "format=" + parsedURL.Query().Get("format")
- } else {
- parsedURL.RawQuery = ""
- }
- inputURLWithoutQueries := parsedURL.String()
- if inputURLWithoutQueries != inputURL {
- return trimDownloadedLinks(getDownloadLinks(inputURLWithoutQueries, m), m)
- }
- }
- return trimDownloadedLinks(map[string]string{inputURL: ""}, m)
- }
- func getFileLinks(m *discordgo.Message) []*fileItem {
- var fileItems []*fileItem
- linkTime := m.Timestamp
- rawLinks := getRawLinks(m)
- for _, rawLink := range rawLinks {
- downloadLinks := getDownloadLinks(rawLink.Link, m)
- for link, filename := range downloadLinks {
- if rawLink.Filename != "" {
- filename = rawLink.Filename
- }
- fileItems = append(fileItems, &fileItem{
- Link: link,
- Filename: filename,
- Time: linkTime,
- })
- }
- }
- fileItems = trimDuplicateLinks(fileItems)
- return fileItems
- }
- type downloadRequestStruct struct {
- InputURL string
- Filename string
- FileExtension string
- Path string
- Message *discordgo.Message
- FileTime time.Time
- HistoryCmd bool
- EmojiCmd bool
- ManualDownload bool
- StartTime time.Time
- }
- func (download downloadRequestStruct) handleDownload() (downloadStatusStruct, int64) {
- status := mDownloadStatus(downloadFailed)
- var tempfilesize int64 = -1
- for i := 0; i < config.DownloadRetryMax; i++ {
- status, tempfilesize = download.tryDownload()
- if status.Status < downloadFailed || status.Status == downloadFailedCode404 { // Success or Skip
- break
- } else {
- time.Sleep(5 * time.Second)
- }
- }
- // Any kind of failure
- if status.Status >= downloadFailed && !download.HistoryCmd && !download.EmojiCmd {
- log.Println(lg("Download", "", color.RedString,
- "Gave up on downloading %s after %d failed attempts...\t%s",
- download.InputURL, config.DownloadRetryMax, getDownloadStatusString(status.Status)))
- if channelConfig := getSource(download.Message); channelConfig != emptyConfig {
- if !download.HistoryCmd && *channelConfig.SendErrorMessages {
- content := fmt.Sprintf(
- "Gave up trying to download\n<%s>\nafter %d failed attempts...\n\n``%s``",
- download.InputURL, config.DownloadRetryMax, getDownloadStatusString(status.Status))
- if status.Error != nil {
- content += fmt.Sprintf("\n```ERROR: %s```", status.Error)
- }
- // Failure Notice
- if !hasPerms(download.Message.ChannelID, discordgo.PermissionSendMessages) {
- log.Println(lg("Download", "", color.HiRedString, fmtBotSendPerm, download.Message.ChannelID))
- } else {
- if selfbot {
- _, err := bot.ChannelMessageSend(download.Message.ChannelID,
- fmt.Sprintf("%s **Download Failure**\n\n%s", download.Message.Author.Mention(), content))
- if err != nil {
- log.Println(lg("Download", "", color.HiRedString,
- "Failed to send failure message to %s: %s", download.Message.ChannelID, err))
- }
- } else {
- if _, err := bot.ChannelMessageSendComplex(download.Message.ChannelID,
- &discordgo.MessageSend{
- Content: fmt.Sprintf("<@!%s>", download.Message.Author.ID),
- Embed: buildEmbed(download.Message.ChannelID, "Download Failure", content),
- }); err != nil {
- log.Println(lg("Download", "", color.HiRedString,
- "Failed to send failure message to %s: %s",
- download.Message.ChannelID, err))
- }
- }
- }
- }
- if status.Error != nil {
- sendErrorMessage(fmt.Sprintf("**%s**\n\n%s", getDownloadStatusString(status.Status), status.Error))
- }
- }
- }
- // Log Links to File
- if channelConfig := getSource(download.Message); channelConfig != emptyConfig {
- if channelConfig.LogLinks != nil {
- if channelConfig.LogLinks.Destination != "" {
- logPath := channelConfig.LogLinks.Destination
- if *channelConfig.LogLinks.DestinationIsFolder {
- if !strings.HasSuffix(logPath, string(os.PathSeparator)) {
- logPath += string(os.PathSeparator)
- }
- err := os.MkdirAll(logPath, 0755)
- if err == nil {
- logPath += "Log_Links"
- if *channelConfig.LogLinks.DivideLogsByServer {
- if download.Message.GuildID == "" {
- ch, err := bot.State.Channel(download.Message.ChannelID)
- if err == nil {
- if ch.Type == discordgo.ChannelTypeDM {
- logPath += " DM"
- } else if ch.Type == discordgo.ChannelTypeGroupDM {
- logPath += " GroupDM"
- } else {
- logPath += " Unknown"
- }
- } else {
- logPath += " Unknown"
- }
- } else {
- logPath += " SID_" + download.Message.GuildID
- }
- }
- if *channelConfig.LogLinks.DivideLogsByChannel {
- logPath += " CID_" + download.Message.ChannelID
- }
- if *channelConfig.LogLinks.DivideLogsByUser {
- logPath += " UID_" + download.Message.Author.ID
- }
- if *channelConfig.LogLinks.DivideLogsByStatus {
- if status.Status >= downloadFailed {
- logPath += " - FAILED"
- } else if status.Status >= downloadSkipped {
- logPath += " - SKIPPED"
- } else if status.Status == downloadIgnored {
- logPath += " - IGNORED"
- } else if status.Status == downloadSuccess {
- logPath += " - DOWNLOADED"
- }
- }
- }
- logPath += ".txt"
- }
- // Read
- currentLog, err := ioutil.ReadFile(logPath)
- currentLogS := ""
- if err == nil {
- currentLogS = string(currentLog)
- }
- // Writer
- f, err := os.OpenFile(logPath, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0600)
- if err != nil {
- log.Println(lg("Download", "", color.RedString,
- "[channelConfig.LogLinks] Failed to open log file:\t%s", err))
- f.Close()
- }
- defer f.Close()
- var newLine string
- shouldLog := true
- // Log Failures
- if status.Status > downloadSuccess {
- shouldLog = *channelConfig.LogLinks.LogFailures // will not log if LogFailures is false
- } else if *channelConfig.LogLinks.LogDownloads { // Log Downloads
- shouldLog = true
- }
- // Filter Duplicates
- if channelConfig.LogLinks.FilterDuplicates != nil {
- if *channelConfig.LogLinks.FilterDuplicates {
- if strings.Contains(currentLogS, download.InputURL) {
- shouldLog = false
- }
- }
- }
- if shouldLog {
- // Prepend
- prefix := ""
- if channelConfig.LogLinks.Prefix != nil {
- prefix = *channelConfig.LogLinks.Prefix
- }
- // More Data
- additionalInfo := ""
- if channelConfig.LogLinks.UserData != nil {
- if *channelConfig.LogLinks.UserData {
- additionalInfo = fmt.Sprintf("[%s/%s] \"%s\"#%s (%s) @ %s: ",
- download.Message.GuildID, download.Message.ChannelID,
- download.Message.Author.Username, download.Message.Author.Discriminator,
- download.Message.Author.ID, download.Message.Timestamp)
- }
- }
- // Append
- suffix := ""
- if channelConfig.LogLinks.Suffix != nil {
- suffix = *channelConfig.LogLinks.Suffix
- }
- // New Line
- newLine += "\n" + prefix + additionalInfo + download.InputURL + suffix
- if _, err = f.WriteString(newLine); err != nil {
- log.Println(lg("Download", "", color.RedString,
- "[channelConfig.LogLinks] Failed to append file:\t%s", err))
- }
- }
- }
- }
- }
- return status, tempfilesize
- }
- func (download downloadRequestStruct) tryDownload() (downloadStatusStruct, int64) {
- var err error
- cachedDownloadID++
- logPrefix := ""
- if download.HistoryCmd {
- logPrefix = "HISTORY "
- }
- var fileinfo fs.FileInfo
- var channelConfig configurationSource
- sourceDefault(&channelConfig)
- _channelConfig := getSource(download.Message)
- if _channelConfig != emptyConfig {
- channelConfig = _channelConfig
- }
- if _channelConfig != emptyConfig || download.EmojiCmd || download.ManualDownload {
- // Source validation
- if _, err = url.ParseRequestURI(download.InputURL); err != nil {
- return mDownloadStatus(downloadFailedInvalidSource, err), 0
- }
- // Clean/fix path
- if download.Path == "" || download.Path == string(os.PathSeparator) {
- log.Println(lg("Download", "", color.HiRedString, "Destination cannot be empty path..."))
- return mDownloadStatus(downloadFailedInvalidPath, err), 0
- }
- if !strings.HasSuffix(download.Path, string(os.PathSeparator)) {
- download.Path = download.Path + string(os.PathSeparator)
- }
- // Create folder
- if err = os.MkdirAll(download.Path, 0755); err != nil {
- log.Println(lg("Download", "", color.HiRedString,
- "Error while creating destination folder \"%s\": %s",
- download.Path, err))
- return mDownloadStatus(downloadFailedCreatingFolder, err), 0
- }
- // Request
- timeout := time.Duration(time.Duration(config.DownloadTimeout) * time.Second)
- client := &http.Client{
- Timeout: timeout,
- }
- request, err := http.NewRequest("GET", download.InputURL, nil)
- request.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36")
- if err != nil {
- log.Println(lg("Download", "", color.HiRedString, "Error while requesting \"%s\": %s", download.InputURL, err))
- return mDownloadStatus(downloadFailedRequesting, err), 0
- }
- request.Header.Add("Accept-Encoding", "identity")
- response, err := client.Do(request)
- if err != nil {
- if !strings.Contains(err.Error(), "no such host") && !strings.Contains(err.Error(), "connection refused") {
- log.Println(lg("Download", "", color.HiRedString,
- "Error while receiving response from \"%s\": %s",
- download.InputURL, err))
- }
- return mDownloadStatus(downloadFailedDownloadingResponse, err), 0
- }
- defer response.Body.Close()
- // Read
- bodyOfResp, err := ioutil.ReadAll(response.Body)
- if err != nil {
- log.Println(lg("Download", "", color.HiRedString,
- "Could not read response from \"%s\": %s",
- download.InputURL, err))
- return mDownloadStatus(downloadFailedReadResponse, err), 0
- }
- // Errors
- if response.StatusCode >= 400 {
- log.Println(lg("Download", "", color.HiRedString, logPrefix+"DOWNLOAD FAILED, %d %s: %s",
- response.StatusCode, http.StatusText(response.StatusCode), download.InputURL))
- if response.StatusCode == 404 {
- return mDownloadStatus(downloadFailedCode404, err), 0
- } else {
- return mDownloadStatus(downloadFailedCode, err), 0
- }
- }
- // Filename
- if download.Filename == "" {
- download.Filename = filenameFromURL(response.Request.URL.String())
- for key, iHeader := range response.Header {
- if key == "Content-Disposition" {
- if _, params, err := mime.ParseMediaType(iHeader[0]); err == nil {
- newFilename, err := url.QueryUnescape(params["filename"])
- if err != nil {
- newFilename = params["filename"]
- }
- if newFilename != "" {
- download.Filename = newFilename
- }
- }
- }
- }
- }
- if len(download.Filename) >= 260 {
- download.Filename = download.Filename[:255]
- }
- extension := strings.ToLower(filepath.Ext(download.Filename))
- download.FileExtension = extension
- contentType := http.DetectContentType(bodyOfResp)
- contentTypeParts := strings.Split(contentType, "/")
- contentTypeFound := contentTypeParts[0]
- parsedURL, err := url.Parse(download.InputURL)
- if err != nil {
- log.Println(lg("Download", "", color.RedString, "Error while parsing url:\t%s", err))
- }
- domain := parsedURL.Hostname()
- // Check extension
- if channelConfig.Filters.AllowedExtensions != nil || channelConfig.Filters.BlockedExtensions != nil {
- shouldAbort := false
- if channelConfig.Filters.AllowedExtensions != nil {
- shouldAbort = true
- }
- if channelConfig.Filters.BlockedExtensions != nil {
- if stringInSlice(extension, *channelConfig.Filters.BlockedExtensions) {
- shouldAbort = true
- }
- }
- if channelConfig.Filters.AllowedExtensions != nil {
- if stringInSlice(extension, *channelConfig.Filters.AllowedExtensions) {
- shouldAbort = false
- }
- }
- // Abort
- if shouldAbort {
- if !download.HistoryCmd {
- log.Println(lg("Download", "Skip", color.GreenString, "Unpermitted extension (%s) found at %s", extension, download.InputURL))
- }
- return mDownloadStatus(downloadSkippedUnpermittedExtension), 0
- }
- }
- // Fix content type
- if stringInSlice(extension, []string{".mov"}) ||
- stringInSlice(extension, []string{".mp4"}) ||
- stringInSlice(extension, []string{".webm"}) {
- contentTypeFound = "video"
- } else if stringInSlice(extension, []string{".psd"}) ||
- stringInSlice(extension, []string{".nef"}) ||
- stringInSlice(extension, []string{".dng"}) ||
- stringInSlice(extension, []string{".tif"}) ||
- stringInSlice(extension, []string{".tiff"}) {
- contentTypeFound = "image"
- }
- // Filename extension fix
- if filepath.Ext(download.Filename) == "" {
- if possibleExtension, _ := mime.ExtensionsByType(contentType); len(possibleExtension) > 0 {
- download.Filename += possibleExtension[0]
- download.FileExtension = possibleExtension[0]
- }
- }
- // Check Domain
- if channelConfig.Filters.AllowedDomains != nil || channelConfig.Filters.BlockedDomains != nil {
- shouldAbort := false
- if channelConfig.Filters.AllowedDomains != nil {
- shouldAbort = true
- }
- if channelConfig.Filters.BlockedDomains != nil {
- if stringInSlice(domain, *channelConfig.Filters.BlockedDomains) {
- shouldAbort = true
- }
- }
- if channelConfig.Filters.AllowedDomains != nil {
- if stringInSlice(domain, *channelConfig.Filters.AllowedDomains) {
- shouldAbort = false
- }
- }
- // Abort
- if shouldAbort {
- if !download.HistoryCmd {
- log.Println(lg("Download", "Skip", color.GreenString,
- "Unpermitted domain (%s) found at %s", domain, download.InputURL))
- }
- return mDownloadStatus(downloadSkippedUnpermittedDomain), 0
- }
- }
- // Check content type
- if !((*channelConfig.SaveImages && contentTypeFound == "image") ||
- (*channelConfig.SaveVideos && contentTypeFound == "video") ||
- (*channelConfig.SaveAudioFiles && contentTypeFound == "audio") ||
- (*channelConfig.SaveTextFiles && contentTypeFound == "text") ||
- (*channelConfig.SaveOtherFiles && contentTypeFound == "application")) {
- if !download.HistoryCmd {
- log.Println(lg("Download", "Skip", color.GreenString,
- "Unpermitted filetype (%s) found at %s", contentTypeFound, download.InputURL))
- }
- return mDownloadStatus(downloadSkippedUnpermittedType), 0
- }
- // Names
- sourceChannelName := download.Message.ChannelID
- sourceName := "UNKNOWN"
- sourceChannel, _ := bot.State.Channel(download.Message.ChannelID)
- if sourceChannel != nil {
- // Channel Naming
- if sourceChannel.Name != "" {
- sourceChannelName = "#" + sourceChannel.Name
- }
- switch sourceChannel.Type {
- case discordgo.ChannelTypeGuildText:
- // Server Naming
- if sourceChannel.GuildID != "" {
- sourceGuild, _ := bot.State.Guild(sourceChannel.GuildID)
- if sourceGuild != nil && sourceGuild.Name != "" {
- sourceName = sourceGuild.Name
- }
- }
- // Category Naming
- if sourceChannel.ParentID != "" {
- sourceParent, _ := bot.State.Channel(sourceChannel.ParentID)
- if sourceParent != nil {
- if sourceParent.Name != "" {
- sourceChannelName = sourceParent.Name + " / " + sourceChannelName
- }
- }
- }
- case discordgo.ChannelTypeDM:
- sourceName = "Direct Messages"
- case discordgo.ChannelTypeGroupDM:
- sourceName = "Group Messages"
- }
- }
- subfolder := ""
- // Subfolder Division - Year Nesting
- if *channelConfig.DivideByYear {
- year := fmt.Sprint(time.Now().Year())
- if download.Message.Author != nil {
- year = fmt.Sprint(download.Message.Timestamp.Year())
- }
- subfolderSuffix := year + string(os.PathSeparator)
- subfolder = subfolder + subfolderSuffix
- // Create folder
- if err := os.MkdirAll(download.Path+subfolder, 0755); err != nil {
- log.Println(lg("Download", "", color.HiRedString,
- "Error while creating server subfolder \"%s\": %s", download.Path, err))
- return mDownloadStatus(downloadFailedCreatingSubfolder, err), 0
- }
- }
- // Subfolder Division - Month Nesting
- if *channelConfig.DivideByMonth {
- year := fmt.Sprintf("%02d", time.Now().Month())
- if download.Message.Author != nil {
- year = fmt.Sprintf("%02d", download.Message.Timestamp.Month())
- }
- subfolderSuffix := year + string(os.PathSeparator)
- subfolder = subfolder + subfolderSuffix
- // Create folder
- if err := os.MkdirAll(download.Path+subfolder, 0755); err != nil {
- log.Println(lg("Download", "", color.HiRedString,
- "Error while creating server subfolder \"%s\": %s", download.Path, err))
- return mDownloadStatus(downloadFailedCreatingSubfolder, err), 0
- }
- }
- // Subfolder Division - Server Nesting
- if *channelConfig.DivideByServer {
- subfolderSuffix := download.Message.GuildID
- if !*channelConfig.DivideFoldersUseID && sourceName != "" && sourceName != "UNKNOWN" {
- subfolderSuffix = clearPath(sourceName)
- }
- if subfolderSuffix != "" {
- subfolderSuffix = subfolderSuffix + string(os.PathSeparator)
- subfolder = subfolder + subfolderSuffix
- // Create folder
- if err := os.MkdirAll(download.Path+subfolder, 0755); err != nil {
- log.Println(lg("Download", "", color.HiRedString,
- "Error while creating server subfolder \"%s\": %s", download.Path, err))
- return mDownloadStatus(downloadFailedCreatingSubfolder, err), 0
- }
- }
- }
- // Subfolder Division - Channel Nesting
- if *channelConfig.DivideByChannel {
- subfolderSuffix := download.Message.ChannelID
- if !*channelConfig.DivideFoldersUseID && sourceChannelName != "" {
- subfolderSuffix = clearPath(sourceChannelName)
- }
- if subfolderSuffix != "" {
- subfolder = subfolder + subfolderSuffix + string(os.PathSeparator)
- // Create folder
- if err := os.MkdirAll(download.Path+subfolder, 0755); err != nil {
- log.Println(lg("Download", "", color.HiRedString,
- "Error while creating channel subfolder \"%s\": %s", download.Path, err))
- return mDownloadStatus(downloadFailedCreatingSubfolder, err), 0
- }
- }
- }
- // Subfolder Division - User Nesting
- if *channelConfig.DivideByUser && download.Message.Author != nil {
- subfolderSuffix := download.Message.Author.ID
- if !*channelConfig.DivideFoldersUseID && download.Message.Author.Username != "" {
- subfolderSuffix = clearPath(download.Message.Author.Username + "#" +
- download.Message.Author.Discriminator)
- }
- if subfolderSuffix != "" {
- subfolder = subfolder + subfolderSuffix + string(os.PathSeparator)
- // Create folder
- if err := os.MkdirAll(download.Path+subfolder, 0755); err != nil {
- log.Println(lg("Download", "", color.HiRedString,
- "Error while creating user subfolder \"%s\": %s", download.Path, err))
- return mDownloadStatus(downloadFailedCreatingSubfolder, err), 0
- }
- }
- }
- // Subfolder Division - Content Type
- if *channelConfig.DivideByType {
- subfolderSuffix := contentTypeFound
- switch contentTypeFound {
- case "image":
- subfolderSuffix = "images"
- case "video":
- subfolderSuffix = "videos"
- case "application":
- subfolderSuffix = "applications"
- }
- if subfolderSuffix != "" {
- subfolder = subfolder + subfolderSuffix + string(os.PathSeparator)
- // Create folder.
- if err := os.MkdirAll(download.Path+subfolder, 0755); err != nil {
- log.Println(lg("Download", "", color.HiRedString,
- "Error while creating type subfolder \"%s\": %s", download.Path+subfolder, err))
- return mDownloadStatus(downloadFailedCreatingSubfolder, err), 0
- }
- }
- }
- // Format Filename
- filename := dynamicKeyReplacement(channelConfig, download)
- download.Path = download.Path + subfolder
- download.Filename = filename
- completePath := filepath.Clean(download.Path + download.Filename)
- // Check if filepath exists
- if _, err := os.Stat(completePath); err == nil {
- if *channelConfig.SavePossibleDuplicates {
- tmpPath := completePath
- i := 1
- for {
- // Append number to name
- completePath = tmpPath[0:len(tmpPath)-len(filepathExtension(tmpPath))] +
- "-" + strconv.Itoa(i) + filepathExtension(tmpPath)
- if _, err := os.Stat(completePath); os.IsNotExist(err) {
- break
- }
- i = i + 1
- }
- if !download.HistoryCmd {
- log.Println(lg("Download", "Skip", color.GreenString,
- "Matching filenames, possible duplicate? Saving \"%s\" as \"%s\" instead",
- tmpPath, completePath))
- }
- } else {
- if !download.HistoryCmd {
- log.Println(lg("Download", "Skip", color.GreenString,
- "Matching filenames, possible duplicate..."))
- }
- return mDownloadStatus(downloadSkippedDuplicate), 0
- }
- }
- // Write
- if *channelConfig.Save {
- if err = ioutil.WriteFile(completePath, bodyOfResp, 0644); err != nil {
- log.Println(lg("Download", "", color.HiRedString,
- "Error while writing file to disk \"%s\": %s", download.InputURL, err))
- return mDownloadStatus(downloadFailedWritingFile, err), 0
- }
- // Change file time
- if err = os.Chtimes(completePath, download.FileTime, download.FileTime); err != nil {
- log.Println(lg("Download", "", color.RedString,
- logPrefix+"Error while changing metadata date \"%s\": %s", download.InputURL, err))
- }
- filesize := "unknown"
- speed := 0.0
- speedlabel := "kB/s"
- fileinfo, err = os.Stat(completePath)
- if err == nil {
- filesize = humanize.Bytes(uint64(fileinfo.Size()))
- speed = float64(fileinfo.Size() / humanize.KByte)
- if fileinfo.Size() >= humanize.MByte {
- speed = float64(fileinfo.Size() / humanize.MByte)
- speedlabel = "MB/s"
- }
- }
- dlColor := color.HiGreenString
- msgTimestamp := ""
- if download.HistoryCmd {
- dlColor = color.HiCyanString
- msgTimestamp = "on " + download.Message.Timestamp.Format("2006/01/02 @ 15:04:05") + " "
- }
- log.Println(lg("Download", "", dlColor,
- logPrefix+"SAVED %s sent %sin %s\n\t\t\t\t\t\t%s",
- strings.ToUpper(contentTypeFound), msgTimestamp,
- color.HiYellowString("\"%s / %s\" (%s)", sourceName, sourceChannelName, download.Message.ChannelID),
- color.GreenString("> %s to \"%s%s\"\t\t%s", domain, download.Path, download.Filename,
- color.WhiteString("(%s, %s, %0.1f %s)",
- filesize, durafmt.ParseShort(time.Since(download.StartTime)).String(), speed/time.Since(download.StartTime).Seconds(), speedlabel))))
- } else {
- log.Println(lg("Download", "", color.GreenString,
- logPrefix+"Did not save %s sent in %s#%s --- file saving disabled...",
- contentTypeFound, sourceName, sourceChannelName))
- }
- userID := botUser.ID
- if download.Message.Author != nil {
- userID = download.Message.Author.ID
- }
- // Store in db
- err = dbInsertDownload(&downloadItem{
- URL: download.InputURL,
- Time: time.Now(),
- Destination: completePath,
- Filename: download.Filename,
- ChannelID: download.Message.ChannelID,
- UserID: userID,
- })
- if err != nil {
- log.Println(lg("Download", "", color.HiRedString, "Error writing to database: %s", err))
- return mDownloadStatus(downloadFailedWritingDatabase, err), 0
- }
- // React
- {
- shouldReact := config.ReactWhenDownloaded
- if channelConfig.ReactWhenDownloaded != nil {
- shouldReact = *channelConfig.ReactWhenDownloaded
- }
- if download.HistoryCmd {
- if !config.ReactWhenDownloadedHistory {
- shouldReact = false
- }
- if channelConfig.ReactWhenDownloadedHistory != nil {
- if *channelConfig.ReactWhenDownloadedHistory {
- shouldReact = true
- }
- }
- }
- if download.Message.Author != nil && shouldReact {
- reaction := ""
- if channelConfig.ReactWhenDownloadedEmoji == nil {
- if download.Message.GuildID != "" {
- guild, err := bot.State.Guild(download.Message.GuildID)
- if err != nil {
- log.Println(lg("Download", "", color.RedString,
- "Error fetching guild state for emojis from %s: %s",
- download.Message.GuildID, err))
- } else {
- emojis := guild.Emojis
- if len(emojis) > 1 {
- for {
- rand.Seed(time.Now().UnixNano())
- chosenEmoji := emojis[rand.Intn(len(emojis))]
- formattedEmoji := chosenEmoji.APIName()
- if !chosenEmoji.Animated && !stringInSlice(formattedEmoji,
- *channelConfig.BlacklistReactEmojis) {
- reaction = formattedEmoji
- break
- }
- }
- } else {
- reaction = defaultReact
- }
- }
- } else {
- reaction = defaultReact
- }
- } else {
- reaction = *channelConfig.ReactWhenDownloadedEmoji
- }
- // Add Reaction
- if hasPerms(download.Message.ChannelID, discordgo.PermissionAddReactions) {
- if err = bot.MessageReactionAdd(download.Message.ChannelID, download.Message.ID, reaction); err != nil {
- log.Println(lg("Download", "", color.RedString,
- "Error adding reaction to message: %s", err))
- }
- } else {
- log.Println(lg("Download", "", color.RedString,
- "Bot does not have permission to add reactions in %s", download.Message.ChannelID))
- }
- }
- }
- // Log Media To Channel(s)
- {
- var logMediaChannels []string
- if channelConfig.SendFileToChannel != nil {
- if *channelConfig.SendFileToChannel != "" {
- logMediaChannels = append(logMediaChannels, *channelConfig.SendFileToChannel)
- }
- }
- if channelConfig.SendFileToChannels != nil {
- logMediaChannels = append(logMediaChannels, *channelConfig.SendFileToChannels...)
- }
- for _, logChannel := range logMediaChannels {
- if logChannel != "" {
- if hasPerms(logChannel, discordgo.PermissionSendMessages) {
- actualFile := false
- if channelConfig.SendFileDirectly != nil {
- actualFile = *channelConfig.SendFileDirectly
- }
- msg := ""
- if channelConfig.SendFileCaption != nil {
- msg = *channelConfig.SendFileCaption
- msg = channelKeyReplacement(msg, download.Message.ChannelID)
- }
- // File
- if actualFile {
- _, err := bot.ChannelMessageSendComplex(logChannel,
- &discordgo.MessageSend{
- Content: msg,
- File: &discordgo.File{Name: download.Filename, Reader: bytes.NewReader(bodyOfResp)},
- },
- )
- if err != nil {
- log.Println(lg("Download", "", color.HiRedString,
- "File log message failed to send:\t%s", err))
- }
- } else { // Embed
- embed := &discordgo.MessageEmbed{
- Title: fmt.Sprintf("Downloaded: %s", download.Filename),
- Color: getEmbedColor(logChannel),
- Footer: &discordgo.MessageEmbedFooter{
- IconURL: projectIcon,
- Text: fmt.Sprintf("%s v%s", projectName, projectVersion),
- },
- }
- if contentTypeFound == "image" {
- embed.Image = &discordgo.MessageEmbedImage{URL: download.InputURL}
- } else if contentTypeFound == "video" {
- embed.Video = &discordgo.MessageEmbedVideo{URL: download.InputURL}
- } else {
- embed.Description = fmt.Sprintf("Unsupported filetype: %s\n%s",
- contentTypeFound, download.InputURL)
- }
- _, err := bot.ChannelMessageSendComplex(logChannel,
- &discordgo.MessageSend{
- Content: msg,
- Embed: embed,
- },
- )
- if err != nil {
- log.Println(lg("Download", "", color.HiRedString,
- "File log message failed to send:\t%s", err))
- }
- }
- }
- }
- }
- }
- // Update Presence
- if !download.HistoryCmd {
- timeLastUpdated = time.Now()
- if *channelConfig.PresenceEnabled {
- go updateDiscordPresence()
- }
- }
- timeLastDownload = time.Now()
- return mDownloadStatus(downloadSuccess), fileinfo.Size()
- }
- return mDownloadStatus(downloadIgnored), 0
- }
|