Skip to content

Commit

Permalink
fix: logic on naming url with secret
Browse files Browse the repository at this point in the history
  • Loading branch information
jabuxas committed Oct 29, 2024
1 parent 343af57 commit c62e2bb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
14 changes: 7 additions & 7 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ func (app *Application) formHandler(w http.ResponseWriter, r *http.Request) {
}
defer file.Close()

filename := app.publicURL(file, ".txt")

full := true
if len(r.Form["secret"]) == 0 {
filename = filename[:8]
full = false
}
filename := app.publicURL(file, ".txt", full)

// reopening file because hash consumes it
file, err = os.Open("/tmp/file.txt")
Expand Down Expand Up @@ -198,11 +198,11 @@ func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) {
}
defer file.Close()

filename := app.publicURL(file, filepath.Ext(handler.Filename))

full := true
if len(r.Form["secret"]) == 0 {
filename = filename[:8]
full = false
}
filename := app.publicURL(file, filepath.Ext(handler.Filename), full)

// reopen the file for copying, as the hash process consumed the file reader
file, _, err = r.FormFile("file")
Expand All @@ -219,7 +219,7 @@ func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) {
ResponseURLHandler(w, app.url, filename)
}

func (app *Application) publicURL(file io.Reader, extension string) string {
func (app *Application) publicURL(file io.Reader, extension string, full bool) string {
filename, _ := HashFile(file, extension)

Check failure on line 223 in handlers.go

View workflow job for this annotation

GitHub Actions / build (linux, amd64)

not enough arguments in call to HashFile

Check failure on line 223 in handlers.go

View workflow job for this annotation

GitHub Actions / build (linux, arm64)

not enough arguments in call to HashFile

Check failure on line 223 in handlers.go

View workflow job for this annotation

GitHub Actions / build (linux, riscv64)

not enough arguments in call to HashFile

Check failure on line 223 in handlers.go

View workflow job for this annotation

GitHub Actions / build (linux, ppc64le)

not enough arguments in call to HashFile

filepath := filepath.Join(app.filesDir, filename)
Expand Down
12 changes: 7 additions & 5 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,19 @@ func FormatFileSize(size int64) string {
return fmt.Sprintf("%.2f GB", float64(size)/(1024*1024*1024))
}

func HashFile(file io.Reader, extension string) (string, error) {
func HashFile(file io.Reader, extension string, full bool) (string, error) {
hasher := md5.New()
if _, err := io.Copy(hasher, file); err != nil {
return "", err
}

sha1Hash := hex.EncodeToString(hasher.Sum(nil))[:8]

sha1Hash := hex.EncodeToString(hasher.Sum(nil))
filename := fmt.Sprintf("%s%s", sha1Hash, extension)

return filename, nil
if full {
return filename, nil
} else {
return fmt.Sprintf("%s%s", sha1Hash[:8], extension), nil
}
}

func SaveFile(name string, file io.Reader) error {
Expand Down

0 comments on commit c62e2bb

Please sign in to comment.