use prefix const instead of hardcoding

This commit is contained in:
Antoni Sawicki 2024-07-02 00:46:24 -07:00
parent 7673e15b9e
commit bf946f0f63
2 changed files with 9 additions and 6 deletions

13
txt.go
View File

@ -31,6 +31,8 @@ import (
var imgStor imageStore
const imgZpfx = "/imgz/"
func init() {
imgStor.img = make(map[string]imageContainer)
// TODO: add garbage collector
@ -102,9 +104,9 @@ func (t *astTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
link.Destination = append([]byte("?t=txt&url="), link.Destination...)
}
if img, ok := n.(*ast.Image); ok && entering {
id := fmt.Sprintf("txt%04d.gif", rand.Intn(9999)) // atomic.AddInt64 could be better here
grabImage(id, string(img.Destination)) // TODO: goroutines with waitgroup
img.Destination = []byte("/imgz/" + id)
id := fmt.Sprintf("txt%05d.gif", rand.Intn(99999)) // atomic.AddInt64 could be better here
grabImage(id, string(img.Destination)) // TODO: use goroutines with waitgroup
img.Destination = []byte(imgZpfx + id)
}
return ast.WalkContinue, nil
})
@ -140,11 +142,12 @@ func (rq *wrpReq) captureMarkdown() {
}
func imgServerZ(w http.ResponseWriter, r *http.Request) {
log.Printf("%s IMGZ Request for %s\n", r.RemoteAddr, r.URL.Path)
id := strings.Replace(r.URL.Path, "/imgz/", "", 1)
log.Printf("%s IMGZ Request for %s", r.RemoteAddr, r.URL.Path)
id := strings.Replace(r.URL.Path, imgZpfx, "", 1)
img, err := imgStor.get(id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("%s IMGZ error for %s: %v", r.RemoteAddr, r.URL.Path, err)
return
}
imgStor.del(id)

2
wrp.go
View File

@ -616,7 +616,7 @@ func main() {
http.HandleFunc("/", pageServer)
http.HandleFunc("/map/", mapServer)
http.HandleFunc("/img/", imgServer)
http.HandleFunc("/imgz/", imgServerZ)
http.HandleFunc(imgZpfx, imgServerZ)
http.HandleFunc("/shutdown/", haltServer)
http.HandleFunc("/favicon.ico", http.NotFound)