first version

This commit is contained in:
Jay
2019-05-15 18:09:36 +08:00
commit 1275619fa0
10 changed files with 365 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
package config
import (
"errors"
"io/ioutil"
"os"
"path"
"git.trj.tw/golang/utils"
"gopkg.in/yaml.v2"
)
// Config -
type Config struct {
Src string `yaml:"src"`
Dist string `yaml:"dist"`
}
var conf *Config
// LoadConfig -
func LoadConfig(p ...string) error {
var fp string
if len(p) > 0 && len(p[0]) > 0 {
fp = p[0]
} else {
wd, err := os.Getwd()
if err != nil {
return err
}
fp = path.Join(wd, "config.yml")
}
fp = utils.ParsePath(fp)
if exists := utils.CheckExists(fp, false); !exists {
return errors.New("config not found")
}
dataByte, err := ioutil.ReadFile(fp)
if err != nil {
return err
}
conf = &Config{}
err = yaml.Unmarshal(dataByte, conf)
if err != nil {
return err
}
return nil
}
// GetConfig -
func GetConfig() *Config { return conf }
+40
View File
@@ -0,0 +1,40 @@
package image
import (
"fmt"
"io"
"log"
"os"
"path"
"time"
"image/png"
"git.trj.tw/golang/go-watch-webp/module/config"
"golang.org/x/image/webp"
)
// ConvertToPng -
func ConvertToPng(file io.Reader) error {
log.Println("convert func ")
conf := config.GetConfig()
webpImg, err := webp.Decode(file)
if err != nil {
return err
}
filename := fmt.Sprintf("%d.png", time.Now().UnixNano())
fp := path.Join(conf.Dist, filename)
target, err := os.Create(fp)
if err != nil {
return err
}
if err := png.Encode(target, webpImg); err != nil {
return err
}
return nil
}
+23
View File
@@ -0,0 +1,23 @@
package option
import "flag"
// Options -
type Options struct {
Help bool
Config string
}
var opts *Options
// RegOptions -
func RegOptions() {
opts = &Options{}
flag.StringVar(&opts.Config, "config", "", "config file path - default: `pwd/config.yml`")
flag.StringVar(&opts.Config, "f", "", "config file path - default: `pwd/config.yml`")
flag.BoolVar(&opts.Help, "help", false, "show help")
flag.Parse()
}
// GetOptions -
func GetOptions() *Options { return opts }
+40
View File
@@ -0,0 +1,40 @@
package pool
import "sync"
// Pool -
type Pool struct {
q chan bool
wg *sync.WaitGroup
}
// NewPool -
func NewPool(size int) *Pool {
if size < 1 {
size = 1
}
p := &Pool{
q: make(chan bool, size),
wg: &sync.WaitGroup{},
}
return p
}
// Add new queue work
func (p *Pool) Add() {
p.q <- true
p.wg.Add(1)
}
// Done finish work
func (p *Pool) Done() {
<-p.q
p.wg.Done()
}
// Wait -
func (p *Pool) Wait() {
p.wg.Wait()
}