2025-03-10 14:53:59 -05:00
|
|
|
package nomino
|
|
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
|
|
// Make generates a random filename. The behavior can be controlled by specifying Options
|
|
|
|
|
// In general, the final filename will be [prefix]_[generated_string]_[original_filename]_[suffix].[extension].
|
|
|
|
|
// If the name generator returns an error (generally, it shouldn't), that will be returned instead.
|
2025-03-14 21:46:30 -05:00
|
|
|
func Make(conf Config, opts ...Option) (string, error) {
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
|
opt(&conf)
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 15:36:30 -05:00
|
|
|
name, err := conf.generator(&conf)
|
2025-03-10 14:53:59 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2025-03-14 21:46:30 -05:00
|
|
|
seperateConf(&conf)
|
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s%s%s%s%s", conf.prefix, name, conf.original, conf.suffix, conf.extension), nil
|
|
|
|
|
}
|
2025-03-10 14:53:59 -05:00
|
|
|
|
2025-03-14 21:46:30 -05:00
|
|
|
func seperateConf(conf *Config) {
|
2025-03-11 16:42:39 -05:00
|
|
|
if conf.prefix != "" {
|
2025-03-14 13:47:44 -05:00
|
|
|
conf.prefix += conf.separator
|
2025-03-11 16:42:39 -05:00
|
|
|
}
|
|
|
|
|
if conf.original != "" {
|
|
|
|
|
conf.original = conf.separator + conf.original
|
|
|
|
|
}
|
|
|
|
|
if conf.suffix != "" {
|
|
|
|
|
conf.suffix = conf.separator + conf.suffix
|
|
|
|
|
}
|
2025-03-10 14:53:59 -05:00
|
|
|
}
|