Skip to content

Commit

Permalink
Bin the URL length setting
Browse files Browse the repository at this point in the history
  • Loading branch information
hgw8 committed Dec 28, 2023
1 parent bd9f682 commit c52f37e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ You can upload files using cURL like so:
curl -F file=@$1 https://hardfiles.org/
```

Additionally, you can append some extra options to modify the expiry time or file name length. Currently the file expiry time must be provided in seconds and is limited to 5 days maximum. The file name length is limited to 128 characters. The following example will return a file that expires in 48 hours rather than the default 24 and a file name length of 64 characters:
Additionally, you can specify the amount of time before your upload is removed from the server. Currently the file expiry time must be provided in seconds and is limited to 5 days maximum. The following example will return a file that expires in 48 hours rather than the default of 24 hours.

```shell
curl -F file=@$1 -F expiry=172800 -F url_len=64 https://hardfiles.org/
curl -F file=@$1 -F expiry=172800 https://hardfiles.org/
```

### Bash Alias
Expand Down
29 changes: 4 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ func Zeros(path string, size int64) error {
return nil
}

func NameGen(fileNameLength int) string {
func NameGen() string {
const chars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ0123456789"
ll := len(chars)
b := make([]byte, fileNameLength)
b := make([]byte, conf.FileLen)
rand.Read(b) // generates len(b) random bytes
for i := int64(0); i < int64(fileNameLength); i++ {
for i := int64(0); i < int64(conf.FileLen); i++ {
b[i] = chars[int(b[i])%ll]
}
return string(b)
Expand All @@ -113,9 +113,7 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
// expiry time
var name string
var ttl int64
var fileNameLength int

fileNameLength = 0
ttl = 0

file, _, err := r.FormFile("file")
Expand Down Expand Up @@ -151,28 +149,9 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
ttl = int64(conf.DefaultTTL)
}

// Check if the file length parameter exists and also if it's too long
if r.PostFormValue("url_len") != "" {
fileNameLength, err = strconv.Atoi(r.PostFormValue("url_len"))
if err != nil {
log.Error().Err(err).Msg("url_len could not be parsed")
} else {
// if the length is < 3 and > 128 return error
if fileNameLength < 3 || fileNameLength > 128 {
w.WriteHeader(http.StatusBadRequest)
return
}
}
}

// Default to conf if not present
if fileNameLength == 0 {
fileNameLength = conf.FileLen
}

// generate + check name
for {
id := NameGen(fileNameLength)
id := NameGen()
name = id + mtype.Extension()
if !Exists(conf.FileFolder + "/" + name) {
break
Expand Down

0 comments on commit c52f37e

Please sign in to comment.