Skip to content

Commit 86933af

Browse files
authored
fix(public): add view query param (#5)
When embeding image in public preview, use view query param instead of download, to prevent download hit count to be incremented fix #1
1 parent d011208 commit 86933af

File tree

5 files changed

+29
-25
lines changed

5 files changed

+29
-25
lines changed

server/api/file.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"shortpaste/core/tools"
1212

1313
"github.com/go-chi/chi/v5"
14-
log "github.com/sirupsen/logrus"
14+
"github.com/sirupsen/logrus"
1515
"github.com/thanhpk/randstr"
1616
"gopkg.in/go-playground/validator.v9"
1717
)
@@ -56,9 +56,9 @@ func FileAdd(w http.ResponseWriter, r *http.Request) {
5656
file.Name = handler.Filename
5757
file.MIME = handler.Header["Content-Type"][0]
5858

59-
log.WithField("category", "file-upload").Info("Uploaded File: " + file.Name)
60-
log.WithField("category", "file-upload").Info("File Size: " + tools.IECFormat(handler.Size))
61-
log.WithField("category", "file-upload").Info("MIME Header: " + file.MIME)
59+
logrus.WithField("category", "file-upload").Info("Uploaded File: " + file.Name)
60+
logrus.WithField("category", "file-upload").Info("File Size: " + tools.IECFormat(handler.Size))
61+
logrus.WithField("category", "file-upload").Info("MIME Header: " + file.MIME)
6262

6363
filePath := path.Join(config.GetDataDirPath(), "files", file.ID, file.Name)
6464

server/core/database/database.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"path"
66
"shortpaste/core/config"
77

8-
log "github.com/sirupsen/logrus"
8+
"github.com/sirupsen/logrus"
99

1010
"github.com/glebarez/sqlite"
1111
"gorm.io/gorm"
@@ -20,15 +20,15 @@ func Init() {
2020

2121
func Get() *gorm.DB {
2222
if !isInited() {
23-
log.WithField("category", "database").Info("Initing...")
23+
logrus.WithField("category", "database").Info("Initing...")
2424

2525
dbUri := path.Join(config.GetDataDirPath(), "mapping.db")
2626
ndb, err := gorm.Open(sqlite.Open(dbUri), &gorm.Config{})
2727
if err != nil {
2828
panic(fmt.Errorf("db error %v", err))
2929
}
3030
if err != nil {
31-
log.Fatal(err)
31+
logrus.Fatal(err)
3232
}
3333
db = ndb
3434

@@ -43,7 +43,7 @@ func isInited() bool {
4343
}
4444

4545
func migrate() {
46-
log.WithField("category", "database").Info("Migrating...")
46+
logrus.WithField("category", "database").Info("Migrating...")
4747
db.AutoMigrate(&Link{})
4848
db.AutoMigrate(&File{})
4949
db.AutoMigrate(&Text{})

server/core/tools/placeholder/placeholder.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"shortpaste/core/tools/file"
88
"strings"
99

10-
log "github.com/sirupsen/logrus"
10+
"github.com/sirupsen/logrus"
1111
)
1212

1313
var replacers map[string]Replacer = map[string]Replacer{}
@@ -18,7 +18,7 @@ func SetReplacer(replacer Replacer) {
1818

1919
func ReplaceInFiles(rootDirectory string) {
2020
if _, err := os.Stat(rootDirectory); os.IsNotExist(err) {
21-
log.WithField("category", "placeholders").Warn(rootDirectory + " not found.")
21+
logrus.WithField("category", "placeholders").Warn(rootDirectory + " not found.")
2222
return
2323
}
2424

@@ -33,7 +33,7 @@ func ReplaceInFiles(rootDirectory string) {
3333
return nil
3434
})
3535
if err != nil {
36-
log.WithField("category", "placeholders").Fatal(err)
36+
logrus.WithField("category", "placeholders").Fatal(err)
3737
}
3838

3939
err = filepath.Walk(rootDirectory,
@@ -50,14 +50,14 @@ func ReplaceInFiles(rootDirectory string) {
5050
return nil
5151
})
5252
if err != nil {
53-
log.WithField("category", "placeholders").Fatal(err)
53+
logrus.WithField("category", "placeholders").Fatal(err)
5454
}
5555
}
5656

5757
func containsPlaceHolders(path string) bool {
5858
b, err := ioutil.ReadFile(path)
5959
if err != nil {
60-
log.Fatal(err)
60+
logrus.Fatal(err)
6161
}
6262
contains := false
6363
for _, replacer := range replacers {
@@ -70,7 +70,7 @@ func containsPlaceHolders(path string) bool {
7070
func replacePlaceHolders(path string) error {
7171
b, err := ioutil.ReadFile(path)
7272
if err != nil {
73-
log.Fatal(err)
73+
logrus.Fatal(err)
7474
}
7575

7676
result := string(b)

server/main.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ import (
1111
"shortpaste/public"
1212

1313
nested "github.com/antonfisher/nested-logrus-formatter"
14-
log "github.com/sirupsen/logrus"
14+
"github.com/sirupsen/logrus"
1515

1616
"github.com/go-chi/chi/v5"
1717
"github.com/go-chi/chi/v5/middleware"
1818
)
1919

2020
func init() {
21-
logLevel := log.InfoLevel
21+
logLevel := logrus.InfoLevel
2222
if config.IsDebug() {
23-
logLevel = log.DebugLevel
23+
logLevel = logrus.DebugLevel
2424
}
2525

26-
log.SetLevel(logLevel)
27-
log.SetFormatter(&nested.Formatter{
26+
logrus.SetLevel(logLevel)
27+
logrus.SetFormatter(&nested.Formatter{
2828
HideKeys: true,
2929
FieldsOrder: []string{"component", "category"},
3030
})
@@ -58,7 +58,7 @@ func main() {
5858
r.Route(basePath, public.Router)
5959
r.Route(basePath+"api/v"+constants.API_VERSION, api.Router)
6060

61-
log.WithField("category", "general").Info("Server started: http://0.0.0.0:" + config.GetPort())
61+
logrus.WithField("category", "general").Info("Server started: http://0.0.0.0:" + config.GetPort())
6262

63-
log.Fatal(http.ListenAndServe(":"+config.GetPort(), r))
63+
logrus.Fatal(http.ListenAndServe(":"+config.GetPort(), r))
6464
}

server/public/file.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ func FileGet(w http.ResponseWriter, r *http.Request) {
2424
}
2525

2626
filePath := path.Join(config.GetDataDirPath(), "files", file.ID, file.Name)
27-
if _, ok := r.URL.Query()["download"]; ok {
28-
file.DownloadCount += 1
29-
db.Save(&file)
27+
_, isDownload := r.URL.Query()["download"]
28+
_, isView := r.URL.Query()["view"]
29+
if isDownload || isView {
30+
if isDownload {
31+
file.DownloadCount += 1
32+
db.Save(&file)
33+
}
3034

3135
w.Header().Set("Content-Disposition", "attachment; filename="+file.Name)
3236
http.ServeFile(w, r, filePath)
@@ -53,7 +57,7 @@ func FileGet(w http.ResponseWriter, r *http.Request) {
5357
Size string
5458
}{
5559
Name: file.Name,
56-
Src: "/f/" + id + "?download",
60+
Src: "/f/" + id + "?view",
5761
Image: strings.HasPrefix(file.MIME, "image/"),
5862
Size: tools.IECFormat(fi.Size()),
5963
}

0 commit comments

Comments
 (0)