-
Notifications
You must be signed in to change notification settings - Fork 4
/
imageUtils.go
95 lines (78 loc) · 1.83 KB
/
imageUtils.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"os"
"bytes"
"encoding/base64"
"encoding/hex"
"time"
"golang.org/x/crypto/blake2b"
"github.com/stakwork/sphinx-meme/storage"
)
func getImageDimension(file io.Reader) (int, int) {
image, _, err := image.DecodeConfig(file)
if err != nil {
fmt.Println(err)
return 0, 0
}
return image.Width, image.Height
}
func goTest() {
TTL := 60 * 60 * 24 * 365 * 100
filename := "example10"
contentType := "image/png"
file, err := os.Open("templates/" + filename + ".png")
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
var bufferRead bytes.Buffer
img := io.TeeReader(file, &bufferRead)
var buf bytes.Buffer
hasher, _ := blake2b.New256(nil) // hash it
length, err := io.Copy(&buf, io.TeeReader(img, hasher))
if err != nil {
fmt.Println(err)
return
}
hash := hasher.Sum(nil)
defer file.Close()
// img must be read first, then its in bufferRead
imageWidth, imageHeight := getImageDimension(&bufferRead)
fmt.Println(imageWidth)
nonce, _ := storage.Store.GenNonce()
nonceString := hex.EncodeToString(nonce[:])
now := time.Now()
media := Media{
ID: base64.URLEncoding.EncodeToString(hash[:]),
OwnerPubKey: "a",
Name: filename,
Description: "payment template",
Tags: []string{},
Size: length,
Filename: filename,
Mime: contentType,
Nonce: nonceString,
TTL: int64(TTL),
Price: 0,
Created: &now,
Updated: &now,
TotalBuys: 0,
TotalSats: 0,
Width: imageWidth,
Height: imageHeight,
Template: true,
}
fmt.Printf("MEDIA: %+v\n", media)
created, err := DB.createMedia(media)
if err != nil {
fmt.Println("error", err)
return
}
path := media.ID
storage.Store.PostReader(path, &buf, length, contentType, nonce)
fmt.Println(created)
}