<commit_before>package store

import (
	"path/filepath"
	"strings"
	"sync"

	"github.com/disorganizer/brig/id"
	"github.com/disorganizer/brig/store/wire"
	"github.com/disorganizer/brig/util/ipfsutil"
)

// Store is responsible for adding & retrieving all files from ipfs,
// while managing their metadata in a boltDB.
type Store struct {
	// TODO: Load this.
	fs *FS
	kv KV

	// Internal path of the repository.
	repoPath string

	// IPFS manager layer (from daemon.Server)
	IPFS *ipfsutil.Node

	// Lock for atomic operations inside the store
	// where several db operations happen in a row.
	// Access to the trie is secured by store.Root.RWMutex.
	mu sync.Mutex
}

// Open loads an existing store at `brigPath/$ID/index.bolt`, if it does not
// exist, it is created.  For full function, Connect() should be called
// afterwards.
func Open(brigPath string, owner id.Peer, IPFS *ipfsutil.Node) (*Store, error) {
	dbDir := filepath.Join(
		brigPath,
		"bolt."+strings.Replace(string(owner.ID()), "/", "-", -1),
	)

	kv, err := NewBoltKV(dbDir)
	if err != nil {
		return nil, err
	}

	fs := NewFilesystem(kv)

	st := &Store{
		repoPath: brigPath,
		IPFS:     IPFS,
		fs:       fs,
		kv:       kv,
	}

	// This version does not attempt any version checking:
	if err := fs.MetadataPut("version", []byte("1")); err != nil {
		return nil, err
	}

	// TODO: Insert metadata here
	if err := st.storeOwner(owner); err != nil {
		return nil, err
	}

	return st, err
}

func (st *Store) storeOwner(owner id.Peer) error {
	if err := st.fs.MetadataPut("id", []byte(owner.ID())); err != nil {
		return err
	}

	if err := st.fs.MetadataPut("hash", []byte(owner.Hash())); err != nil {
		return err
	}

	return nil
}

// Owner returns the owner of the store (name + hash)
func (st *Store) Owner() (id.Peer, error) {
	bid, err := st.fs.MetadataGet("id")
	if err != nil {
		return nil, err
	}

	bhash, err := st.fs.MetadataGet("hash")
	if err != nil {
		return nil, err
	}

	ident, err := id.Cast(string(bid))
	if err != nil {
		return nil, err
	}

	return id.NewPeer(ident, string(bhash)), nil
}

// Close syncs all data. It is an error to use the store afterwards.
func (st *Store) Close() error {
	st.mu.Lock()
	defer st.mu.Unlock()

	return st.kv.Close()
}

// Export marshals all relevant inside the database, so a cloned
// repository may import them again.
// The exported data includes:
//  - All files (including their history and keys)
//  - All commits.
//  - Pinning information.
//
// TODO: Describe stream format.
//
// w is not closed after Export.
func (st *Store) Export() (*wire.Store, error) {
	// TODO: Export commits (not implemented)
	// TODO: Export pinning information?
	/*
		protoStore := &wire.Store{}

		var err error

		st.mu.Lock()
		defer st.mu.Unlock()

		st.Root.Walk(true, func(child *File) bool {
			// Note: Walk() already calls Lock()
			protoFile, errPbf := child.ToProto()
			if err != nil {
				err = errPbf
				return false
			}

			if child.kind != FileTypeRegular {
				// Directories are implicit:
				return true
			}

			history, errHist := st.History(child.node.Path())
			if errHist != nil {
				err = errHist
				return false
			}

			protoHist, errPbh := history.ToProto()
			if err != nil {
				err = errPbh
				return false
			}

			protoPack := &wire.Pack{
				File:    protoFile,
				History: protoHist,
			}

			protoStore.Packs = append(protoStore.Packs, protoPack)
			return true
		})

		// TODO: Export refs, only commits are exported currently.
		// TODO: Get Head() and traverse down to root.
		//       -> History is linear?
		//       -> Merge commits have a special Merge attr?
		if err != nil {
			return nil, err
		}

		cmts := &wire.Commits{}

		err = st.viewWithBucket("commits", func(tx *bolt.Tx, bkt *bolt.Bucket) error {
			return bkt.ForEach(func(k, v []byte) error {
				cmt := &wire.Commit{}
				if err := proto.Unmarshal(v, cmt); err != nil {
					return err
				}

				cmts.Commits = append(cmts.Commits, cmt)
				return nil
			})
		})

		if err != nil {
			return nil, err
		}

		protoStore.Commits = cmts
		return protoStore, nil
	*/
	return nil, nil
}

// Import unmarshals the data written by export.
// If succesful, a new store with the data is created.
func (st *Store) Import(protoStore *wire.Store) error {
	st.mu.Lock()
	defer st.mu.Unlock()

	// TODO: re-design
	/*
		for _, pack := range protoStore.Packs {
			file := emptyFile(st)
			if err := file.Import(pack.GetFile()); err != nil {
				return err
			}

			log.Debugf("-- Imported: %v", file.Path())
			file.Sync()
			file.updateParents()

			// TODO: Only make one transaction after the for{}.
			for _, protoCheckpoint := range pack.GetHistory().GetHist() {
				err := st.updateWithBucket("refs", func(tx *bolt.Tx, bkt *bolt.Bucket) error {
					data, err := proto.Marshal(protoCheckpoint)
					if err != nil {
						return err
					}

					return bkt.Put(protoCheckpoint.GetModTime(), data)
				})

				if err != nil {
					return err
				}
			}
		}

		return st.updateWithBucket("commits", func(tx *bolt.Tx, bkt *bolt.Bucket) error {
			for _, protoCommit := range protoStore.GetCommits().GetCommits() {
				data, err := proto.Marshal(protoCommit)
				if err != nil {
					return err
				}

				if err := bkt.Put(protoCommit.GetHash(), data); err != nil {
					return err
				}
			}

			return nil
		})
	*/
	return nil
}
<commit_msg>store: Remember the .Transaction() idea<commit_after>package store

import (
	"path/filepath"
	"strings"
	"sync"

	"github.com/disorganizer/brig/id"
	"github.com/disorganizer/brig/store/wire"
	"github.com/disorganizer/brig/util/ipfsutil"
)

// Store is responsible for adding & retrieving all files from ipfs,
// while managing their metadata in a boltDB.
type Store struct {
	// TODO: Load this.
	fs *FS
	kv KV

	// Internal path of the repository.
	repoPath string

	// IPFS manager layer (from daemon.Server)
	IPFS *ipfsutil.Node

	// Lock for atomic operations inside the store
	// where several db operations happen in a row.
	// Access to the trie is secured by store.Root.RWMutex.
	mu sync.Mutex
}

// Open loads an existing store at `brigPath/$ID/index.bolt`, if it does not
// exist, it is created.  For full function, Connect() should be called
// afterwards.
func Open(brigPath string, owner id.Peer, IPFS *ipfsutil.Node) (*Store, error) {
	dbDir := filepath.Join(
		brigPath,
		"bolt."+strings.Replace(string(owner.ID()), "/", "-", -1),
	)

	kv, err := NewBoltKV(dbDir)
	if err != nil {
		return nil, err
	}

	fs := NewFilesystem(kv)

	st := &Store{
		repoPath: brigPath,
		IPFS:     IPFS,
		fs:       fs,
		kv:       kv,
	}

	// This version does not attempt any version checking:
	if err := fs.MetadataPut("version", []byte("1")); err != nil {
		return nil, err
	}

	// TODO: Insert metadata here
	if err := st.storeOwner(owner); err != nil {
		return nil, err
	}

	return st, err
}

func (st *Store) storeOwner(owner id.Peer) error {
	if err := st.fs.MetadataPut("id", []byte(owner.ID())); err != nil {
		return err
	}

	if err := st.fs.MetadataPut("hash", []byte(owner.Hash())); err != nil {
		return err
	}

	return nil
}

// Owner returns the owner of the store (name + hash)
func (st *Store) Owner() (id.Peer, error) {
	bid, err := st.fs.MetadataGet("id")
	if err != nil {
		return nil, err
	}

	bhash, err := st.fs.MetadataGet("hash")
	if err != nil {
		return nil, err
	}

	ident, err := id.Cast(string(bid))
	if err != nil {
		return nil, err
	}

	return id.NewPeer(ident, string(bhash)), nil
}

// TODO: Use this for the fuse layer.
func (st *Store) Transaction(fn func() error) error {
	st.Lock()
	defer st.mu.Unlock()

	return fn()
}

// Close syncs all data. It is an error to use the store afterwards.
func (st *Store) Close() error {
	st.mu.Lock()
	defer st.mu.Unlock()

	return st.kv.Close()
}

// Export marshals all relevant inside the database, so a cloned
// repository may import them again.
// The exported data includes:
//  - All files (including their history and keys)
//  - All commits.
//  - Pinning information.
//
// TODO: Describe stream format.
//
// w is not closed after Export.
func (st *Store) Export() (*wire.Store, error) {
	// TODO: Export commits (not implemented)
	// TODO: Export pinning information?
	/*
		protoStore := &wire.Store{}

		var err error

		st.mu.Lock()
		defer st.mu.Unlock()

		st.Root.Walk(true, func(child *File) bool {
			// Note: Walk() already calls Lock()
			protoFile, errPbf := child.ToProto()
			if err != nil {
				err = errPbf
				return false
			}

			if child.kind != FileTypeRegular {
				// Directories are implicit:
				return true
			}

			history, errHist := st.History(child.node.Path())
			if errHist != nil {
				err = errHist
				return false
			}

			protoHist, errPbh := history.ToProto()
			if err != nil {
				err = errPbh
				return false
			}

			protoPack := &wire.Pack{
				File:    protoFile,
				History: protoHist,
			}

			protoStore.Packs = append(protoStore.Packs, protoPack)
			return true
		})

		// TODO: Export refs, only commits are exported currently.
		// TODO: Get Head() and traverse down to root.
		//       -> History is linear?
		//       -> Merge commits have a special Merge attr?
		if err != nil {
			return nil, err
		}

		cmts := &wire.Commits{}

		err = st.viewWithBucket("commits", func(tx *bolt.Tx, bkt *bolt.Bucket) error {
			return bkt.ForEach(func(k, v []byte) error {
				cmt := &wire.Commit{}
				if err := proto.Unmarshal(v, cmt); err != nil {
					return err
				}

				cmts.Commits = append(cmts.Commits, cmt)
				return nil
			})
		})

		if err != nil {
			return nil, err
		}

		protoStore.Commits = cmts
		return protoStore, nil
	*/
	return nil, nil
}

// Import unmarshals the data written by export.
// If succesful, a new store with the data is created.
func (st *Store) Import(protoStore *wire.Store) error {
	st.mu.Lock()
	defer st.mu.Unlock()

	// TODO: re-design
	/*
		for _, pack := range protoStore.Packs {
			file := emptyFile(st)
			if err := file.Import(pack.GetFile()); err != nil {
				return err
			}

			log.Debugf("-- Imported: %v", file.Path())
			file.Sync()
			file.updateParents()

			// TODO: Only make one transaction after the for{}.
			for _, protoCheckpoint := range pack.GetHistory().GetHist() {
				err := st.updateWithBucket("refs", func(tx *bolt.Tx, bkt *bolt.Bucket) error {
					data, err := proto.Marshal(protoCheckpoint)
					if err != nil {
						return err
					}

					return bkt.Put(protoCheckpoint.GetModTime(), data)
				})

				if err != nil {
					return err
				}
			}
		}

		return st.updateWithBucket("commits", func(tx *bolt.Tx, bkt *bolt.Bucket) error {
			for _, protoCommit := range protoStore.GetCommits().GetCommits() {
				data, err := proto.Marshal(protoCommit)
				if err != nil {
					return err
				}

				if err := bkt.Put(protoCommit.GetHash(), data); err != nil {
					return err
				}
			}

			return nil
		})
	*/
	return nil
}
<|endoftext|>


<commit_before>package postgresql

import (
	"database/sql"
	"fmt"
	"testing"

	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func TestAccPostgresqlExtension_Basic(t *testing.T) {

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckPostgresqlExtensionDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccPostgresqlExtensionConfig,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckPostgresqlExtensionExists("postgresql_extension.myextension"),
					resource.TestCheckResourceAttr(
						"postgresql_role.myextension", "name", "pg_trgm"),
				),
			},
		},
	})
}

func testAccCheckPostgresqlExtensionDestroy(s *terraform.State) error {
	client := testAccProvider.Meta().(*Client)

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "postgresql_extension" {
			continue
		}

		exists, err := checkExtensionExists(client, rs.Primary.ID)

		if err != nil {
			return fmt.Errorf("Error checking extension %s", err)
		}

		if exists {
			return fmt.Errorf("Extension still exists after destroy")
		}
	}

	return nil
}

func testAccCheckPostgresqlExtensionExists(n string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Resource not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No ID is set")
		}

		client := testAccProvider.Meta().(*Client)
		exists, err := checkExtensionExists(client, rs.Primary.ID)

		if err != nil {
			return fmt.Errorf("Error checking extension %s", err)
		}

		if !exists {
			return fmt.Errorf("Extension not found")
		}

		return nil
	}
}

func checkExtensionExists(client *Client, extensionName string) (bool, error) {
	conn, err := client.Connect()
	if err != nil {
		return false, err
	}
	defer conn.Close()

	var _rez int
	err = conn.QueryRow("SELECT 1 from pg_extension d WHERE extname=$1", extensionName).Scan(&_rez)
	switch {
	case err == sql.ErrNoRows:
		return false, nil
	case err != nil:
		return false, fmt.Errorf("Error reading info about extension: %s", err)
	default:
		return true, nil
	}
}

var testAccPostgresqlExtensionConfig = `
resource "postgresql_extension" "myextension" {
  name = "pg_trgm"
}
`
<commit_msg>provider/postgresql: Fixing up the PG Extension test<commit_after>package postgresql

import (
	"database/sql"
	"fmt"
	"testing"

	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func TestAccPostgresqlExtension_Basic(t *testing.T) {

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckPostgresqlExtensionDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccPostgresqlExtensionConfig,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckPostgresqlExtensionExists("postgresql_extension.myextension"),
					resource.TestCheckResourceAttr(
						"postgresql_extension.myextension", "name", "pg_trgm"),
				),
			},
		},
	})
}

func testAccCheckPostgresqlExtensionDestroy(s *terraform.State) error {
	client := testAccProvider.Meta().(*Client)

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "postgresql_extension" {
			continue
		}

		exists, err := checkExtensionExists(client, rs.Primary.ID)

		if err != nil {
			return fmt.Errorf("Error checking extension %s", err)
		}

		if exists {
			return fmt.Errorf("Extension still exists after destroy")
		}
	}

	return nil
}

func testAccCheckPostgresqlExtensionExists(n string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Resource not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No ID is set")
		}

		client := testAccProvider.Meta().(*Client)
		exists, err := checkExtensionExists(client, rs.Primary.ID)

		if err != nil {
			return fmt.Errorf("Error checking extension %s", err)
		}

		if !exists {
			return fmt.Errorf("Extension not found")
		}

		return nil
	}
}

func checkExtensionExists(client *Client, extensionName string) (bool, error) {
	conn, err := client.Connect()
	if err != nil {
		return false, err
	}
	defer conn.Close()

	var _rez int
	err = conn.QueryRow("SELECT 1 from pg_extension d WHERE extname=$1", extensionName).Scan(&_rez)
	switch {
	case err == sql.ErrNoRows:
		return false, nil
	case err != nil:
		return false, fmt.Errorf("Error reading info about extension: %s", err)
	default:
		return true, nil
	}
}

var testAccPostgresqlExtensionConfig = `
resource "postgresql_extension" "myextension" {
  name = "pg_trgm"
}
`
<|endoftext|>


<commit_before>package main

import (
	"../../src/graph/_obj/graph"

	"fmt"
	"flag"
	"os"
	"path"

	"github.com/StepLg/go-erx/src/erx"
)

func main() {
    defer func() {
        if err := recover(); err != nil {
            if errErx, ok := err.(erx.Error); ok {
                formatter := erx.NewStringFormatter("  ")
                fmt.Println(formatter.Format(errErx))
            }
        }
    }()

	flag_help := flag.Bool("help", false, "Display this help.")
	flag_inputFile := flag.String("in", "",
`Input file with messages. If file extension is .ugr, .dgr or .mgr then
graph type automaticly set to undirected, directed or mixed respectively.
If flag doesn't set, then read from stdin.`)
	flag_outputFile := flag.String("out", "",
`Output file. If it isn't set and flag -autoname isn't set too, then
output to stdout.`)
	flag_autoname := flag.Bool("autoname", false,
`Generate output file name automaticly from input file name with
replacing it's extension to ".dot."`)
	flag_type := flag.String("type", "",
`[u|d|m] -- Graph type: undirected, directed or mixed respectively.`)

	flag.Parse()

	if *flag_help {
		flag.PrintDefaults()
		return
	}

	infile := os.Stdin
	if *flag_inputFile!="" {
		var err os.Error
		infile, err = os.Open(*flag_inputFile, os.O_RDONLY, 0000)
		if err!=nil {
			erxErr := erx.NewSequent("Can't open input file.", err)
			erxErr.AddV("file name", *flag_inputFile)
			panic(erxErr)
		}

		infileExt := path.Ext(*flag_inputFile)

		if *flag_autoname {
			// generating autoname for output file
			fpath, fname := path.Split(*flag_inputFile)
			baseFileName := fname
			baseFileName = fname[0:len(fname)-len(infileExt)]

			*flag_outputFile = path.Join(fpath, baseFileName + ".dot")
		}

		// initializing graph type from known file extensions.
		switch infileExt {
			case ".ugr":
				*flag_type = "u"
			case ".dgr":
				*flag_type = "d"
			case ".mgr":
				*flag_type = "m"
		}
	}

	outfile := os.Stdout
	if *flag_outputFile!="" {
		var err os.Error
		outfile, err = os.Open(*flag_outputFile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0644)
		if err!=nil {
			erxErr := erx.NewSequent("Can't open output file.", err)
			erxErr.AddV("file name", *flag_outputFile)
			panic(erxErr)
		}
	}

	switch *flag_type {
		case "u":
			gr := graph.NewUndirectedMap()
			graph.ReadUgraphFile(infile, gr)
			graph.PlotUgraphToDot(gr, outfile, nil, nil)
		case "d":
			gr := graph.NewDirectedMap()
			graph.ReadDgraphFile(infile, gr)
			graph.PlotDgraphToDot(gr, outfile, nil, nil)
		case "m":
			gr := graph.NewMixedMap()
			graph.ReadMgraphFile(infile, gr)
			graph.PlotMgraphToDot(gr, outfile, nil, nil)
		default:
			err := erx.NewError("Unknown type flag.")
			err.AddV("flag value", *flag_type)
			panic(err)
	}

	outfile.Close()
	return
}
<commit_msg>add help message to todot example<commit_after>package main

import (
	"../../src/graph/_obj/graph"

	"fmt"
	"flag"
	"os"
	"path"

	"github.com/StepLg/go-erx/src/erx"
)

func main() {
    defer func() {
        if err := recover(); err != nil {
            if errErx, ok := err.(erx.Error); ok {
                formatter := erx.NewStringFormatter("  ")
                fmt.Println(formatter.Format(errErx))
            }
        }
    }()

	flag_help := flag.Bool("help", false, "Display this help.")
	flag_inputFile := flag.String("in", "",
`Input file with messages. If file extension is .ugr, .dgr or .mgr then
graph type automaticly set to undirected, directed or mixed respectively.
If flag doesn't set, then read from stdin.`)
	flag_outputFile := flag.String("out", "",
`Output file. If it isn't set and flag -autoname isn't set too, then
output to stdout.`)
	flag_autoname := flag.Bool("autoname", false,
`Generate output file name automaticly from input file name with
replacing it's extension to ".dot."`)
	flag_type := flag.String("type", "",
`[u|d|m] -- Graph type: undirected, directed or mixed respectively.`)

	flag.Parse()

	if *flag_help {
		fmt.Println(`Read graph from text representation and output it in graphviz format.`)
		fmt.Println()
		flag.PrintDefaults()
		return
	}

	infile := os.Stdin
	if *flag_inputFile!="" {
		var err os.Error
		infile, err = os.Open(*flag_inputFile, os.O_RDONLY, 0000)
		if err!=nil {
			erxErr := erx.NewSequent("Can't open input file.", err)
			erxErr.AddV("file name", *flag_inputFile)
			panic(erxErr)
		}

		infileExt := path.Ext(*flag_inputFile)

		if *flag_autoname {
			// generating autoname for output file
			fpath, fname := path.Split(*flag_inputFile)
			baseFileName := fname
			baseFileName = fname[0:len(fname)-len(infileExt)]

			*flag_outputFile = path.Join(fpath, baseFileName + ".dot")
		}

		// initializing graph type from known file extensions.
		switch infileExt {
			case ".ugr":
				*flag_type = "u"
			case ".dgr":
				*flag_type = "d"
			case ".mgr":
				*flag_type = "m"
		}
	}

	outfile := os.Stdout
	if *flag_outputFile!="" {
		var err os.Error
		outfile, err = os.Open(*flag_outputFile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0644)
		if err!=nil {
			erxErr := erx.NewSequent("Can't open output file.", err)
			erxErr.AddV("file name", *flag_outputFile)
			panic(erxErr)
		}
	}

	switch *flag_type {
		case "u":
			gr := graph.NewUndirectedMap()
			graph.ReadUgraphFile(infile, gr)
			graph.PlotUgraphToDot(gr, outfile, nil, nil)
		case "d":
			gr := graph.NewDirectedMap()
			graph.ReadDgraphFile(infile, gr)
			graph.PlotDgraphToDot(gr, outfile, nil, nil)
		case "m":
			gr := graph.NewMixedMap()
			graph.ReadMgraphFile(infile, gr)
			graph.PlotMgraphToDot(gr, outfile, nil, nil)
		default:
			err := erx.NewError("Unknown type flag.")
			err.AddV("flag value", *flag_type)
			panic(err)
	}

	outfile.Close()
	return
}
<|endoftext|>


<commit_before>package cqlb

import (
	"reflect"
	"strings"

	"github.com/gocql/gocql"
)

func compile(v interface{}, cols []gocql.ColumnInfo) error {
	return nil
}

func fields(v interface{}) map[string]interface{} {
	result := map[string]interface{}{}
	value := reflect.ValueOf(v)
	indirect := reflect.Indirect(value)
	t := indirect.Type()
	for i := 0; i < t.NumField(); i++ {
		var inf interface{}
		fv := indirect.Field(i)
		kind := fv.Kind()
		if kind.String() == "slice" {
			inf = contentOfSlice(fv)
		}
		f := t.Field(i)
		tag := f.Tag.Get("cql")
		if inf == nil {
			inf = indirect.Field(i).Interface()
		}
		if tag != "" {
			result[tag] = inf
		} else {
			//fmt.Println(f.Name, indirect.Field(f.Index[0]))
			result[strings.ToLower(f.Name)] = inf
			//b.fieldMap[strings.ToLower(f.Name)] = f.Index
		}
	}
	return result
}

func contentOfSlice(v reflect.Value) []interface{} {
	slice := make([]interface{}, v.Len())
	for i := 0; i < v.Len(); i++ {
		f := v.Index(i)
		if f.Kind().String() != "ptr" && f.Kind().String() != "struct" {
			slice[i] = f.Interface()
			continue
		}
		elem := f.Elem()
		if elem.Kind() != reflect.Struct {
			slice[i] = elem.Interface()
			continue
		}
		nestedFields := fields(f.Interface())
		slice[i] = nestedFields
	}
	return slice
}

func getType(v interface{}) {

}
<commit_msg>Using indirect instead of ask for Kind.<commit_after>package cqlb

import (
	"reflect"
	"strings"

	"github.com/gocql/gocql"
)

func compile(v interface{}, cols []gocql.ColumnInfo) error {
	return nil
}

func fields(v interface{}) map[string]interface{} {
	result := map[string]interface{}{}
	value := reflect.ValueOf(v)
	indirect := reflect.Indirect(value)
	t := indirect.Type()
	for i := 0; i < t.NumField(); i++ {
		var inf interface{}
		fv := indirect.Field(i)
		kind := fv.Kind()
		if kind.String() == "slice" {
			inf = contentOfSlice(fv)
		}
		f := t.Field(i)
		tag := f.Tag.Get("cql")
		if inf == nil {
			inf = indirect.Field(i).Interface()
		}
		if tag != "" {
			result[tag] = inf
		} else {
			//fmt.Println(f.Name, indirect.Field(f.Index[0]))
			result[strings.ToLower(f.Name)] = inf
			//b.fieldMap[strings.ToLower(f.Name)] = f.Index
		}
	}
	return result
}

func contentOfSlice(v reflect.Value) []interface{} {
	slice := make([]interface{}, v.Len())
	for i := 0; i < v.Len(); i++ {
		f := reflect.Indirect(v.Index(i))
		slice[i] = f.Interface()
	}
	return slice
}

func getType(v interface{}) {

}
<|endoftext|>


<commit_before>package spawn

import (
	"testing"
)

func test(t *testing.T, expected bool, message string) {
	if !expected {
		t.Error(message)
	}
}

func TestIsAlphaNumeric(t *testing.T) {
	str1 := "abcd1234.35_df-12"
	test(t, isAlphaNumeric(str1), "Expected "+str1+" is alpha numeric, got false")
	str2 := "abcd1234.%*35_df-12"
	test(t, !isAlphaNumeric(str2), "Expected "+str2+" is not alpha numeric, got true")
}
<commit_msg>extended the test method helper<commit_after>package spawn

import (
	"testing"
)

func test(t *testing.T, expected bool, messages ...interface{}) {
	if !expected {
		t.Error(messages)
	}
}

func TestIsAlphaNumeric(t *testing.T) {
	str1 := "abcd1234.35_df-12"
	test(t, isAlphaNumeric(str1), "Expected "+str1+" is alpha numeric, got false")
	str2 := "abcd1234.%*35_df-12"
	test(t, !isAlphaNumeric(str2), "Expected "+str2+" is not alpha numeric, got true")
}
<|endoftext|>


<commit_before>package color

import (
	"unicode"
	"unicode/utf8"
)

const eof = -1

// stateFn represents the state of the scanner as a function that returns the next state.
type stateFn func(*highlighter) stateFn

// highlighter holds the state of the scanner.
type highlighter struct {
	s     string // string being scanned
	pos   int    // position in s
	width int    // width of last rune read from s
	start int    // start position of current verb
	r     rune   // current rune
	attrs string // attributes of current highlight verb
}

// Highlight replaces the highlight verbs in s with their appropriate
// control sequences and then returns the resulting string
func Highlight(s string) string {
	h := &highlighter{s: s}
	h.run()
	return h.s
}

// run runs the state machine for the highlighter.
func (h *highlighter) run() {
	h.next()
	for state := scanText; state != nil; {
		state = state(h)
	}
}

// next gets the next rune in the string.
func (h *highlighter) next() {
	if h.pos >= len(h.s) {
		h.r = eof
		return
	}
	r, w := utf8.DecodeRuneInString(h.s[h.pos:])
	h.pos += w
	h.width = w
	h.r = r
}

// replaces the verb with a control sequence derived from h.attrs[1:].
func (h *highlighter) replace() {
	h.attrs = h.attrs[1:]
	back := h.pos - h.start
	h.s = h.s[:h.pos-back] + csi + h.attrs + "m" + h.s[h.pos:]
	h.pos += len(csi) + len(h.attrs) - back
}

// scans until the next highlight or reset verb.
func scanText(h *highlighter) stateFn {
	for ; ; h.next() {
		switch h.r {
		case eof:
			return nil
		case '%':
			// a verb!
		default:
			continue
		}
		h.next()
		switch h.r {
		case 'r':
			h.start = h.pos - 2
			return verbReset
		case 'h':
			h.start = h.pos - 2
			h.pos++ // skip the [
			h.next()
			return scanHighlight
		case eof:
			return nil
		}
	}
}

// verbReset replaces the reset verb with the reset control sequence.
func verbReset(h *highlighter) stateFn {
	h.attrs = attrs["reset"]
	h.replace()
	return scanText
}

// scanHighlight scans the highlight verb for attributes,
// then replaces it with a control sequence derived from said attributes.
func scanHighlight(h *highlighter) stateFn {
	for ; ; h.next() {
		switch {
		case h.r == eof:
			return nil
		case h.r == 'f':
			return scanColor256(h, preFg256)
		case h.r == 'b':
			return scanColor256(h, preBg256)
		case unicode.IsLetter(h.r):
			return scanAttribute
		case h.r == '+':
			// skip
		case h.r == ']':
			if h.attrs != "" {
				h.replace()
			}
			h.next()
			fallthrough
		default:
			h.attrs = ""
			return scanText
		}
	}
}

// scanAttribute scans a named attribute
func scanAttribute(h *highlighter) stateFn {
	start := h.pos - h.width
	for {
		h.next()
		switch {
		case h.r == eof:
			return nil
		case unicode.IsLetter(h.r):
			// continue
		default:
			if a, ok := attrs[h.s[start:h.pos-h.width]]; ok {
				h.attrs += a
			}
			return scanHighlight
		}
	}
}

// scanColor256 scans a 256 color attribute
func scanColor256(h *highlighter, pre string) stateFn {
	h.next()
	if h.r != 'g' {
		h.width++ // start at f/b
		return scanAttribute
	}
	h.next()
	if !unicode.IsNumber(h.r) {
		h.width += 2 // start at (f/b)g
		return scanAttribute
	}
	start := h.pos - h.width
	for {
		h.next()
		switch {
		case h.r == eof:
			return nil
		case unicode.IsNumber(h.r):
			// continue
		default:
			h.attrs += pre + h.s[start:h.pos-h.width]
			return scanHighlight
		}
	}
}
<commit_msg>simplified next()<commit_after>package color

import (
	"unicode"
	"unicode/utf8"
)

const eof = -1

// stateFn represents the state of the scanner as a function that returns the next state.
type stateFn func(*highlighter) stateFn

// highlighter holds the state of the scanner.
type highlighter struct {
	s     string // string being scanned
	pos   int    // position in s
	width int    // width of last rune read from s
	start int    // start position of current verb
	r     rune   // current rune
	attrs string // attributes of current highlight verb
}

// Highlight replaces the highlight verbs in s with their appropriate
// control sequences and then returns the resulting string
func Highlight(s string) string {
	h := &highlighter{s: s}
	h.run()
	return h.s
}

// run runs the state machine for the highlighter.
func (h *highlighter) run() {
	h.next()
	for state := scanText; state != nil; {
		state = state(h)
	}
}

// next gets the next rune in the string.
func (h *highlighter) next() {
	if h.pos >= len(h.s) {
		h.r = eof
		return
	}
	h.r, h.width = utf8.DecodeRuneInString(h.s[h.pos:])
	h.pos += h.width
}

// replaces the verb with a control sequence derived from h.attrs[1:].
func (h *highlighter) replace() {
	h.attrs = h.attrs[1:]
	back := h.pos - h.start
	h.s = h.s[:h.pos-back] + csi + h.attrs + "m" + h.s[h.pos:]
	h.pos += len(csi) + len(h.attrs) - back
}

// scans until the next highlight or reset verb.
func scanText(h *highlighter) stateFn {
	for ; ; h.next() {
		switch h.r {
		case eof:
			return nil
		case '%':
			// a verb!
		default:
			continue
		}
		h.next()
		switch h.r {
		case 'r':
			h.start = h.pos - 2
			return verbReset
		case 'h':
			h.start = h.pos - 2
			h.pos++ // skip the [
			h.next()
			return scanHighlight
		case eof:
			return nil
		}
	}
}

// verbReset replaces the reset verb with the reset control sequence.
func verbReset(h *highlighter) stateFn {
	h.attrs = attrs["reset"]
	h.replace()
	return scanText
}

// scanHighlight scans the highlight verb for attributes,
// then replaces it with a control sequence derived from said attributes.
func scanHighlight(h *highlighter) stateFn {
	for ; ; h.next() {
		switch {
		case h.r == eof:
			return nil
		case h.r == 'f':
			return scanColor256(h, preFg256)
		case h.r == 'b':
			return scanColor256(h, preBg256)
		case unicode.IsLetter(h.r):
			return scanAttribute
		case h.r == '+':
			// skip
		case h.r == ']':
			if h.attrs != "" {
				h.replace()
			}
			h.next()
			fallthrough
		default:
			h.attrs = ""
			return scanText
		}
	}
}

// scanAttribute scans a named attribute
func scanAttribute(h *highlighter) stateFn {
	start := h.pos - h.width
	for {
		h.next()
		switch {
		case h.r == eof:
			return nil
		case unicode.IsLetter(h.r):
			// continue
		default:
			if a, ok := attrs[h.s[start:h.pos-h.width]]; ok {
				h.attrs += a
			}
			return scanHighlight
		}
	}
}

// scanColor256 scans a 256 color attribute
func scanColor256(h *highlighter, pre string) stateFn {
	h.next()
	if h.r != 'g' {
		h.width++ // start at f/b
		return scanAttribute
	}
	h.next()
	if !unicode.IsNumber(h.r) {
		h.width += 2 // start at (f/b)g
		return scanAttribute
	}
	start := h.pos - h.width
	for {
		h.next()
		switch {
		case h.r == eof:
			return nil
		case unicode.IsNumber(h.r):
			// continue
		default:
			h.attrs += pre + h.s[start:h.pos-h.width]
			return scanHighlight
		}
	}
}
<|endoftext|>


<commit_before>package smartcrop

import (
	"errors"
	"fmt"
	"image"
	"image/color"
	"image/jpeg"
	_ "image/png"
	"math"
	"os"
	"time"

	"github.com/nfnt/resize"
)

var (
	aspect                  = 0
	cropWidth               = 0.0
	cropHeight              = 0.0
	detailWeight            = 0.2
	skinColor               = [3]float64{0.78, 0.57, 0.44}
	skinBias                = 0.01
	skinBrightnessMin       = 0.2
	skinBrightnessMax       = 1.0
	skinThreshold           = 0.8
	skinWeight              = 1.8
	saturationBrightnessMin = 0.05
	saturationBrightnessMax = 0.9
	saturationThreshold     = 0.4
	saturationBias          = 0.2
	saturationWeight        = 0.3
	// step * minscale rounded down to the next power of two should be good
	scoreDownSample   = 8
	step              = 8
	scaleStep         = 0.1
	minScale          = 0.9
	maxScale          = 1.0
	edgeRadius        = 0.4
	edgeWeight        = -20.0
	outsideImportance = -0.5
	ruleOfThirds      = true
	prescale          = true
	prescalefactor    = 1.0
	debug             = false
)

type Score struct {
	Detail     float64
	Saturation float64
	Skin       float64
	Total      float64
}

type Crop struct {
	X      int
	Y      int
	Width  int
	Height int
	Score  Score
}

func SmartCrop(img *image.Image, width, height int) (Crop, image.Image, error) {
	if width == 0 && height == 0 {
		return Crop{}, nil, errors.New("Expect either a height or width")
	}

	scale := math.Min(float64((*img).Bounds().Size().X)/float64(width), float64((*img).Bounds().Size().Y)/float64(height))

	// resize image for faster processing
	var lowimg image.Image

	if prescale {

		if f := 1 / scale / minScale; f < 1 {
			prescalefactor = f
		}
		fmt.Println(prescalefactor)

		lowimg = resize.Resize(
			uint(float64((*img).Bounds().Size().X)*prescalefactor),
			0,
			*img,
			resize.NearestNeighbor)
		WriteImageToJpeg(&lowimg, "/tmp/prescale.jpg")

	} else {
		lowimg = *img
	}

	cropWidth, cropHeight = chop(float64(width)*scale*prescalefactor), chop(float64(height)*scale*prescalefactor)
	minScale = math.Min(maxScale, math.Max(1.0/scale, minScale))

	fmt.Printf("original resolution: %dx%d\n", (*img).Bounds().Size().X, (*img).Bounds().Size().Y)
	fmt.Printf("scale: %f, cropw: %f, croph: %f, minscale: %f\n", scale, cropWidth, cropHeight, minScale)

	//topCrop := analyse(img)
	topCrop := analyse(&lowimg)
	return topCrop, lowimg, nil
}

func chop(x float64) float64 {
	if x < 0 {
		return math.Ceil(x)
	}
	return math.Floor(x)
}

func thirds(x float64) float64 {
	x = (math.Mod(x-(1.0/3.0)+1.0, 2.0)*0.5 - 0.5) * 16.0
	return math.Max(1.0-x*x, 0.0)
}

func importance(crop *Crop, x, y int) float64 {
	if crop.X > x || x >= crop.X+crop.Width || crop.Y > y || y >= crop.Y+crop.Height {
		return outsideImportance
	}

	xf := float64(x-crop.X) / float64(crop.Width)
	yf := float64(y-crop.Y) / float64(crop.Height)

	px := math.Abs(0.5-xf) * 2.0
	py := math.Abs(0.5-yf) * 2.0

	dx := math.Max(px-1.0+edgeRadius, 0.0)
	dy := math.Max(py-1.0+edgeRadius, 0.0)
	d := (math.Pow(dx, 2) + math.Pow(dy, 2)) * edgeWeight

	s := 1.41 - math.Sqrt(math.Pow(px, 2)+math.Pow(py, 2))
	if ruleOfThirds {
		s += (math.Max(0.0, s+d+0.5) * 1.2) * (thirds(px) + thirds(py))
	}

	return s + d
}

func score(output *image.Image, crop *Crop) Score {
	o := (*output).(*image.RGBA)
	height := (*output).Bounds().Size().Y
	width := (*output).Bounds().Size().X
	score := Score{}

	for y := 0; y < height; y++ {
		yoffset := y * width
		ydownSample := y * scoreDownSample
		for x := 0; x < width; x++ {
			//			now := time.Now()
			imp := importance(crop, x*scoreDownSample, ydownSample)
			//			fmt.Println("Time elapsed single-imp:", time.Since(now))

			p := yoffset + x*4

			r8 := float64(o.Pix[p]) / 255.0
			g8 := float64(o.Pix[p+1]) / 255.0
			b8 := float64(o.Pix[p+2]) / 255.0

			score.Skin += r8 * (g8 + skinBias) * imp
			score.Detail += g8 * imp
			score.Saturation += b8 * (g8 + saturationBias) * imp
		}
	}

	score.Total = (score.Detail*detailWeight + score.Skin*skinWeight + score.Saturation*saturationWeight) / float64(crop.Width) / float64(crop.Height)
	return score
}

func WriteImageToJpeg(img *image.Image, name string) {
	fso, err := os.Create(name)
	if err != nil {
		panic(err)
	}
	defer fso.Close()

	jpeg.Encode(fso, (*img), &jpeg.Options{Quality: 90})
}

func analyse(img *image.Image) Crop {
	o := image.Image(image.NewRGBA((*img).Bounds()))

	now := time.Now()
	edgeDetect(img, &o)
	fmt.Println("Time elapsed edge:", time.Since(now))
	//WriteImageToJpeg(&o, "/tmp/smartcrop_edge.jpg")

	now = time.Now()
	skinDetect(img, &o)
	fmt.Println("Time elapsed skin:", time.Since(now))
	//WriteImageToJpeg(&o, "/tmp/smartcrop_skin.jpg")

	now = time.Now()
	saturationDetect(img, &o)
	fmt.Println("Time elapsed sat:", time.Since(now))
	//WriteImageToJpeg(&o, "/tmp/smartcrop_sat.jpg")

	now = time.Now()
	var topCrop Crop
	topScore := -1.0
	cs := crops(&o)
	fmt.Println("Time elapsed crops:", time.Since(now), len(cs))

	now = time.Now()
	for _, crop := range cs {
		//		nowIn := time.Now()
		crop.Score = score(&o, &crop)
		//		fmt.Println("Time elapsed single-score:", time.Since(nowIn))
		if crop.Score.Total > topScore {
			topCrop = crop
			topScore = crop.Score.Total
		}
	}
	fmt.Println("Time elapsed score:", time.Since(now))

	return topCrop
}

func saturation(c color.Color) float64 {
	r, g, b, _ := c.RGBA()
	r8 := float64(r >> 8)
	g8 := float64(g >> 8)
	b8 := float64(b >> 8)

	maximum := math.Max(math.Max(r8/255.0, g8/255.0), b8/255.0)
	minimum := math.Min(math.Min(r8/255.0, g8/255.0), b8/255.0)

	if maximum == minimum {
		return 0
	}

	l := (maximum + minimum) / 2.0
	d := maximum - minimum

	if l > 0.5 {
		return d / (2.0 - maximum - minimum)
	} else {
		return d / (maximum + minimum)
	}
}

func cie(c color.Color) float64 {
	r, g, b, _ := c.RGBA()
	r8 := float64(r >> 8)
	g8 := float64(g >> 8)
	b8 := float64(b >> 8)

	return 0.5126*b8 + 0.7152*g8 + 0.0722*r8
}

func skinCol(c color.Color) float64 {
	r, g, b, _ := c.RGBA()
	r8 := float64(r >> 8)
	g8 := float64(g >> 8)
	b8 := float64(b >> 8)

	mag := math.Sqrt(math.Pow(r8, 2) + math.Pow(g8, 2) + math.Pow(b8, 2))
	rd := r8/mag - skinColor[0]
	gd := g8/mag - skinColor[1]
	bd := b8/mag - skinColor[2]

	d := math.Sqrt(math.Pow(rd, 2) + math.Pow(gd, 2) + math.Pow(bd, 2))
	return 1.0 - d
}

func edgeDetect(i *image.Image, o *image.Image) {
	w := (*i).Bounds().Size().X
	h := (*i).Bounds().Size().Y

	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			var lightness float64

			if x == 0 || x >= w-1 || y == 0 || y >= h-1 {
				lightness = cie((*i).At(x, y))
			} else {
				lightness = cie((*i).At(x, y))*4.0 -
					cie((*i).At(x, y-1)) -
					cie((*i).At(x-1, y)) -
					cie((*i).At(x+1, y)) -
					cie((*i).At(x, y+1))
			}

			if lightness < 0 {
				continue
			}

			nc := color.RGBA{uint8(lightness), uint8(lightness), uint8(lightness), 255}
			(*o).(*image.RGBA).Set(x, y, nc)
		}
	}
}

func skinDetect(i *image.Image, o *image.Image) {
	w := (*i).Bounds().Size().X
	h := (*i).Bounds().Size().Y

	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			lightness := cie((*i).At(x, y)) / 255.0
			skin := skinCol((*i).At(x, y))

			if skin > skinThreshold && lightness >= skinBrightnessMin && lightness <= skinBrightnessMax {
				r := (skin - skinThreshold) * (255.0 / (1.0 - skinThreshold))
				_, g, b, _ := (*o).At(x, y).RGBA()
				nc := color.RGBA{uint8(r), uint8(g >> 8), uint8(b >> 8), 255}
				(*o).(*image.RGBA).Set(x, y, nc)
			} else {
				_, g, b, _ := (*o).At(x, y).RGBA()
				nc := color.RGBA{0, uint8(g >> 8), uint8(b >> 8), 255}
				(*o).(*image.RGBA).Set(x, y, nc)
			}
		}
	}
}

func saturationDetect(i *image.Image, o *image.Image) {
	w := (*i).Bounds().Size().X
	h := (*i).Bounds().Size().Y

	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			lightness := cie((*i).At(x, y)) / 255.0
			saturation := saturation((*i).At(x, y))

			if saturation > saturationThreshold && lightness >= saturationBrightnessMin && lightness <= saturationBrightnessMax {
				b := (saturation - saturationThreshold) * (255.0 / (1.0 - saturationThreshold))
				r, g, _, _ := (*o).At(x, y).RGBA()
				nc := color.RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b), 255}
				(*o).(*image.RGBA).Set(x, y, nc)
			} else {
				r, g, _, _ := (*o).At(x, y).RGBA()
				nc := color.RGBA{uint8(r >> 8), uint8(g >> 8), 0, 255}
				(*o).(*image.RGBA).Set(x, y, nc)
			}
		}
	}
}

func crops(i *image.Image) []Crop {
	res := []Crop{}
	width := (*i).Bounds().Size().X
	height := (*i).Bounds().Size().Y

	//minDimension := math.Min(float64(width), float64(height))
	cropW := cropWidth  //|| minDimension
	cropH := cropHeight //|| minDimension

	for scale := maxScale; scale >= minScale; scale -= scaleStep {
		for y := 0; float64(y)+cropH*scale <= float64(height); y += step {
			for x := 0; float64(x)+cropW*scale <= float64(width); x += step {
				res = append(res, Crop{
					X:      x,
					Y:      y,
					Width:  int(cropW * scale),
					Height: int(cropH * scale),
				})
			}
		}
	}

	return res
}
<commit_msg>do not downsample<commit_after>package smartcrop

import (
	"errors"
	"fmt"
	"image"
	"image/color"
	"image/jpeg"
	_ "image/png"
	"math"
	"os"
	"time"

	"github.com/nfnt/resize"
)

var (
	aspect                  = 0
	cropWidth               = 0.0
	cropHeight              = 0.0
	detailWeight            = 0.2
	skinColor               = [3]float64{0.78, 0.57, 0.44}
	skinBias                = 0.01
	skinBrightnessMin       = 0.2
	skinBrightnessMax       = 1.0
	skinThreshold           = 0.8
	skinWeight              = 1.8
	saturationBrightnessMin = 0.05
	saturationBrightnessMax = 0.9
	saturationThreshold     = 0.4
	saturationBias          = 0.2
	saturationWeight        = 0.3
	// step * minscale rounded down to the next power of two should be good
	scoreDownSample   = 8
	step              = 8
	scaleStep         = 0.1
	minScale          = 0.9
	maxScale          = 1.0
	edgeRadius        = 0.4
	edgeWeight        = -20.0
	outsideImportance = -0.5
	ruleOfThirds      = true
	prescale          = true
	prescalefactor    = 1.0
	debug             = false
)

type Score struct {
	Detail     float64
	Saturation float64
	Skin       float64
	Total      float64
}

type Crop struct {
	X      int
	Y      int
	Width  int
	Height int
	Score  Score
}

func SmartCrop(img *image.Image, width, height int) (Crop, image.Image, error) {
	if width == 0 && height == 0 {
		return Crop{}, nil, errors.New("Expect either a height or width")
	}

	scale := math.Min(float64((*img).Bounds().Size().X)/float64(width), float64((*img).Bounds().Size().Y)/float64(height))

	// resize image for faster processing
	var lowimg image.Image

	if prescale {

		if f := 1 / scale / minScale; f < 1 {
			prescalefactor = f
		}
		fmt.Println(prescalefactor)

		lowimg = resize.Resize(
			uint(float64((*img).Bounds().Size().X)*prescalefactor),
			0,
			*img,
			resize.NearestNeighbor)
		WriteImageToJpeg(&lowimg, "/tmp/prescale.jpg")

	} else {
		lowimg = *img
	}

	cropWidth, cropHeight = chop(float64(width)*scale*prescalefactor), chop(float64(height)*scale*prescalefactor)
	minScale = math.Min(maxScale, math.Max(1.0/scale, minScale))

	fmt.Printf("original resolution: %dx%d\n", (*img).Bounds().Size().X, (*img).Bounds().Size().Y)
	fmt.Printf("scale: %f, cropw: %f, croph: %f, minscale: %f\n", scale, cropWidth, cropHeight, minScale)

	//topCrop := analyse(img)
	topCrop := analyse(&lowimg)
	return topCrop, lowimg, nil
}

func chop(x float64) float64 {
	if x < 0 {
		return math.Ceil(x)
	}
	return math.Floor(x)
}

func thirds(x float64) float64 {
	x = (math.Mod(x-(1.0/3.0)+1.0, 2.0)*0.5 - 0.5) * 16.0
	return math.Max(1.0-x*x, 0.0)
}

func importance(crop *Crop, x, y int) float64 {
	if crop.X > x || x >= crop.X+crop.Width || crop.Y > y || y >= crop.Y+crop.Height {
		return outsideImportance
	}

	xf := float64(x-crop.X) / float64(crop.Width)
	yf := float64(y-crop.Y) / float64(crop.Height)

	px := math.Abs(0.5-xf) * 2.0
	py := math.Abs(0.5-yf) * 2.0

	dx := math.Max(px-1.0+edgeRadius, 0.0)
	dy := math.Max(py-1.0+edgeRadius, 0.0)
	d := (math.Pow(dx, 2) + math.Pow(dy, 2)) * edgeWeight

	s := 1.41 - math.Sqrt(math.Pow(px, 2)+math.Pow(py, 2))
	if ruleOfThirds {
		s += (math.Max(0.0, s+d+0.5) * 1.2) * (thirds(px) + thirds(py))
	}

	return s + d
}

func score(output *image.Image, crop *Crop) Score {
	height := (*output).Bounds().Size().Y
	width := (*output).Bounds().Size().X
	score := Score{}

	// FIXME DOWNSAMPLING

	for y := 0; y < height; y += 1 {
		for x := 0; x < width; x += 1 {

			r, g, b, _ := (*output).At(x, y).RGBA()

			r8 := float64(r >> 8)
			g8 := float64(g >> 8)
			b8 := float64(b >> 8)

			imp := importance(crop, int(x), int(y))
			det := g8 / 255.0

			score.Skin += r8 / 255.0 * (det + skinBias) * imp
			score.Detail += det * imp
			score.Saturation += b8 / 255.0 * (det + saturationBias) * imp
		}
	}

	score.Total = (score.Detail*detailWeight + score.Skin*skinWeight + score.Saturation*saturationWeight) / float64(crop.Width) / float64(crop.Height)
	return score
}

func WriteImageToJpeg(img *image.Image, name string) {
	fso, err := os.Create(name)
	if err != nil {
		panic(err)
	}
	defer fso.Close()

	jpeg.Encode(fso, (*img), &jpeg.Options{Quality: 90})
}

func analyse(img *image.Image) Crop {
	o := image.Image(image.NewRGBA((*img).Bounds()))

	now := time.Now()
	edgeDetect(img, &o)
	fmt.Println("Time elapsed edge:", time.Since(now))
	//WriteImageToJpeg(&o, "/tmp/smartcrop_edge.jpg")

	now = time.Now()
	skinDetect(img, &o)
	fmt.Println("Time elapsed skin:", time.Since(now))
	//WriteImageToJpeg(&o, "/tmp/smartcrop_skin.jpg")

	now = time.Now()
	saturationDetect(img, &o)
	fmt.Println("Time elapsed sat:", time.Since(now))
	//WriteImageToJpeg(&o, "/tmp/smartcrop_sat.jpg")

	now = time.Now()
	var topCrop Crop
	topScore := -1.0
	cs := crops(&o)
	fmt.Println("Time elapsed crops:", time.Since(now), len(cs))

	now = time.Now()
	for _, crop := range cs {
		//		nowIn := time.Now()
		crop.Score = score(&o, &crop)
		//		fmt.Println("Time elapsed single-score:", time.Since(nowIn))
		if crop.Score.Total > topScore {
			topCrop = crop
			topScore = crop.Score.Total
		}
	}
	fmt.Println("Time elapsed score:", time.Since(now))

	return topCrop
}

func saturation(c color.Color) float64 {
	r, g, b, _ := c.RGBA()
	r8 := float64(r >> 8)
	g8 := float64(g >> 8)
	b8 := float64(b >> 8)

	maximum := math.Max(math.Max(r8/255.0, g8/255.0), b8/255.0)
	minimum := math.Min(math.Min(r8/255.0, g8/255.0), b8/255.0)

	if maximum == minimum {
		return 0
	}

	l := (maximum + minimum) / 2.0
	d := maximum - minimum

	if l > 0.5 {
		return d / (2.0 - maximum - minimum)
	} else {
		return d / (maximum + minimum)
	}
}

func cie(c color.Color) float64 {
	r, g, b, _ := c.RGBA()
	r8 := float64(r >> 8)
	g8 := float64(g >> 8)
	b8 := float64(b >> 8)

	return 0.5126*b8 + 0.7152*g8 + 0.0722*r8
}

func skinCol(c color.Color) float64 {
	r, g, b, _ := c.RGBA()
	r8 := float64(r >> 8)
	g8 := float64(g >> 8)
	b8 := float64(b >> 8)

	mag := math.Sqrt(math.Pow(r8, 2) + math.Pow(g8, 2) + math.Pow(b8, 2))
	rd := r8/mag - skinColor[0]
	gd := g8/mag - skinColor[1]
	bd := b8/mag - skinColor[2]

	d := math.Sqrt(math.Pow(rd, 2) + math.Pow(gd, 2) + math.Pow(bd, 2))
	return 1.0 - d
}

func edgeDetect(i *image.Image, o *image.Image) {
	w := (*i).Bounds().Size().X
	h := (*i).Bounds().Size().Y

	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			var lightness float64

			if x == 0 || x >= w-1 || y == 0 || y >= h-1 {
				lightness = cie((*i).At(x, y))
			} else {
				lightness = cie((*i).At(x, y))*4.0 -
					cie((*i).At(x, y-1)) -
					cie((*i).At(x-1, y)) -
					cie((*i).At(x+1, y)) -
					cie((*i).At(x, y+1))
			}

			if lightness < 0 {
				continue
			}

			nc := color.RGBA{uint8(lightness), uint8(lightness), uint8(lightness), 255}
			(*o).(*image.RGBA).Set(x, y, nc)
		}
	}
}

func skinDetect(i *image.Image, o *image.Image) {
	w := (*i).Bounds().Size().X
	h := (*i).Bounds().Size().Y

	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			lightness := cie((*i).At(x, y)) / 255.0
			skin := skinCol((*i).At(x, y))

			if skin > skinThreshold && lightness >= skinBrightnessMin && lightness <= skinBrightnessMax {
				r := (skin - skinThreshold) * (255.0 / (1.0 - skinThreshold))
				_, g, b, _ := (*o).At(x, y).RGBA()
				nc := color.RGBA{uint8(r), uint8(g >> 8), uint8(b >> 8), 255}
				(*o).(*image.RGBA).Set(x, y, nc)
			} else {
				_, g, b, _ := (*o).At(x, y).RGBA()
				nc := color.RGBA{0, uint8(g >> 8), uint8(b >> 8), 255}
				(*o).(*image.RGBA).Set(x, y, nc)
			}
		}
	}
}

func saturationDetect(i *image.Image, o *image.Image) {
	w := (*i).Bounds().Size().X
	h := (*i).Bounds().Size().Y

	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			lightness := cie((*i).At(x, y)) / 255.0
			saturation := saturation((*i).At(x, y))

			if saturation > saturationThreshold && lightness >= saturationBrightnessMin && lightness <= saturationBrightnessMax {
				b := (saturation - saturationThreshold) * (255.0 / (1.0 - saturationThreshold))
				r, g, _, _ := (*o).At(x, y).RGBA()
				nc := color.RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b), 255}
				(*o).(*image.RGBA).Set(x, y, nc)
			} else {
				r, g, _, _ := (*o).At(x, y).RGBA()
				nc := color.RGBA{uint8(r >> 8), uint8(g >> 8), 0, 255}
				(*o).(*image.RGBA).Set(x, y, nc)
			}
		}
	}
}

func crops(i *image.Image) []Crop {
	res := []Crop{}
	width := (*i).Bounds().Size().X
	height := (*i).Bounds().Size().Y

	//minDimension := math.Min(float64(width), float64(height))
	cropW := cropWidth  //|| minDimension
	cropH := cropHeight //|| minDimension

	for scale := maxScale; scale >= minScale; scale -= scaleStep {
		for y := 0; float64(y)+cropH*scale <= float64(height); y += step {
			for x := 0; float64(x)+cropW*scale <= float64(width); x += step {
				res = append(res, Crop{
					X:      x,
					Y:      y,
					Width:  int(cropW * scale),
					Height: int(cropH * scale),
				})
			}
		}
	}

	return res
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"compress/gzip"
	"fmt"
	td_client "github.com/treasure-data/td-client-go"
	"github.com/ugorji/go/codec"
	"os"
	"strconv"
	"time"
)

func CompressWithGzip(b []byte) []byte {
	retval := bytes.Buffer{}
	w := gzip.NewWriter(&retval)
	w.Write(b)
	w.Close()
	return retval.Bytes()
}

func main() {
	apiKey := os.Getenv("TD_CLIENT_API_KEY")
	client, err := td_client.NewTDClient(td_client.Settings{
		ApiKey: apiKey,
	})
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	status, err := client.ServerStatus()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Printf("status: %s\n", status.Status)
	account, err := client.ShowAccount()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Println("account:")
	fmt.Printf("  id: %d\n", account.Id)
	fmt.Printf("  plan: %d\n", account.Plan)
	fmt.Printf("  storageSize: %d\n", account.StorageSize)
	fmt.Printf("  guaranteedCores: %d\n", account.GuaranteedCores)
	fmt.Printf("  maximumCores: %d\n", account.MaximumCores)
	fmt.Printf("  createdAt: %s\n", account.CreatedAt.Format(time.RFC3339))
	results, err := client.ListResults()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Printf("%d results\n", len(*results))
	databases, err := client.ListDatabases()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Printf("%d databases\n", len(*databases))
	for _, database := range *databases {
		fmt.Printf("  name: %s\n", database.Name)
		tables, err := client.ListTables(database.Name)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		fmt.Printf("  %d tables\n", len(*tables))
		for _, table := range *tables {
			fmt.Printf("    name: %s\n", table.Name)
			fmt.Printf("    type: %s\n", table.Type)
			fmt.Printf("    count: %d\n", table.Count)
			fmt.Printf("    primaryKey: %s\n", table.PrimaryKey)
			fmt.Printf("    schema: %v\n", table.Schema)
		}
	}
	err = client.CreateDatabase("sample_db2", nil)
	if err != nil {
		_err := err.(*td_client.APIError)
		if _err == nil || _err.Type != td_client.AlreadyExistsError {
			fmt.Println(err.Error())
			return
		}
	}
	err = client.CreateLogTable("sample_db2", "test")
	if err != nil {
		_err := err.(*td_client.APIError)
		if _err == nil || _err.Type != td_client.AlreadyExistsError {
			fmt.Println(err.Error())
			return
		}
	} else {
		err = client.UpdateSchema("sample_db2", "test", []interface{}{
			[]string{"a", "string"},
			[]string{"b", "string"},
		})
	}
	data := bytes.Buffer{}
	handle := codec.MsgpackHandle{}
	encoder := codec.NewEncoder(&data, &handle)
	for i := 0; i < 10000; i += 1 {
		encoder.Encode(map[string]interface{}{
			"time": i, "a": strconv.Itoa(i), "b": strconv.Itoa(i),
		})
	}
	payload := CompressWithGzip(data.Bytes())
	fmt.Printf("payloadSize:%d\n", len(payload))
	time_, err := client.Import("sample_db2", "test", "msgpack.gz", (td_client.InMemoryBlob)(payload), "")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Printf("elapsed time:%g\n", time_)
	jobId, err := client.SubmitQuery("sample_db2", td_client.Query{
		Type:       "hive",
		Query:      "SELECT COUNT(*) AS c FROM test WHERE a >= 5000",
		ResultUrl:  "",
		Priority:   0,
		RetryLimit: 0,
	})
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Printf("jobId:%s\n", jobId)
	for {
		status, err := client.JobStatus(jobId)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		fmt.Printf("jobStatus:%s\n", status)
		if status != "queued" {
			break
		}
		time.Sleep(1000000000)
	}
	{
		jobDesc, err := client.ShowJob(jobId)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		fmt.Printf("query:%s\n", jobDesc.Query)
		fmt.Printf("debug.cmdOut:%s\n", jobDesc.Debug.CmdOut)
		fmt.Printf("debug.stdErr:%s\n", jobDesc.Debug.StdErr)
		fmt.Printf("url:%s\n", jobDesc.Url)
		fmt.Printf("startAt:%s\n", jobDesc.StartAt.String())
		fmt.Printf("endAt:%s\n", jobDesc.EndAt.String())
		fmt.Printf("cpuTime:%g\n", jobDesc.CpuTime)
		fmt.Printf("resultSize:%d\n", jobDesc.ResultSize)
		fmt.Printf("priority:%d\n", jobDesc.Priority)
		fmt.Printf("hiveResultSchema:%v\n", jobDesc.HiveResultSchema)
	}
	for {
		time.Sleep(1000000000)
		status, err := client.JobStatus(jobId)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		fmt.Printf("jobStatus:%s\n", status)
		if status != "queued" && status != "running" {
			break
		}
	}
	{
		jobDesc, err := client.ShowJob(jobId)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		fmt.Printf("query:%s\n", jobDesc.Query)
		fmt.Printf("debug.cmdOut:%s\n", jobDesc.Debug.CmdOut)
		fmt.Printf("debug.stdErr:%s\n", jobDesc.Debug.StdErr)
		fmt.Printf("url:%s\n", jobDesc.Url)
		fmt.Printf("startAt:%s\n", jobDesc.StartAt.String())
		fmt.Printf("endAt:%s\n", jobDesc.EndAt.String())
		fmt.Printf("cpuTime:%g\n", jobDesc.CpuTime)
		fmt.Printf("resultSize:%d\n", jobDesc.ResultSize)
		fmt.Printf("priority:%d\n", jobDesc.Priority)
		fmt.Printf("hiveResultSchema:%v\n", jobDesc.HiveResultSchema)
	}
	err = client.JobResultEach(jobId, func(v interface{}) error {
		fmt.Printf("Result:%v\n", v)
		return nil
	})
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	type_, err := client.DeleteTable("sample_db2", "test")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Printf("deleteTable result: %s\n", type_)
	err = client.DeleteDatabase("sample_db2")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
}
<commit_msg>Missing error handling in the example code.<commit_after>package main

import (
	"bytes"
	"compress/gzip"
	"fmt"
	td_client "github.com/treasure-data/td-client-go"
	"github.com/ugorji/go/codec"
	"os"
	"strconv"
	"time"
)

func CompressWithGzip(b []byte) []byte {
	retval := bytes.Buffer{}
	w := gzip.NewWriter(&retval)
	w.Write(b)
	w.Close()
	return retval.Bytes()
}

func main() {
	apiKey := os.Getenv("TD_CLIENT_API_KEY")
	client, err := td_client.NewTDClient(td_client.Settings{
		ApiKey: apiKey,
	})
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	status, err := client.ServerStatus()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Printf("status: %s\n", status.Status)
	account, err := client.ShowAccount()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Println("account:")
	fmt.Printf("  id: %d\n", account.Id)
	fmt.Printf("  plan: %d\n", account.Plan)
	fmt.Printf("  storageSize: %d\n", account.StorageSize)
	fmt.Printf("  guaranteedCores: %d\n", account.GuaranteedCores)
	fmt.Printf("  maximumCores: %d\n", account.MaximumCores)
	fmt.Printf("  createdAt: %s\n", account.CreatedAt.Format(time.RFC3339))
	results, err := client.ListResults()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Printf("%d results\n", len(*results))
	databases, err := client.ListDatabases()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Printf("%d databases\n", len(*databases))
	for _, database := range *databases {
		fmt.Printf("  name: %s\n", database.Name)
		tables, err := client.ListTables(database.Name)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		fmt.Printf("  %d tables\n", len(*tables))
		for _, table := range *tables {
			fmt.Printf("    name: %s\n", table.Name)
			fmt.Printf("    type: %s\n", table.Type)
			fmt.Printf("    count: %d\n", table.Count)
			fmt.Printf("    primaryKey: %s\n", table.PrimaryKey)
			fmt.Printf("    schema: %v\n", table.Schema)
		}
	}
	err = client.CreateDatabase("sample_db2", nil)
	if err != nil {
		_err := err.(*td_client.APIError)
		if _err == nil || _err.Type != td_client.AlreadyExistsError {
			fmt.Println(err.Error())
			return
		}
	}
	err = client.CreateLogTable("sample_db2", "test")
	if err != nil {
		_err := err.(*td_client.APIError)
		if _err == nil || _err.Type != td_client.AlreadyExistsError {
			fmt.Println(err.Error())
			return
		}
	} else {
		err = client.UpdateSchema("sample_db2", "test", []interface{}{
			[]string{"a", "string"},
			[]string{"b", "string"},
		})
		if err != nil {
			fmt.Println(err.Error())
			return
		}
	}
	data := bytes.Buffer{}
	handle := codec.MsgpackHandle{}
	encoder := codec.NewEncoder(&data, &handle)
	for i := 0; i < 10000; i += 1 {
		encoder.Encode(map[string]interface{}{
			"time": i, "a": strconv.Itoa(i), "b": strconv.Itoa(i),
		})
	}
	payload := CompressWithGzip(data.Bytes())
	fmt.Printf("payloadSize:%d\n", len(payload))
	time_, err := client.Import("sample_db2", "test", "msgpack.gz", (td_client.InMemoryBlob)(payload), "")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Printf("elapsed time:%g\n", time_)
	jobId, err := client.SubmitQuery("sample_db2", td_client.Query{
		Type:       "hive",
		Query:      "SELECT COUNT(*) AS c FROM test WHERE a >= 5000",
		ResultUrl:  "",
		Priority:   0,
		RetryLimit: 0,
	})
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Printf("jobId:%s\n", jobId)
	for {
		status, err := client.JobStatus(jobId)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		fmt.Printf("jobStatus:%s\n", status)
		if status != "queued" {
			break
		}
		time.Sleep(1000000000)
	}
	{
		jobDesc, err := client.ShowJob(jobId)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		fmt.Printf("query:%s\n", jobDesc.Query)
		fmt.Printf("debug.cmdOut:%s\n", jobDesc.Debug.CmdOut)
		fmt.Printf("debug.stdErr:%s\n", jobDesc.Debug.StdErr)
		fmt.Printf("url:%s\n", jobDesc.Url)
		fmt.Printf("startAt:%s\n", jobDesc.StartAt.String())
		fmt.Printf("endAt:%s\n", jobDesc.EndAt.String())
		fmt.Printf("cpuTime:%g\n", jobDesc.CpuTime)
		fmt.Printf("resultSize:%d\n", jobDesc.ResultSize)
		fmt.Printf("priority:%d\n", jobDesc.Priority)
		fmt.Printf("hiveResultSchema:%v\n", jobDesc.HiveResultSchema)
	}
	for {
		time.Sleep(1000000000)
		status, err := client.JobStatus(jobId)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		fmt.Printf("jobStatus:%s\n", status)
		if status != "queued" && status != "running" {
			break
		}
	}
	{
		jobDesc, err := client.ShowJob(jobId)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		fmt.Printf("query:%s\n", jobDesc.Query)
		fmt.Printf("debug.cmdOut:%s\n", jobDesc.Debug.CmdOut)
		fmt.Printf("debug.stdErr:%s\n", jobDesc.Debug.StdErr)
		fmt.Printf("url:%s\n", jobDesc.Url)
		fmt.Printf("startAt:%s\n", jobDesc.StartAt.String())
		fmt.Printf("endAt:%s\n", jobDesc.EndAt.String())
		fmt.Printf("cpuTime:%g\n", jobDesc.CpuTime)
		fmt.Printf("resultSize:%d\n", jobDesc.ResultSize)
		fmt.Printf("priority:%d\n", jobDesc.Priority)
		fmt.Printf("hiveResultSchema:%v\n", jobDesc.HiveResultSchema)
	}
	err = client.JobResultEach(jobId, func(v interface{}) error {
		fmt.Printf("Result:%v\n", v)
		return nil
	})
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	type_, err := client.DeleteTable("sample_db2", "test")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Printf("deleteTable result: %s\n", type_)
	err = client.DeleteDatabase("sample_db2")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
}
<|endoftext|>


<commit_before>// Copyright 2016 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package wav

import (
	"bytes"
	"fmt"
	"io"

	"github.com/hajimehoshi/ebiten/exp/audio"
)

type Stream struct {
	src        audio.ReadSeekCloser
	headerSize int64
	dataSize   int64
}

func (s *Stream) Read(p []byte) (int, error) {
	return s.src.Read(p)
}

func (s *Stream) Seek(offset int64, whence int) (int64, error) {
	return s.src.Seek(offset+s.headerSize, whence)
}

func (s *Stream) Close() error {
	return s.src.Close()
}

func (s *Stream) Size() int64 {
	return s.dataSize
}

func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
	buf := make([]byte, 12)
	n, err := io.ReadFull(src, buf)
	if n != len(buf) {
		return nil, fmt.Errorf("wav: invalid header")
	}
	if err != nil {
		return nil, err
	}
	if !bytes.Equal(buf[0:4], []byte("RIFF")) {
		return nil, fmt.Errorf("wav: invalid header: 'RIFF' not found")
	}
	if !bytes.Equal(buf[8:12], []byte("WAVE")) {
		return nil, fmt.Errorf("wav: invalid header: 'WAVE' not found")
	}

	// Read chunks
	dataSize := int64(0)
	headerSize := int64(0)
chunks:
	for {
		buf := make([]byte, 8)
		n, err := io.ReadFull(src, buf)
		if n != len(buf) {
			return nil, fmt.Errorf("wav: invalid header")
		}
		if err != nil {
			return nil, err
		}
		headerSize += 8
		size := int64(buf[4]) | int64(buf[5])<<8 | int64(buf[6])<<16 | int64(buf[7]<<24)
		switch {
		case bytes.Equal(buf[0:4], []byte("fmt ")):
			if size != 16 {
				return nil, fmt.Errorf("wav: invalid header: maybe non-PCM file?")
			}
			buf := make([]byte, size)
			n, err := io.ReadFull(src, buf)
			if n != len(buf) {
				return nil, fmt.Errorf("wav: invalid header")
			}
			if err != nil {
				return nil, err
			}
			// TODO: Remove this magic number
			if buf[2] != 2 {
				return nil, fmt.Errorf("wav: invalid header: channel num must be 2")
			}
			// TODO: Remove this magic number
			if buf[14] != 16 {
				return nil, fmt.Errorf("wav: invalid header: depth must be 16")
			}
			sampleRate := int64(buf[4]) | int64(buf[5])<<8 | int64(buf[6])<<16 | int64(buf[7]<<24)
			if int64(context.SampleRate()) != sampleRate {
				return nil, fmt.Errorf("wav: sample rate must be %d but %d", context.SampleRate(), sampleRate)
			}
			headerSize += size
		case bytes.Equal(buf[0:4], []byte("data")):
			dataSize = size
			break chunks
		default:
			buf := make([]byte, size)
			n, err := io.ReadFull(src, buf)
			if n != len(buf) {
				return nil, fmt.Errorf("wav: invalid header")
			}
			if err != nil {
				return nil, err
			}
			headerSize += size
		}
	}
	s := &Stream{
		src:        src,
		headerSize: headerSize,
		dataSize:   dataSize,
	}
	return s, nil
}
<commit_msg>audio/wav: Bug fix: add header size to offset only when whence is 0<commit_after>// Copyright 2016 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package wav

import (
	"bytes"
	"fmt"
	"io"

	"github.com/hajimehoshi/ebiten/exp/audio"
)

type Stream struct {
	src        audio.ReadSeekCloser
	headerSize int64
	dataSize   int64
}

func (s *Stream) Read(p []byte) (int, error) {
	return s.src.Read(p)
}

func (s *Stream) Seek(offset int64, whence int) (int64, error) {
	if whence == 0 {
		offset += s.headerSize
	}
	return s.src.Seek(offset, whence)
}

func (s *Stream) Close() error {
	return s.src.Close()
}

func (s *Stream) Size() int64 {
	return s.dataSize
}

func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
	buf := make([]byte, 12)
	n, err := io.ReadFull(src, buf)
	if n != len(buf) {
		return nil, fmt.Errorf("wav: invalid header")
	}
	if err != nil {
		return nil, err
	}
	if !bytes.Equal(buf[0:4], []byte("RIFF")) {
		return nil, fmt.Errorf("wav: invalid header: 'RIFF' not found")
	}
	if !bytes.Equal(buf[8:12], []byte("WAVE")) {
		return nil, fmt.Errorf("wav: invalid header: 'WAVE' not found")
	}

	// Read chunks
	dataSize := int64(0)
	headerSize := int64(0)
chunks:
	for {
		buf := make([]byte, 8)
		n, err := io.ReadFull(src, buf)
		if n != len(buf) {
			return nil, fmt.Errorf("wav: invalid header")
		}
		if err != nil {
			return nil, err
		}
		headerSize += 8
		size := int64(buf[4]) | int64(buf[5])<<8 | int64(buf[6])<<16 | int64(buf[7]<<24)
		switch {
		case bytes.Equal(buf[0:4], []byte("fmt ")):
			if size != 16 {
				return nil, fmt.Errorf("wav: invalid header: maybe non-PCM file?")
			}
			buf := make([]byte, size)
			n, err := io.ReadFull(src, buf)
			if n != len(buf) {
				return nil, fmt.Errorf("wav: invalid header")
			}
			if err != nil {
				return nil, err
			}
			// TODO: Remove this magic number
			if buf[2] != 2 {
				return nil, fmt.Errorf("wav: invalid header: channel num must be 2")
			}
			// TODO: Remove this magic number
			if buf[14] != 16 {
				return nil, fmt.Errorf("wav: invalid header: depth must be 16")
			}
			sampleRate := int64(buf[4]) | int64(buf[5])<<8 | int64(buf[6])<<16 | int64(buf[7]<<24)
			if int64(context.SampleRate()) != sampleRate {
				return nil, fmt.Errorf("wav: sample rate must be %d but %d", context.SampleRate(), sampleRate)
			}
			headerSize += size
		case bytes.Equal(buf[0:4], []byte("data")):
			dataSize = size
			break chunks
		default:
			buf := make([]byte, size)
			n, err := io.ReadFull(src, buf)
			if n != len(buf) {
				return nil, fmt.Errorf("wav: invalid header")
			}
			if err != nil {
				return nil, err
			}
			headerSize += size
		}
	}
	s := &Stream{
		src:        src,
		headerSize: headerSize,
		dataSize:   dataSize,
	}
	return s, nil
}
<|endoftext|>


<commit_before>package bsw

import (
	"errors"
	"github.com/miekg/dns"
)

func GetWildCard(domain, serverAddr string) string {
	var fqdn = "youmustconstructmorepylons." + domain
	m := &dns.Msg{}
	m.SetQuestion(dns.Fqdn(fqdn), dns.TypeA)
	in, err := dns.Exchange(m, serverAddr+":53")
	if err != nil {
		return ""
	}
	if len(in.Answer) < 1 {
		return ""
	}
	if a, ok := in.Answer[0].(*dns.A); ok {
		return a.A.String()
	} else {
		return ""
	}
}

func GetWildCard6(domain, serverAddr string) string {
	var fqdn = "youmustconstructmorepylons." + domain
	m := &dns.Msg{}
	m.SetQuestion(dns.Fqdn(fqdn), dns.TypeAAAA)
	in, err := dns.Exchange(m, serverAddr+":53")
	if err != nil {
		return ""
	}
	if len(in.Answer) < 1 {
		return ""
	}
	if a, ok := in.Answer[0].(*dns.AAAA); ok {
		return a.AAAA.String()
	} else {
		return ""
	}
}

func Dictionary(domain, subname, blacklist, serverAddr string) (Results, error) {
	results := Results{}
	var fqdn = subname + "." + domain
	ip, err := LookupName(fqdn, serverAddr)
	if err != nil {
		cfqdn, err := LookupCname(fqdn, serverAddr)
		if err != nil {
			return results, err
		}
		ip, err = LookupName(cfqdn, serverAddr)
		if err != nil {
			return results, err
		}
		if ip == blacklist {
			return results, errors.New("Returned IP in blackslist")
		}
		results = append(results, Result{Source: "Dictionary-CNAME", IP: ip, Hostname: fqdn}, Result{Source: "Dictionary-CNAME", IP: ip, Hostname: cfqdn})
		return results, nil
	}
	if ip == blacklist {
		return results, errors.New("Returned IP in blacklist")
	}
	results = append(results, Result{Source: "Dictionary", IP: ip, Hostname: fqdn})
	return results, nil
}

func Dictionary6(domain, subname, blacklist, serverAddr string) (Results, error) {
	results := Results{}
	var fqdn = subname + "." + domain
	ip, err := LookupName6(fqdn, serverAddr)
	if err != nil {
		return results, err
	}
	if ip == blacklist {
		return results, errors.New("Returned IP in blacklist")
	}
	results = append(results, Result{Source: "Dictionary IPv6", IP: ip, Hostname: fqdn})
	return results, nil
}
<commit_msg>Comment dictionary functionality<commit_after>package bsw

import (
	"errors"
	"github.com/miekg/dns"
)

// Search for a possible wild card host by attempting to
// get an A record youmustconstructmoreplylong.[domain].
func GetWildCard(domain, serverAddr string) string {
	var fqdn = "youmustconstructmorepylons." + domain
	m := &dns.Msg{}
	m.SetQuestion(dns.Fqdn(fqdn), dns.TypeA)
	in, err := dns.Exchange(m, serverAddr+":53")
	if err != nil {
		return ""
	}
	if len(in.Answer) < 1 {
		return ""
	}
	if a, ok := in.Answer[0].(*dns.A); ok {
		return a.A.String()
	} else {
		return ""
	}
}

// Search for a possible wild card host by attempting to
// get an AAAA record youmustconstructmoreplylong.[domain].
func GetWildCard6(domain, serverAddr string) string {
	var fqdn = "youmustconstructmorepylons." + domain
	m := &dns.Msg{}
	m.SetQuestion(dns.Fqdn(fqdn), dns.TypeAAAA)
	in, err := dns.Exchange(m, serverAddr+":53")
	if err != nil {
		return ""
	}
	if len(in.Answer) < 1 {
		return ""
	}
	if a, ok := in.Answer[0].(*dns.AAAA); ok {
		return a.AAAA.String()
	} else {
		return ""
	}
}

// Attempt to get an A and CNAME record for a sub domain of domain.
func Dictionary(domain, subname, blacklist, serverAddr string) (Results, error) {
	results := Results{}
	var fqdn = subname + "." + domain
	ip, err := LookupName(fqdn, serverAddr)
	if err != nil {
		cfqdn, err := LookupCname(fqdn, serverAddr)
		if err != nil {
			return results, err
		}
		ip, err = LookupName(cfqdn, serverAddr)
		if err != nil {
			return results, err
		}
		if ip == blacklist {
			return results, errors.New("Returned IP in blackslist")
		}
		results = append(results, Result{Source: "Dictionary-CNAME", IP: ip, Hostname: fqdn}, Result{Source: "Dictionary-CNAME", IP: ip, Hostname: cfqdn})
		return results, nil
	}
	if ip == blacklist {
		return results, errors.New("Returned IP in blacklist")
	}
	results = append(results, Result{Source: "Dictionary", IP: ip, Hostname: fqdn})
	return results, nil
}

// Attempt to get an AAAA record for a sub domain of a domain.
func Dictionary6(domain, subname, blacklist, serverAddr string) (Results, error) {
	results := Results{}
	var fqdn = subname + "." + domain
	ip, err := LookupName6(fqdn, serverAddr)
	if err != nil {
		return results, err
	}
	if ip == blacklist {
		return results, errors.New("Returned IP in blacklist")
	}
	results = append(results, Result{Source: "Dictionary IPv6", IP: ip, Hostname: fqdn})
	return results, nil
}
<|endoftext|>


<commit_before>package hmutil

import (
	// "log"
	// "os/exec"
	"strings"
	"fmt"
	"os"
	"path/filepath"
	"bytes"
	"io/ioutil"
	"io"
	"crypto/aes"
	"crypto/cipher"
	"compress/gzip"
	"archive/tar"

	"github.com/rlmcpherson/s3gof3r"
	"github.com/Twizty/scp-go/scp"
	"github.com/dutchcoders/goftp"
)

// func System(cmd string) ([]byte, error) {
// 	log.Println(cmd)
// 	return exec.Command("sh", "-c", cmd).CombinedOutput()
// }

func ReplaceVars(str string, replacements map[string]string) string {
	for from, to := range replacements {
		str = strings.Replace(str, from, to, -1)
	}
	return str
}

func find(prefix string, arr []string, elem string) int {
	for i, e := range arr {
		if filepath.Join(prefix, e) == elem {
			return i
		}
	}
	return -1
}

func removeFromSlice(prefix string, arr []string, elem string) ([]string, bool) {
	index := find(prefix, arr, elem)

	if index == -1 {
		return arr, false
	} else {
		return append(arr[:index], arr[index+1:]...), true
	}
}

func removeExcludedFiles(dir string, files, excludeFiles []string) {
	var changed bool
	var deleted []string = []string{}

	for _, e := range excludeFiles {
		files, changed = removeFromSlice(dir, files, e)
		if changed {
			deleted = append(deleted, e)
		}
	}
	for _, e := range deleted {
		removeFromSlice("", excludeFiles, e)
	}
}

func Tar(dir string, files, excludeFiles []string, tw *tar.Writer) error {
	if len(files) == 1 && files[0] == "*" {
		files = []string{}
		d, _err := ioutil.ReadDir(dir)

		if _err != nil {
			return _err
		}

		for _, e := range d {
			files = append(files, e.Name())
		}
	}

	// for _, e := range excludeFiles {
	// 	fmt.Println(filpath.Join(dir, e))
	// 	files = removeFromSlice(files, e)
	// 	excludeFiles = removeFromSlice(excludeFiles, e)
	// }

	removeExcludedFiles(dir, files, excludeFiles)

	for _, file := range files {
		f, _err := os.Open(filepath.Join(dir, file))
		if _err != nil {
			continue
		}

		s, _err := f.Stat()

		if s.IsDir() {
			Tar(filepath.Join(dir, file), []string{"*"}, excludeFiles, tw)
		} else {
			if _err != nil {
				return _err
			}

			header := &tar.Header{
				Name: filepath.Join(dir, s.Name()),
				Size: s.Size(),
				Mode: 0777,
			}

			if _err = tw.WriteHeader(header); _err != nil {
				return _err
			}

			buffer := make([]byte, s.Size())
			buffer, _err = ioutil.ReadFile(filepath.Join(dir, s.Name())) // file.Read(buffer)
			if _err != nil {
				return _err
			}

			if _, _err = tw.Write(buffer); _err != nil {
				return _err
			}
		}
	}

	return nil
}

// func Gzip(pathToFile string, buffer *bytes.Buffer) {
// 	gzFile, _err := os.Create(pathToFile)
// 	handleError(_err)

// 	gzWriter := gzip.NewWriter(gzFile)
// 	_, _err = gzWriter.Write(buffer.Bytes())
// 	handleError(_err)

// 	gzWriter.Close()
// }

func Gzip(buffer *bytes.Buffer) (bytes.Buffer, error) {
	var gzFile bytes.Buffer
	gzWriter := gzip.NewWriter(&gzFile)
	_, _err := gzWriter.Write(buffer.Bytes())
	gzWriter.Flush()
	gzWriter.Close()
	if _err != nil {
		return bytes.Buffer{}, _err
	}

	return gzFile, nil
}

func Encode(buffer *bytes.Buffer, encodeKey []byte) (bytes.Buffer, error) {
	var outbuffer bytes.Buffer

	block, _err := aes.NewCipher(encodeKey)
	if _err != nil {
		return bytes.Buffer{}, _err
	}

	var iv [aes.BlockSize]byte
	stream := cipher.NewOFB(block, iv[:])

	writer := &cipher.StreamWriter{S: stream, W: &outbuffer}
	defer writer.Close()
	if _, _err = io.Copy(writer, buffer); _err != nil {
		return bytes.Buffer{}, _err
	}

	return outbuffer, nil
}

func WriteToFile(path string, buffer bytes.Buffer) error {
	file, _err := os.Create(path)
	defer file.Close()
	if _err != nil {
		return _err
	}
	file.Write(buffer.Bytes())
	return nil
}

func PackAndCompress(dir string, files, excludeFiles []string, outputFile string, key []byte, encrypt bool) error {
	outdir, _ := filepath.Split(outputFile)
	_err := os.MkdirAll(outdir, 0777)
	if _err != nil {
		return _err
	}

	var tarFile bytes.Buffer

	tarWriter := tar.NewWriter(&tarFile)
	_err = Tar(dir, files, excludeFiles, tarWriter)

	if _err != nil {
		return _err
	}

	tarWriter.Close()
	gzipedBuffer, _err := Gzip(&tarFile)

	if _err != nil {
		return _err
	}

	if encrypt {
		encryptedBuffer, _err := Encode(&gzipedBuffer, key)
		if _err != nil {
			return _err
		}
		return WriteToFile(outputFile + ".encrypted", encryptedBuffer)
	} else {
		return WriteToFile(outputFile, gzipedBuffer)
	}
}

func handleError(e error) {
	if e != nil {
		panic(e)
	}
}

func UploadToS3(k s3gof3r.Keys, bucketName string, pathToFile string) error {
	s3 := s3gof3r.New("", k)
	b := s3.Bucket(bucketName)

	file, err := os.Open(pathToFile)
	if err != nil {
		return err
	}

	stats, _ := file.Stat()

	w, err := b.PutWriter(stats.Name(), nil, nil)
	if err != nil {
		return err
	}

	if _, err = io.Copy(w, file); err != nil { // Copy into S3
		return err
	}

	if err = w.Close(); err != nil {
		return err
	}

	return nil
}

func DownloadFromS3(k s3gof3r.Keys, bucketName, filename string) (bytes.Buffer, error) {
	s3 := s3gof3r.New("", k)
	b := s3.Bucket(bucketName)

	r, _, err := b.GetReader(filename, nil)
	if err != nil {
		return bytes.Buffer{}, err
	}

	// outputFile, err := os.Create(filepath.Join(outputDirPath, filename))
	// if err != nil {
	// 	return nil, err
	// }

	var output bytes.Buffer

	if _, err = io.Copy(&output, r); err != nil {
		return bytes.Buffer{}, err
	}

	err = r.Close()
	if err != nil {
		return bytes.Buffer{}, err
	}

	return output, nil
}

func SSHUploader(port int, id_rsa, host, user, srcFile, dstFile string) *scp.SecureCopier {
	return scp.NewSecureCopier(port, false, false, false, true, false, false, id_rsa, "", "", srcFile, host, user, dstFile)
}

func SSHDownloader(port int, id_rsa, host, user, srcFile, dstFile string) *scp.SecureCopier {
	return scp.NewSecureCopier(port, false, false, false, true, false, false, id_rsa, host, user, srcFile, "", "", dstFile)
}

func SSHExec(ssh *scp.SecureCopier) (bytes.Buffer, error) {
	var o, e, result bytes.Buffer
	err, _ := ssh.Exec(nil, &o, &e, &result)
	return result, err
}

func prepareFTPConn(port int, host, login, password string) (*goftp.FTP, error) {
	var ftp *goftp.FTP
	var err error

	if ftp, err = goftp.Connect(fmt.Sprintf("%s:%d", host, port)); err != nil {
		return nil, err
	}

	if err = ftp.Login(login, password); err != nil {
		return nil, err
	}

	return ftp, nil
}

func FTPUpload(port int, host, login, password, pathToFile, pathToRemoteFile string) error {
	ftp, err := prepareFTPConn(port, host, login, password)
	if err != nil {
		return err
	}

	file, err := os.Open(pathToFile)
	if err != nil {
		return err
	}

	if err = ftp.Stor(pathToRemoteFile, file); err != nil {
		return err
	}

	return ftp.Quit()
}

func FTPDownload(port int, host, login, password, pathToRemoteFile string) (bytes.Buffer, error) {
	ftp, err := prepareFTPConn(port, host, login, password)
	if err != nil {
		return bytes.Buffer{}, err
	}

	var buffer bytes.Buffer

	_, err = ftp.Retr(pathToRemoteFile, func(r io.Reader) error {
		// file, _err := os.Create(pathToFile)
		// if _err != nil {
		// 	return _err
		// }

		if _, _err := io.Copy(&buffer, r); _err != nil {
			return _err
		}

		return nil
	})

	if err != nil {
		return bytes.Buffer{}, err
	}

	if err = ftp.Quit(); err != nil {
		return buffer, err
	}

	return buffer, nil
}

func Decode(buffer *bytes.Buffer, key []byte) (bytes.Buffer, error) {
	block, err := aes.NewCipher(key)
  if err != nil {
    return bytes.Buffer{}, err
  }

  var iv [aes.BlockSize]byte
  stream := cipher.NewOFB(block, iv[:])

  var outbuffer bytes.Buffer
  reader := &cipher.StreamReader{S: stream, R: buffer}

  if _, err = io.Copy(&outbuffer, reader); err != nil {
    return bytes.Buffer{}, err
  }

  return outbuffer, nil
}

/*
func ErrString(err error) (s *string) {
	if err != nil {
		tmp := err.Error()
		s = &tmp
	}
	return
}
*/
<commit_msg>Fix file headers<commit_after>package hmutil

import (
	// "log"
	// "os/exec"
	"strings"
	"fmt"
	"os"
	"path/filepath"
	"bytes"
	"io/ioutil"
	"io"
	"crypto/aes"
	"crypto/cipher"
	"compress/gzip"
	"archive/tar"

	"github.com/rlmcpherson/s3gof3r"
	"github.com/Twizty/scp-go/scp"
	"github.com/dutchcoders/goftp"
)

// func System(cmd string) ([]byte, error) {
// 	log.Println(cmd)
// 	return exec.Command("sh", "-c", cmd).CombinedOutput()
// }

func ReplaceVars(str string, replacements map[string]string) string {
	for from, to := range replacements {
		str = strings.Replace(str, from, to, -1)
	}
	return str
}

func find(prefix string, arr []string, elem string) int {
	for i, e := range arr {
		if filepath.Join(prefix, e) == elem {
			return i
		}
	}
	return -1
}

func removeFromSlice(prefix string, arr []string, elem string) ([]string, bool) {
	index := find(prefix, arr, elem)

	if index == -1 {
		return arr, false
	} else {
		return append(arr[:index], arr[index+1:]...), true
	}
}

func removeExcludedFiles(dir string, files, excludeFiles []string) {
	var changed bool
	var deleted []string = []string{}

	for _, e := range excludeFiles {
		files, changed = removeFromSlice(dir, files, e)
		if changed {
			deleted = append(deleted, e)
		}
	}
	for _, e := range deleted {
		removeFromSlice("", excludeFiles, e)
	}
}

func Tar(dir string, files, excludeFiles []string, tw *tar.Writer, prevdir string) error {
	if len(files) == 1 && files[0] == "*" {
		files = []string{}
		d, _err := ioutil.ReadDir(dir)

		if _err != nil {
			return _err
		}

		for _, e := range d {
			files = append(files, e.Name())
		}
	}

	// for _, e := range excludeFiles {
	// 	fmt.Println(filpath.Join(dir, e))
	// 	files = removeFromSlice(files, e)
	// 	excludeFiles = removeFromSlice(excludeFiles, e)
	// }

	removeExcludedFiles(dir, files, excludeFiles)

	for _, file := range files {
		f, _err := os.Open(filepath.Join(dir, file))
		if _err != nil {
			continue
		}

		s, _err := f.Stat()

		if s.IsDir() {
			Tar(filepath.Join(dir, file), []string{"*"}, excludeFiles, tw, filepath.Join(prevdir, file))
		} else {
			if _err != nil {
				return _err
			}

			header := &tar.Header{
				Name: filepath.Join(prevdir, s.Name()),
				Size: s.Size(),
				Mode: 0777,
			}

			if _err = tw.WriteHeader(header); _err != nil {
				return _err
			}

			buffer := make([]byte, s.Size())
			buffer, _err = ioutil.ReadFile(filepath.Join(dir, s.Name())) // file.Read(buffer)
			if _err != nil {
				return _err
			}

			if _, _err = tw.Write(buffer); _err != nil {
				return _err
			}
		}
	}

	return nil
}

// func Gzip(pathToFile string, buffer *bytes.Buffer) {
// 	gzFile, _err := os.Create(pathToFile)
// 	handleError(_err)

// 	gzWriter := gzip.NewWriter(gzFile)
// 	_, _err = gzWriter.Write(buffer.Bytes())
// 	handleError(_err)

// 	gzWriter.Close()
// }

func Gzip(buffer *bytes.Buffer) (bytes.Buffer, error) {
	var gzFile bytes.Buffer
	gzWriter := gzip.NewWriter(&gzFile)
	_, _err := gzWriter.Write(buffer.Bytes())
	gzWriter.Flush()
	gzWriter.Close()
	if _err != nil {
		return bytes.Buffer{}, _err
	}

	return gzFile, nil
}

func Encode(buffer *bytes.Buffer, encodeKey []byte) (bytes.Buffer, error) {
	var outbuffer bytes.Buffer

	block, _err := aes.NewCipher(encodeKey)
	if _err != nil {
		return bytes.Buffer{}, _err
	}

	var iv [aes.BlockSize]byte
	stream := cipher.NewOFB(block, iv[:])

	writer := &cipher.StreamWriter{S: stream, W: &outbuffer}
	defer writer.Close()
	if _, _err = io.Copy(writer, buffer); _err != nil {
		return bytes.Buffer{}, _err
	}

	return outbuffer, nil
}

func WriteToFile(path string, buffer bytes.Buffer) error {
	file, _err := os.Create(path)
	defer file.Close()
	if _err != nil {
		return _err
	}
	file.Write(buffer.Bytes())
	return nil
}

func PackAndCompress(dir string, files, excludeFiles []string, outputFile string, key []byte, encrypt bool) error {
	outdir, _ := filepath.Split(outputFile)
	_err := os.MkdirAll(outdir, 0777)
	if _err != nil {
		return _err
	}

	var tarFile bytes.Buffer

	tarWriter := tar.NewWriter(&tarFile)
	_err = Tar(dir, files, excludeFiles, tarWriter, "")

	if _err != nil {
		return _err
	}

	tarWriter.Close()
	gzipedBuffer, _err := Gzip(&tarFile)

	if _err != nil {
		return _err
	}

	if encrypt {
		encryptedBuffer, _err := Encode(&gzipedBuffer, key)
		if _err != nil {
			return _err
		}
		return WriteToFile(outputFile + ".encrypted", encryptedBuffer)
	} else {
		return WriteToFile(outputFile, gzipedBuffer)
	}
}

func handleError(e error) {
	if e != nil {
		panic(e)
	}
}

func UploadToS3(k s3gof3r.Keys, bucketName string, pathToFile string) error {
	s3 := s3gof3r.New("", k)
	b := s3.Bucket(bucketName)

	file, err := os.Open(pathToFile)
	if err != nil {
		return err
	}

	stats, _ := file.Stat()

	w, err := b.PutWriter(stats.Name(), nil, nil)
	if err != nil {
		return err
	}

	if _, err = io.Copy(w, file); err != nil { // Copy into S3
		return err
	}

	if err = w.Close(); err != nil {
		return err
	}

	return nil
}

func DownloadFromS3(k s3gof3r.Keys, bucketName, filename string) (bytes.Buffer, error) {
	s3 := s3gof3r.New("", k)
	b := s3.Bucket(bucketName)

	r, _, err := b.GetReader(filename, nil)
	if err != nil {
		return bytes.Buffer{}, err
	}

	// outputFile, err := os.Create(filepath.Join(outputDirPath, filename))
	// if err != nil {
	// 	return nil, err
	// }

	var output bytes.Buffer

	if _, err = io.Copy(&output, r); err != nil {
		return bytes.Buffer{}, err
	}

	err = r.Close()
	if err != nil {
		return bytes.Buffer{}, err
	}

	return output, nil
}

func SSHUploader(port int, id_rsa, host, user, srcFile, dstFile string) *scp.SecureCopier {
	return scp.NewSecureCopier(port, false, false, false, true, false, false, id_rsa, "", "", srcFile, host, user, dstFile)
}

func SSHDownloader(port int, id_rsa, host, user, srcFile, dstFile string) *scp.SecureCopier {
	return scp.NewSecureCopier(port, false, false, false, true, false, false, id_rsa, host, user, srcFile, "", "", dstFile)
}

func SSHExec(ssh *scp.SecureCopier) (bytes.Buffer, error) {
	var o, e, result bytes.Buffer
	err, _ := ssh.Exec(nil, &o, &e, &result)
	return result, err
}

func prepareFTPConn(port int, host, login, password string) (*goftp.FTP, error) {
	var ftp *goftp.FTP
	var err error

	if ftp, err = goftp.Connect(fmt.Sprintf("%s:%d", host, port)); err != nil {
		return nil, err
	}

	if err = ftp.Login(login, password); err != nil {
		return nil, err
	}

	return ftp, nil
}

func FTPUpload(port int, host, login, password, pathToFile, pathToRemoteFile string) error {
	ftp, err := prepareFTPConn(port, host, login, password)
	if err != nil {
		return err
	}

	file, err := os.Open(pathToFile)
	if err != nil {
		return err
	}

	if err = ftp.Stor(pathToRemoteFile, file); err != nil {
		return err
	}

	return ftp.Quit()
}

func FTPDownload(port int, host, login, password, pathToRemoteFile string) (bytes.Buffer, error) {
	ftp, err := prepareFTPConn(port, host, login, password)
	if err != nil {
		return bytes.Buffer{}, err
	}

	var buffer bytes.Buffer

	_, err = ftp.Retr(pathToRemoteFile, func(r io.Reader) error {
		// file, _err := os.Create(pathToFile)
		// if _err != nil {
		// 	return _err
		// }

		if _, _err := io.Copy(&buffer, r); _err != nil {
			return _err
		}

		return nil
	})

	if err != nil {
		return bytes.Buffer{}, err
	}

	if err = ftp.Quit(); err != nil {
		return buffer, err
	}

	return buffer, nil
}

func Decode(buffer *bytes.Buffer, key []byte) (bytes.Buffer, error) {
	block, err := aes.NewCipher(key)
  if err != nil {
    return bytes.Buffer{}, err
  }

  var iv [aes.BlockSize]byte
  stream := cipher.NewOFB(block, iv[:])

  var outbuffer bytes.Buffer
  reader := &cipher.StreamReader{S: stream, R: buffer}

  if _, err = io.Copy(&outbuffer, reader); err != nil {
    return bytes.Buffer{}, err
  }

  return outbuffer, nil
}

/*
func ErrString(err error) (s *string) {
	if err != nil {
		tmp := err.Error()
		s = &tmp
	}
	return
}
*/
<|endoftext|>