-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_dalle.go
67 lines (59 loc) · 1.77 KB
/
handle_dalle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
"github.com/TrueBlocks/trueblocks-dalleserver/pkg/dd"
)
var isDebugging = false
func (a *App) handleDalleDress(w http.ResponseWriter, r *http.Request) {
a.Logger.Printf("Received request: %s %s", r.Method, r.URL.Path)
req, err := a.parseRequest(r)
a.Logger.Printf("Req: %s", req.String())
if err != nil {
a.Logger.Printf("Err: %s", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
req.Respond(w, r)
}
func (req *Request) Respond(w io.Writer, r *http.Request) {
exists := file.FileExists(req.filePath)
if req.remove {
if !exists {
fmt.Fprintln(w, "Image not found")
return
}
os.Remove(req.filePath)
fmt.Fprintln(w, "Image removed")
return
}
// If the file already exists and we're not told to generate it, serve it
req.app.Logger.Println("exists:", exists)
req.app.Logger.Println("generate:", req.generate)
if exists && !req.generate {
rw, ok := w.(http.ResponseWriter)
if ok {
req.app.Logger.Println("the image exists, serving it")
http.ServeFile(rw, r, req.filePath)
return
}
}
if !isDebugging {
// If the file is currently being generated, return a waiting message
lockFilePath := filepath.Join("./pending", req.series+"-"+req.address+".lck")
if _, err := os.Stat(lockFilePath); err == nil {
req.app.Logger.Println("the image is being generated")
fmt.Fprintln(w, "Your image will be ready shortly")
return
}
req.app.Logger.Println("calling the go routine to generate the image")
go dd.CreateDalleDress(req.series, req.address, req.filePath)
} else {
dd.CreateDalleDress(req.series, req.address, req.filePath)
}
fmt.Fprintln(w, "Your image will be ready shortly")
}