resource/backend/utils/reader/sketch.go

51 lines
937 B
Go
Raw Normal View History

2022-07-04 14:23:36 +08:00
package reader
import (
"archive/zip"
"bytes"
"io/ioutil"
"os"
)
const SketchPreviewPath = "previews/preview.png"
type Sketch struct {
}
func (s *Sketch)ReadPreviewFromData(data []byte)([]byte, error){
readAt := bytes.NewReader(data)
ziper, err := zip.NewReader(readAt, int64(len(data)))
if err != nil {
return nil, err
}
pf, err := ziper.Open(SketchPreviewPath)
if err != nil {
return nil, err
}
defer pf.Close()
res, err := ioutil.ReadAll(pf)
return res, err
}
func (s *Sketch)ReadPreview(name string)([]byte, error){
ziper, err := zip.OpenReader(name)
if err != nil {
return nil, err
}
pf, err := ziper.Open(SketchPreviewPath)
if err != nil {
return nil, err
}
defer pf.Close()
res, err := ioutil.ReadAll(pf)
return res, err
}
func (s *Sketch)SavePreview(file string)error{
data, err := s.ReadPreview(file)
if err != nil {
return err
}
return os.WriteFile(file+".png", data, os.ModePerm)
}