Drn 2 years ago
parent
commit
4ef3ca4ba6
5 changed files with 28 additions and 5 deletions
  1. 1 0
      README.md
  2. 9 0
      discord.go
  3. 15 5
      downloads.go
  4. 1 0
      go.mod
  5. 2 0
      go.sum

+ 1 - 0
README.md

@@ -1098,6 +1098,7 @@ Key                     | Description
 `{{date}}`              | Timestamp
 `{{file}}`              | Filename
 `{{fileType}}`          | File type
+`{{fileSize}}`          | File size short (43 MB, 24 kB, etc)
 `{{messageID}}`         | Source Message ID
 `{{userID}}`            | Source Author User ID
 `{{username}}`          | Source Author Username

+ 9 - 0
discord.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"log"
 	"net/url"
+	"os"
 	"runtime"
 	"strconv"
 	"strings"
@@ -12,6 +13,7 @@ import (
 	"github.com/AvraamMavridis/randomcolor"
 	"github.com/aidarkhanov/nanoid/v2"
 	"github.com/bwmarrin/discordgo"
+	"github.com/dustin/go-humanize"
 	"github.com/fatih/color"
 	"github.com/hako/durafmt"
 	"github.com/teris-io/shortid"
@@ -289,10 +291,17 @@ func dynamicKeyReplacement(channelConfig configurationSource, download downloadR
 			domain = parsedURL.Hostname()
 		}
 
+		fileinfo, err := os.Stat(download.Path + download.Filename)
+		filesize := "unknown"
+		if err == nil {
+			filesize = humanize.Bytes(uint64(fileinfo.Size()))
+		}
+
 		keys := [][]string{
 			{"{{date}}", messageTime.Format(filenameDateFormat)},
 			{"{{file}}", download.Filename},
 			{"{{fileType}}", download.FileExtension},
+			{"{{fileSize}}", filesize},
 			{"{{messageID}}", download.Message.ID},
 			{"{{userID}}", userID},
 			{"{{username}}", username},

+ 15 - 5
downloads.go

@@ -18,6 +18,7 @@ import (
 	"time"
 
 	"github.com/bwmarrin/discordgo"
+	"github.com/dustin/go-humanize"
 	"github.com/fatih/color"
 	"github.com/hako/durafmt"
 	"github.com/rivo/duplo"
@@ -964,7 +965,10 @@ func tryDownload(download downloadRequestStruct) downloadStatusStruct {
 		}
 
 		// Format Filename
-		completePath := filepath.Clean(download.Path + subfolder + dynamicKeyReplacement(channelConfig, download))
+		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 {
@@ -1008,6 +1012,12 @@ func tryDownload(download downloadRequestStruct) downloadStatusStruct {
 					logPrefix+"Error while changing metadata date \"%s\": %s", download.InputURL, err))
 			}
 
+			fileinfo, err := os.Stat(completePath)
+			filesize := "unknown"
+			if err == nil {
+				filesize = humanize.Bytes(uint64(fileinfo.Size()))
+			}
+
 			dlColor := color.HiGreenString
 			msgTimestamp := ""
 			if download.HistoryCmd {
@@ -1015,10 +1025,10 @@ func tryDownload(download downloadRequestStruct) downloadStatusStruct {
 				msgTimestamp = "on " + download.Message.Timestamp.Format("2006/01/02 @ 15:04:05") + " "
 			}
 			log.Println(lg("Download", "", dlColor,
-				logPrefix+"SAVED %s sent %sin \"%s / %s\" from \"%s\" to %s (%s)",
-				strings.ToUpper(contentTypeFound), msgTimestamp, sourceName, sourceChannelName,
-				condenseString(download.InputURL, 50), download.Path+subfolder,
-				durafmt.ParseShort(time.Since(download.StartTime)).String()))
+				logPrefix+"SAVED %s sent %sin %s\n\t\t\t\t\t%s",
+				strings.ToUpper(contentTypeFound), msgTimestamp, color.HiYellowString("\"%s / %s\" (%s)", sourceName, sourceChannelName, download.Message.ChannelID),
+				color.GreenString("from %s to \"%s%s\" (%s, %s, %0.1f MB/s)", parsedURL.Hostname(), download.Path, download.Filename,
+					durafmt.ParseShort(time.Since(download.StartTime)).String(), filesize, float64(fileinfo.Size()/1000000)/time.Since(download.StartTime).Seconds())))
 		} else {
 			log.Println(lg("Download", "", color.GreenString,
 				logPrefix+"Did not save %s sent in %s#%s --- file saving disabled...",

+ 1 - 0
go.mod

@@ -31,6 +31,7 @@ require (
 	github.com/ChimeraCoder/tokenbucket v0.0.0-20131201223612-c5a927568de7 // indirect
 	github.com/andybalholm/cascadia v1.1.0 // indirect
 	github.com/azr/backoff v0.0.0-20160115115103-53511d3c7330 // indirect
+	github.com/dustin/go-humanize v1.0.0 // indirect
 	github.com/dustin/go-jsonpointer v0.0.0-20160814072949-ba0abeacc3dc // indirect
 	github.com/dustin/gojson v0.0.0-20160307161227-2e71ec9dd5ad // indirect
 	github.com/garyburd/go-oauth v0.0.0-20180319155456-bca2e7f09a17 // indirect

+ 2 - 0
go.sum

@@ -82,6 +82,8 @@ github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403 h1:cqQfy1jclcSy/FwLje
 github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
 github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
+github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
 github.com/dustin/go-jsonpointer v0.0.0-20160814072949-ba0abeacc3dc h1:tP7tkU+vIsEOKiK+l/NSLN4uUtkyuxc6hgYpQeCWAeI=
 github.com/dustin/go-jsonpointer v0.0.0-20160814072949-ba0abeacc3dc/go.mod h1:ORH5Qp2bskd9NzSfKqAF7tKfONsEkCarTE5ESr/RVBw=
 github.com/dustin/gojson v0.0.0-20160307161227-2e71ec9dd5ad h1:Qk76DOWdOp+GlyDKBAG3Klr9cn7N+LcYc82AZ2S7+cA=