Bin the URL length setting
This commit is contained in:
parent
bd9f68282c
commit
c52f37e711
@ -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
|
||||
|
29
main.go
29
main.go
@ -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)
|
||||
@ -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")
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user