diff --git a/README.md b/README.md index 3932779..586f2ca 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Feature-rich HTTP File Server - [x] SHA1 checksum - [x] Command line cheatsheet (curl, wget, PS) - [ ] Hot reload - - [ ] Relative last modified + - [x] Relative timestamp - [ ] Regex filtering - Functionality - [ ] Send message to backend diff --git a/internal/utils/time.go b/internal/utils/time.go new file mode 100644 index 0000000..94d9b47 --- /dev/null +++ b/internal/utils/time.go @@ -0,0 +1,85 @@ +package utils + +// original project : https://github.com/andanhm/go-prettytime + +import ( + "strconv" + "strings" + "time" +) + +// Unix epoch (or Unix time or POSIX time or Unix timestamp) 1 year (365.24 days) +const infinity float64 = 31556926 * 1000 + +// Handler function which determines the time difference based on defined time spams +func handler(timeIntervalThreshold float64, timePeriod, message string) func(float64) string { + return func(difference float64) string { + var str strings.Builder + n := difference / timeIntervalThreshold + nStr := strconv.FormatFloat(n, 'f', 0, 64) + str.WriteString(nStr) + str.WriteString(" ") + str.WriteString(timePeriod) + if int(n) > 1 { + str.WriteString("s ") + str.WriteString(message) + return str.String() + } + str.WriteString(" ") + str.WriteString(message) + return str.String() + } +} + +// timeLapse condition struct +type timeLapse struct { + // Time stamp threshold to handle the time lap condition + Threshold float64 + // Handler function which determines the time lapse based on the condition + Handler func(float64) string +} + +var timeLapses = []timeLapse{ + {Threshold: -31535999, Handler: handler(-31536000, "year", "from now")}, + {Threshold: -2591999, Handler: handler(-2592000, "month", "from now")}, + {Threshold: -604799, Handler: handler(-604800, "week", "from now")}, + {Threshold: -172799, Handler: handler(-86400, "day", "from now")}, + {Threshold: -86399, Handler: func(diff float64) string { + return "tomorrow" + }}, + {Threshold: -3599, Handler: handler(-3600, "hour", "from now")}, + {Threshold: -59, Handler: handler(-60, "minute", "from now")}, + {Threshold: -0.9999, Handler: handler(-1, "second", "from now")}, + {Threshold: 1, Handler: func(diff float64) string { + return "just now" + }}, + {Threshold: 60, Handler: handler(1, "second", "ago")}, + {Threshold: 3600, Handler: handler(60, "minute", "ago")}, + {Threshold: 86400, Handler: handler(3600, "hour", "ago")}, + {Threshold: 172800, Handler: func(diff float64) string { + return "yesterday" + }}, + {Threshold: 604800, Handler: handler(86400, "day", "ago")}, + {Threshold: 2592000, Handler: handler(604800, "week", "ago")}, + {Threshold: 31536000, Handler: handler(2592000, "month", "ago")}, + {Threshold: infinity, Handler: handler(31536000, "year", "ago")}, +} + +//Format returns a string describing how long it has been since the time argument passed int +func RelativeTimeDiff(t time.Time) (timeSince string) { + timestamp := t.Unix() + now := time.Now().Unix() + + if timestamp > now || timestamp <= 0 { + timeSince = "" + } + + timeElapsed := float64(now - timestamp) + for _, formatter := range timeLapses { + if timeElapsed < formatter.Threshold { + timeSince = formatter.Handler(timeElapsed) + break + } + } + return timeSince +} \ No newline at end of file diff --git a/makefile b/makefile index 7ef958f..17382ac 100644 --- a/makefile +++ b/makefile @@ -7,6 +7,7 @@ test: go run cmd/gohfs/main.go compress: - upx releases/gohfs-linux-amd64 - upx releases/gohfs-macos-amd64 - upx releases/gohfs-amd64.exe \ No newline at end of file + upx --ultra-brute releases/gohfs-linux-amd64 + upx --ultra-brute releases/gohfs-macos-amd64 + cp releases/gohfs-amd64.exe releases/gohfs-amd64-packed.exe + upx --ultra-brute releases/gohfs-amd64-packed.exe \ No newline at end of file diff --git a/web/static/css/style.css b/web/static/css/style.css index a6358af..43254bb 100644 --- a/web/static/css/style.css +++ b/web/static/css/style.css @@ -80,7 +80,7 @@ a:hover { .footer { width: 100%; - padding-top: 5px; + padding-top: 15px; text-align: center; color: #107d8e; font-weight: bold; @@ -230,7 +230,7 @@ td { /* UPLOAD MODAL */ .upload { - width: fit-content; + display: table; margin: auto; } @@ -250,7 +250,7 @@ td { .qrcode { margin: 0 auto; - width: fit-content; + display: table; } .qrcaption { diff --git a/web/web.go b/web/web.go index 3ada12a..a345d41 100644 --- a/web/web.go +++ b/web/web.go @@ -43,7 +43,7 @@ func Embed(cfg *config.Config){ func ParseItem(info fs.FileInfo) Item { tmp := Item{ Name: url.PathEscape(info.Name()), - ModTime: info.ModTime().Format(time.RFC1123), + ModTime: info.ModTime().Format(time.RFC822) + " (" + utils.RelativeTimeDiff(info.ModTime()) + ")", RawModTime: info.ModTime().Format(time.RFC3339), }