Example #1
0
func Unzip(zip_filename, target string) (err error) {
	zr, err := zip.OpenReader(zip_filename)
	defer zr.Close()
	if goutils.CheckErr(err) {
		return
	}
	var target_filename string
	for _, it := range zr.File {
		fmt.Println(it.Name)
		target_filename = filepath.Join(target, it.Name)
		if it.FileInfo().IsDir() {
			os.MkdirAll(target_filename, 0666)
			continue
		}
		of, err := os.OpenFile(target_filename, os.O_CREATE|os.O_WRONLY, 0666)
		defer of.Close()
		if goutils.CheckErr(err) {
			continue
		}
		rit, err := it.Open()
		defer rit.Close()
		_, err = io.Copy(of, rit)
		if goutils.CheckErr(err) {
			continue
		}
	}
	return nil
}
Example #2
0
func Untar(tar_filename, target string) (err error) {
	file, err := os.OpenFile(tar_filename, os.O_RDONLY, 0444)
	defer file.Close()
	if goutils.CheckErr(err) {
		return
	}
	tr := tar.NewReader(file)
	var target_filename string
	for {
		hr, err := tr.Next()
		if err == io.EOF {
			break
		}
		fmt.Println(hr.FileInfo().Name())
		target_filename = filepath.Join(target, hr.Name)
		if hr.FileInfo().IsDir() {
			os.MkdirAll(target_filename, 0666)
			continue
		}
		of, err := os.OpenFile(target_filename, os.O_CREATE|os.O_WRONLY, 0666)
		defer of.Close()
		if goutils.CheckErr(err) {
			continue
		}
		_, err = io.Copy(of, tr)
		if goutils.CheckErr(err) {
			continue
		}
	}
	return nil
}
Example #3
0
func markdownCBQiniu(rw http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	rawContent := req.Form.Get("rawContent")
	// fmt.Println(rawContent)
	fmt.Print(",")
	out := make([]byte, 0, 100)
	in := goutils.ToByte(rawContent)
	RPC_Client = rpcsv.RPCClient(rpc_tcp_server)
	err := rpcsv.Markdown(RPC_Client, &in, &out)
	if goutils.CheckErr(err) {
		rw.Write(goutils.ToByte(err.Error()))
		return
	}
	qiniuWriter.Reset()
	qiniuWriter.Write(goutils.ToByte("CallbackFunc(`"))
	data := make(map[string]interface{})
	data["MDContent"] = template.HTML(goutils.ToString(out))
	err = rpcsv.Theme.Execute(qiniuWriter, data)
	if goutils.CheckErr(err) {
		rw.Write(goutils.ToByte(err.Error()))
	}
	qiniuWriter.Write(goutils.ToByte("`)"))
	rw.Write(qiniuWriter.Bytes())
	writeCrossDomainHeaders(rw, req)
}
Example #4
0
File: modle.go Project: shaalx/gooj
func GenerateOjModle(path_ string, m *Model) error {
	_, err := os.Stat(path_)
	if !goutils.CheckErr(err) {
		os.RemoveAll(path_)
	}
	err = os.Mkdir(path_, 0777)
	if goutils.CheckErr(err) {
		// return err
	}
	fname := m.FuncName + ".go"
	tname := m.FuncName + "_test.go"
	func_file, err := os.OpenFile(filepath.Join(path_, fname), os.O_CREATE|os.O_WRONLY, 0644)
	defer func_file.Close()
	if goutils.CheckErr(err) {
		return err
	}
	if goutils.CheckErr(generateOjFunc(m.Content, func_file)) {
		return fmt.Errorf("generateOjFunc error")
	}

	test_file, err := os.OpenFile(filepath.Join(path_, tname), os.O_CREATE|os.O_WRONLY, 0644)
	defer test_file.Close()
	if goutils.CheckErr(err) {
		return err
	}
	args := make(map[string]interface{})
	args["FUNC"] = m.FuncName
	args["ArgsType"] = m.ArgsType
	args["RetsType"] = m.RetsType
	args["TestCases"] = m.TestCases
	return renderOjModle(args, test_file)
}
Example #5
0
func renderFile(filename string, redo bool) bool {
	_, err := os.Lstat(filename + ".html")
	if !redo && nil == err {
		return false
	}
	if nil == err {
		err = os.Remove(filename + ".html")
		goutils.CheckErr(err)
	}
	input := readFile(filename)
	if nil == input {
		return false
	}
	// unsafe := blackfriday.MarkdownCommon(input)
	// html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
	html := md.Markdown(input)

	data := make(map[string]interface{})
	data["MDContent"] = goutils.ToString(html)

	data["Title"] = getName(filename)
	of, err := os.OpenFile( /*targetDir+string(os.PathSeparator)+*/ filename+".html", os.O_CREATE|os.O_RDWR, 0666)
	defer of.Close()
	if goutils.CheckErr(err) {
		return false
	}
	err = theme.Execute(of, data)
	if goutils.CheckErr(err) {
		return false
	}
	return true
}
Example #6
0
func ExtractFunc(path string, info os.FileInfo, err error) error {
	if strings.EqualFold(".git", info.Name()) {
		return filepath.SkipDir
	}
	if strings.HasPrefix(path, base) {
		return filepath.SkipDir
	}
	if info.IsDir() {
		err = os.Mkdir(home+Spor+path, 0644)
		if goutils.CheckErr(err) {
			return nil
		}
	}

	if strings.EqualFold(ext, filepath.Ext(path)) {
		orf, err := os.OpenFile(path, os.O_RDONLY, 0644)
		defer orf.Close()
		if goutils.CheckErr(err) {
			return nil
		}
		owf, err := os.OpenFile(base+string(os.PathSeparator)+path, os.O_CREATE|os.O_WRONLY, 0622)
		defer owf.Close()
		if goutils.CheckErr(err) {
			return nil
		}
		n, err := io.Copy(owf, orf)
		fmt.Printf("%s: %d bytes.\n", path, n)
	}
	return nil
}
Example #7
0
func markdown(rw http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	rawContent := req.Form.Get("rawContent")
	// fmt.Println(req.RemoteAddr, req.Referer())
	fmt.Print(".")
	// fmt.Println(rawContent)
	out := make([]byte, 0, 100)
	in := goutils.ToByte(rawContent)
	times := 0
	connect()
retry:
	times++
	err := rpcsv.Markdown(RPC_Client, &in, &out)
	if goutils.CheckErr(err) {
		connect()
		if times < 6 {
			goto retry
		}
		rw.Write(goutils.ToByte(err.Error()))
		return
	}

	data := make(map[string]interface{})
	data["MDContent"] = template.HTML(goutils.ToString(out))
	err = rpcsv.Theme.Execute(rw, data)
	if goutils.CheckErr(err) {
		rw.Write(goutils.ToByte(err.Error()))
	}
	// if len(out) <= 0 {
	// 	rw.Write(goutils.ToByte("{response:nil}"))
	// 	return
	// }
	// writeCrossDomainHeaders(rw, req)
	// rw.Write(out)
}
Example #8
0
// in: Linux/index.md
// out: ./MDFs
func modifiedMD(file_in, dir_out string) {
	finfo, err := os.Stat(file_in)
	if goutils.CheckErr(err) {
		return
	}
	filename := finfo.Name()
	dir := filepath.Dir(file_in)
	fs := strings.Split(filename, ".")
	in := goutils.ReadFile(file_in)
	out := make([]byte, 1)
	err = rpcsv.Markdown(rpc_client, &in, &out)
	if goutils.CheckErr(err) {
		return
	}
	// fmt.Println(goutils.ToString(out))
	target := fmt.Sprintf("%s.html", filepath.Join(dir_out, dir, fs[0]))

	goutils.Mkdir(fmt.Sprintf("%s", filepath.Join(dir_out, dir)))
	outfile, _ := os.OpenFile(fmt.Sprintf("%s.html", filepath.Join(dir_out, dir, fs[0])), os.O_CREATE|os.O_WRONLY, 0666)
	defer outfile.Close()
	dt := make(map[string]interface{})
	dt["MDContent"] = template.HTML(goutils.ToString(out))
	tpl.Execute(outfile, dt)
	fmt.Println(file_in, " ==> ", target)
}
Example #9
0
File: mj.go Project: shaalx/gooj
func ToM() *Model {
	b, err := ioutil.ReadFile("model.json")
	goutils.CheckErr(err)
	var ret Model
	err = json.Unmarshal(b, &ret)
	goutils.CheckErr(err)
	return &ret
}
Example #10
0
File: mj.go Project: shaalx/gooj
func ToMs() []Model {
	b, err := ioutil.ReadFile("models.json")
	goutils.CheckErr(err)
	var ret []Model
	err = json.Unmarshal(b, &ret)
	goutils.CheckErr(err)
	return ret
}
Example #11
0
File: mj.go Project: shaalx/gooj
func MJ() {
	f, err := os.OpenFile("model.json", os.O_CREATE|os.O_WRONLY, 0644)
	goutils.CheckErr(err)
	m := NewModel()
	b, err := json.MarshalIndent(m, "", "\t")
	goutils.CheckErr(err)
	f.Write(b)
}
Example #12
0
File: mj.go Project: shaalx/gooj
// http://7xku3c.com1.z0.glb.clouddn.com/models.json
func TiniuMs(_url string) []Model {
	resp, _ := http.Get(_url)
	b, err := ioutil.ReadAll(resp.Body)
	goutils.CheckErr(err)
	var ret []Model
	err = json.Unmarshal(b, &ret)
	goutils.CheckErr(err)
	return ret
}
Example #13
0
func readFile(filename string) []byte {
	file, err := os.OpenFile(filename, os.O_RDONLY, 0644)
	if goutils.CheckErr(err) {
		return nil
	}
	defer file.Close()
	b, err := ioutil.ReadAll(file)
	if goutils.CheckErr(err) {
		return nil
	}
	return b
}
Example #14
0
File: modle.go Project: shaalx/gooj
func renderOjModle(args map[string]interface{}, w io.Writer) error {
	b, err := ioutil.ReadFile("oj.tpl")
	if goutils.CheckErr(err) {
		return err
	}
	s := goutils.ToString(b)
	tpl, err := template.New("oj.tpl").Parse(s)
	if goutils.CheckErr(err) {
		return err
	}
	return tpl.Execute(w, args)
}
Example #15
0
File: c.go Project: everfore/rpcsv
func RPCClient(tcp_addr string) *rpc.Client {
	addr, err := net.ResolveTCPAddr("tcp", tcp_addr)
	if goutils.CheckErr(err) {
		return nil
	}
	conn, err := net.DialTCP("tcp", nil, addr)
	if goutils.CheckErr(err) {
		return nil
	}

	rc := rpc.NewClient(conn)
	return rc
}
Example #16
0
func DownloadImg(url_ string) {
	resp, err := http.Get(url_)
	if goutils.CheckErr(err) {
		return
	}
	log.Println(resp.Header.Get("Content-Type"))
	log.Println(resp.ContentLength)
	_ = resp
	b, err := ioutil.ReadAll(resp.Body)
	if goutils.CheckErr(err) {
		return
	}
	// log.Println(b)
	file, _ := os.OpenFile("down.png", os.O_CREATE|os.O_WRONLY, 0644)
	file.Write(b)
}
Example #17
0
func RPCServeWithCode(port string) (net.Listener, error) {
	rpc.Register(&RPC{})
	lis, err := net.Listen("tcp", ":"+port)
	goutils.CheckErr(err)
	go func() {
		for {
			conn, err := lis.Accept()
			if err != nil {
				fmt.Println("Error: accept rpc connection", err.Error())
				continue
			}
			go func(conn net.Conn) {
				buf := bufio.NewWriter(conn)
				srv := &gobServerCodec{
					rwc:    conn,
					dec:    gob.NewDecoder(conn),
					enc:    gob.NewEncoder(buf),
					encBuf: buf,
				}
				defer srv.Close()
				err = rpc.ServeRequest(srv)
				if err != nil {
					fmt.Println("Error: server rpc request", err.Error())
				}
			}(conn)
		}
	}()
	return lis, nil
}
Example #18
0
func ojCheck(id int) int {
	n, err := models.ORM.QueryTable((*models.Puzzle)(nil)).Filter("Id", id).Filter("Online", 1).Count()
	if goutils.CheckErr(err) {
		return 0
	}
	return int(n)
}
Example #19
0
// @router /puzzle/:id:int [post]
func (c *PuzzleController) PuzzlePostId() {
	var id int
	c.Ctx.Input.Bind(&id, ":id")
	check_res := c.checkPuzzleUser(id)
	if check_res < 2 {
		c.Redirect(fmt.Sprintf("/oj/%d", id), 302)
		return
	}

	var puzzle models.Puzzle
	err := c.ParseForm(&puzzle)
	puzzle.Id = id
	var puzzle_old models.Puzzle
	models.ORM.QueryTable((*models.Puzzle)(nil)).Filter("Id", id).One(&puzzle_old)
	puzzle.User = puzzle_old.User
	if check_res != 3 { // 管理员
		puzzle.Online = 0
	}
	if !goutils.CheckErr(err) {
		n, err := models.ORM.Update(&puzzle)
		beego.Debug(n, err)
	}
	fmt.Println(puzzle)
	c.Redirect(fmt.Sprintf("/oj/%d", id), 302)
}
Example #20
0
func Json(data interface{}) []byte {
	bs, err := json.Marshal(CallbackData{Mddata: data})
	if goutils.CheckErr(err) {
		return nil
	}
	return bs
}
Example #21
0
File: fmt.go Project: shaalx/jsnm
func FileNameFmt(fn string) *Jsnm {
	rf, err := os.OpenFile(fn, os.O_RDONLY, 0644)
	if goutils.CheckErr(err) {
		return nil
	}
	return ReaderFmt(rf)
}
Example #22
0
File: c.go Project: everfore/rpcsv
func Markdown(rc *rpc.Client, in, out *([]byte)) error {
	err := rc.Call("RPC.Markdown", in, out)
	if goutils.CheckErr(err) {
		return err
	}
	return nil
}
Example #23
0
func TestUnzipReader1(t *testing.T) {
	of, err := os.OpenFile("test.zip", os.O_RDONLY, 0644)
	defer of.Close()
	if goutils.CheckErr(err) {
		t.Log(err)
	}
	UnzipReader(of, "./test")
}
Example #24
0
File: fmt.go Project: shaalx/jsnm
func ReaderFmt(r io.Reader) *Jsnm {
	v := NewJsnm(nil)
	err := json.NewDecoder(r).Decode(&v.raw_data)
	if goutils.CheckErr(err) {
		return nil
	}
	return v
}
Example #25
0
func indexV2(rw http.ResponseWriter, req *http.Request) {
	tpl, err := template.New("indexV2.html").ParseFiles("indexV2.html")
	if goutils.CheckErr(err) {
		rw.Write(goutils.ToByte(err.Error()))
		return
	}
	tpl.Execute(rw, nil)
}
Example #26
0
func main() {
	// rpcsv.RPCServe("8800")
	lis, err := rpcsv.RPCServeWithCode("8800")
	if goutils.CheckErr(err) {
		return
	}
	defer lis.Close()
	<-make(chan bool)
}
Example #27
0
func TestUnzipReader2(t *testing.T) {
	resp, err := http.Get("https://codeload.github.com/toukii/tools/zip/env")
	if goutils.CheckErr(err) {
		return
	}
	gopath := os.Getenv("GOPATH")
	target := filepath.Join(gopath, "src", "github.com", "toukii", "tools")
	UnzipReader(resp.Body, target)
}
Example #28
0
File: main.go Project: everfore/exc
func OverWrite(path string, bs []byte) {
	// wf, err := os.OpenFile(path, os.O_WRONLY, 0655)
	// defer wf.Close()
	// if goutils.CheckErr(err) {
	// 	return
	// }
	err := ioutil.WriteFile(path, bs, 0655)
	goutils.CheckErr(err)
}
Example #29
0
File: main.go Project: everfore/exc
func ContentContains(path string, stuff string) ([]byte, bool) {
	abs_path, err := filepath.Abs(path)
	if goutils.CheckErr(err) {
		return nil, false
	}
	fr, err := os.OpenFile(abs_path, os.O_RDONLY, 0644)
	defer fr.Close()
	if goutils.CheckErr(err) {
		return nil, false
	}
	bs, err := ioutil.ReadAll(fr)
	if goutils.CheckErr(err) {
		return nil, false
	}
	if strings.Contains(goutils.ToString(bs), stuff) {
		return bs, true
	}
	return nil, false
}
Example #30
0
func RPCClientWithCodec(tcp_addr string) *rpc.Client {
	conn, err := net.DialTimeout("tcp", tcp_addr, time.Second*5)
	if goutils.CheckErr(err) {
		return nil
	}
	encBuf := bufio.NewWriter(conn)
	codec := &RPCgobClientCodec{conn, gob.NewDecoder(conn), gob.NewEncoder(encBuf), encBuf}
	c := rpc.NewClientWithCodec(codec)
	return c
}