Hello mates, I am trying to upload a PNG image to IPFS running a daemon on my machine. The code below give a CID/hash but when I try to see it using https://ipfs.io/Ipfs/CID the image do not appear. Comparing to JS codes, I am not passing any metadata, which is maybe the issue I guess.
I open the image, decode it, convert to bytes and then upload to IPFS using the import shell "github.com/ipfs/go-ipfs-api"
Anyone could help me?
Code:
func ReadImageBytes(path_image string) []byte {
inputFile, _ := os.Open(path_image)
inputFile.Close()
File, err := os.Open(path_image)
if err != nil {
log.Fatal(err)
}
defer File.Close()
img, err := png.Decode(File)
if err != nil {
log.Fatal(err)
}
sz := img.Bounds()
raw := make([]uint8, (sz.Max.X-sz.Min.X)*(sz.Max.Y-sz.Min.Y)*4)
idx := 0
for y := sz.Min.Y; y < sz.Max.Y; y++ {
for x := sz.Min.X; x < sz.Max.X; x++ {
r, g, b, a := img.At(x, y).RGBA()
raw[idx], raw[idx+1], raw[idx+2], raw[idx+3] = uint8(r), uint8(g), uint8(b), uint8(a)
idx += 4
}
}
return raw
}`
func UploadIPFS(data []byte) (string, error) {
sh := shell.NewShell("localhost:5001")
reader := bytes.NewReader(data)
fileHash, err := sh.Add(reader)
if err != nil {
return "", err
}
return fileHash, nil
}