mirror of
https://github.com/tenox7/wrp.git
synced 2025-08-11 17:25:13 +00:00
count image size for simple html mode
This commit is contained in:
35
txt.go
35
txt.go
@@ -2,8 +2,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// - imgOpt image quality for jpeg <<== TEST
|
|
||||||
// - non overlaping image names atomic.int etc
|
// - non overlaping image names atomic.int etc
|
||||||
|
// - add image processing times counter to the footer
|
||||||
// - cache + garbage collector / delete old images from map -- test back/button behavior in old browsers
|
// - cache + garbage collector / delete old images from map -- test back/button behavior in old browsers
|
||||||
// - add referer header
|
// - add referer header
|
||||||
// - svg support
|
// - svg support
|
||||||
@@ -88,40 +88,40 @@ func (i *imageStore) del(id string) {
|
|||||||
delete(i.img, id)
|
delete(i.img, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchImage(id, url, imgType string, maxSize, imgOpt int) error {
|
func fetchImage(id, url, imgType string, maxSize, imgOpt int) (int, error) {
|
||||||
log.Printf("Downloading IMGZ URL=%q for ID=%q", url, id)
|
log.Printf("Downloading IMGZ URL=%q for ID=%q", url, id)
|
||||||
var img []byte
|
var in []byte
|
||||||
var err error
|
var err error
|
||||||
switch url[:4] {
|
switch url[:4] {
|
||||||
case "http":
|
case "http":
|
||||||
r, err := http.Get(url) // TODO: possibly set a header "referer" here
|
r, err := http.Get(url) // TODO: possibly set a header "referer" here
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error downloading %q: %v", url, err)
|
return 0, fmt.Errorf("Error downloading %q: %v", url, err)
|
||||||
}
|
}
|
||||||
if r.StatusCode != http.StatusOK {
|
if r.StatusCode != http.StatusOK {
|
||||||
return fmt.Errorf("Error %q HTTP Status Code: %v", url, r.StatusCode)
|
return 0, fmt.Errorf("Error %q HTTP Status Code: %v", url, r.StatusCode)
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
img, err = io.ReadAll(r.Body)
|
in, err = io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error reading %q: %v", url, err)
|
return 0, fmt.Errorf("Error reading %q: %v", url, err)
|
||||||
}
|
}
|
||||||
case "data":
|
case "data":
|
||||||
idx := strings.Index(url, ",")
|
idx := strings.Index(url, ",")
|
||||||
if idx < 1 {
|
if idx < 1 {
|
||||||
return fmt.Errorf("image is embeded but unable to find coma: %q", url)
|
return 0, fmt.Errorf("image is embeded but unable to find coma: %q", url)
|
||||||
}
|
}
|
||||||
img, err = base64.StdEncoding.DecodeString(url[idx+1:])
|
in, err = base64.StdEncoding.DecodeString(url[idx+1:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error decoding image from url embed: %q: %v", url, err)
|
return 0, fmt.Errorf("error decoding image from url embed: %q: %v", url, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gif, err := smallImg(img, imgType, maxSize, imgOpt)
|
out, err := smallImg(in, imgType, maxSize, imgOpt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error scaling down image: %v", err)
|
return 0, fmt.Errorf("Error scaling down image: %v", err)
|
||||||
}
|
}
|
||||||
imgStor.add(id, url, gif)
|
imgStor.add(id, url, out)
|
||||||
return nil
|
return len(out), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func smallImg(src []byte, imgType string, maxSize, imgOpt int) ([]byte, error) {
|
func smallImg(src []byte, imgType string, maxSize, imgOpt int) ([]byte, error) {
|
||||||
@@ -163,6 +163,7 @@ type astTransformer struct {
|
|||||||
imgType string
|
imgType string
|
||||||
maxSize int
|
maxSize int
|
||||||
imgOpt int
|
imgOpt int
|
||||||
|
totSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *astTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
|
func (t *astTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
|
||||||
@@ -171,14 +172,15 @@ func (t *astTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
|
|||||||
link.Destination = append([]byte("/?m=html&t="+t.imgType+"&s="+strconv.Itoa(t.maxSize)+"&url="), link.Destination...)
|
link.Destination = append([]byte("/?m=html&t="+t.imgType+"&s="+strconv.Itoa(t.maxSize)+"&url="), link.Destination...)
|
||||||
}
|
}
|
||||||
if img, ok := n.(*ast.Image); ok && entering {
|
if img, ok := n.(*ast.Image); ok && entering {
|
||||||
id := fmt.Sprintf("txt%05d.%s", rand.Intn(99999), strings.ToLower(t.imgType)) // BUG: atomic.AddInt64 or something that ever increases - time based?
|
id := fmt.Sprintf("txt%05d.%s", rand.Intn(99999), strings.ToLower(t.imgType)) // BUG: atomic.AddInt64 or something that ever increases - time based?
|
||||||
err := fetchImage(id, string(img.Destination), t.imgType, t.maxSize, t.imgOpt) // TODO: use goroutines with waitgroup
|
size, err := fetchImage(id, string(img.Destination), t.imgType, t.maxSize, t.imgOpt) // TODO: use goroutines with waitgroup
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
n.Parent().RemoveChildren(n)
|
n.Parent().RemoveChildren(n)
|
||||||
return ast.WalkContinue, nil
|
return ast.WalkContinue, nil
|
||||||
}
|
}
|
||||||
img.Destination = []byte(imgZpfx + id)
|
img.Destination = []byte(imgZpfx + id)
|
||||||
|
t.totSize += size
|
||||||
}
|
}
|
||||||
return ast.WalkContinue, nil
|
return ast.WalkContinue, nil
|
||||||
})
|
})
|
||||||
@@ -218,6 +220,7 @@ func (rq *wrpReq) captureMarkdown() {
|
|||||||
rq.printUI(uiParams{
|
rq.printUI(uiParams{
|
||||||
text: string(asciify([]byte(ht.String()))),
|
text: string(asciify([]byte(ht.String()))),
|
||||||
bgColor: "#FFFFFF",
|
bgColor: "#FFFFFF",
|
||||||
|
imgSize: fmt.Sprintf("%.0f KB", float32(t.totSize)/1024.0),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user