mirror of https://github.com/Superioz/aqua.git
Change name generation algorithm
This commit is contained in:
parent
dcc0cbb659
commit
132983960b
|
@ -47,7 +47,7 @@ For examplary usage of the environment variables, have a look at the `.env.dist`
|
|||
| -------- | ----------- |
|
||||
| `AUTH_CONFIG_PATH` | Path to the `auth.yml` config file. |
|
||||
| `FILE_STORAGE_PATH` | Path to the directory, where the files should be stored. |
|
||||
| `FILE_NAME_LENGTH` | Length of the file names, that should be randomly generated. Should be long enough to make guessing impossible. |
|
||||
| `FILE_NAME_LENGTH` | Length of the file names, that should be randomly generated. Should be long enough to make guessing impossible. Cannot be longer than 24 characters. |
|
||||
| `FILE_META_DB_PATH` | Path to the directory, where the sqlite database for file metadata should be stored. Recommended to not be the same folder as `FILE_STORAGE_PATH` to prevent overlapping. |
|
||||
| `FILE_EXPIRATION_CYCLE` | Determines the interval of the expiration cycle. `5` means that every 5 seconds the files will be checked for expiration. |
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
|
@ -122,21 +123,17 @@ func (fs *FileStorage) StoreFile(of multipart.File, expiration int64) (*StoredFi
|
|||
return sf, nil
|
||||
}
|
||||
|
||||
var escaper = strings.NewReplacer("9", "99", "-", "90", "_", "91")
|
||||
|
||||
// getRandomFileName returns a random string with a fixed size
|
||||
// that is generated from an uuid.
|
||||
// It also checks, that no file with that name already exists,
|
||||
// if that is the case, it generates a new one.
|
||||
func getRandomFileName(size int) (string, error) {
|
||||
if size <= 1 {
|
||||
return "", errors.New("size must be greater than 1")
|
||||
}
|
||||
id, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// strip '-' from uuid
|
||||
str := strings.ReplaceAll(id.String(), "-", "")
|
||||
str := escaper.Replace(base64.RawURLEncoding.EncodeToString([]byte(id.String())))
|
||||
if size >= len(str) {
|
||||
return str, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue