Change name generation algorithm

This commit is contained in:
Tobias B 2021-11-17 12:04:06 +01:00
parent dcc0cbb659
commit 132983960b
No known key found for this signature in database
GPG Key ID: 5EF4C92355A3B53D
3 changed files with 6 additions and 9 deletions

View File

@ -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. |

View File

@ -1 +1 @@
0.4.1
0.5.0

View File

@ -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
}