From 269cb8586eb0578e21f38e18dd2453da883a5171 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sun, 11 Dec 2022 20:56:39 +0000 Subject: [PATCH] feat(job): add ImageFilename() method Produces a filename string that matches what midjourney.com produces when you download a image. --- job.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/job.go b/job.go index 557101d..4a48dab 100644 --- a/job.go +++ b/job.go @@ -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) +}