feat(job): add ImageFilename() method

Produces a filename string that matches what midjourney.com produces
when you download a image.
This commit is contained in:
2022-12-11 20:56:39 +00:00
parent 7b1d0a0376
commit 269cb8586e

17
job.go
View File

@@ -1,6 +1,9 @@
package midjourney
import "fmt"
import (
"fmt"
"regexp"
)
type JobType string
@@ -75,3 +78,15 @@ func (j *Job) DiscordURL() string {
func (j *Job) MainImageURL() string {
return fmt.Sprintf("https://mj-gallery.com/%s/grid_0.png", j.ID)
}
var imageFilenameRegexp = regexp.MustCompile(`[^a-zA-Z0-9\._]+`)
func (j *Job) ImageFilename() string {
s := fmt.Sprintf("%s_%s", j.Username, j.Prompt)
s = imageFilenameRegexp.ReplaceAllString(s, "_")
if len(s) > 63 {
s = s[:63]
}
return fmt.Sprintf("%s_%s.png", s, j.ID)
}