示例#1
0
func tarGzHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	files := vars["files"]

	tarfilename := fmt.Sprintf("transfersh-%d.tar.gz", uint16(time.Now().UnixNano()))

	w.Header().Set("Content-Type", "application/x-gzip")
	w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", tarfilename))
	w.Header().Set("Connection", "close")

	os := gzip.NewWriter(w)
	defer os.Close()

	zw := tar.NewWriter(os)
	defer zw.Close()

	for _, key := range strings.Split(files, ",") {
		if strings.HasPrefix(key, "/") {
			key = key[1:]
		}

		key = strings.Replace(key, "\\", "/", -1)

		token := strings.Split(key, "/")[0]
		filename := sanitize.Path(strings.Split(key, "/")[1])

		reader, _, contentLength, err := storage.Get(token, filename)
		if err != nil {
			if err.Error() == "The specified key does not exist." {
				http.Error(w, "File not found", 404)
				return
			} else {
				log.Printf("%s", err.Error())
				http.Error(w, "Could not retrieve file.", 500)
				return
			}
		}

		defer reader.Close()

		header := &tar.Header{
			Name: strings.Split(key, "/")[1],
			Size: int64(contentLength),
		}

		err = zw.WriteHeader(header)
		if err != nil {
			log.Printf("%s", err.Error())
			http.Error(w, "Internal server error.", 500)
			return
		}

		if _, err = io.Copy(zw, reader); err != nil {
			log.Printf("%s", err.Error())
			http.Error(w, "Internal server error.", 500)
			return
		}
	}
}
示例#2
0
func main() {
	args := os.Args
	if len(args) < 2 {
		fmt.Printf(
			"usage: stt out.srt model in.[wav|flac]\n" +
				"       stt -l\n")
		return
	}

	cfg, err := loadCfg("./stt.cfg.json")
	if err != nil {
		log.Fatal(err)
	}

	w := watson.New(cfg.User, cfg.Pass)

	ml, err := w.GetModels()
	if err != nil {
		log.Fatal(err)
	}

	if args[1] == "-l" {
		if len(args) > 2 {
			log.Fatal("too many args")
		}

		for _, m := range ml.Models {
			fmt.Printf("%s %-8d=> %s\n", m.Lang, m.Rate, m.Name)
		}

		return
	}

	out := args[1]
	model := args[2]
	in := args[3]
	ext := ""
	switch path.Ext(in) {
	case ".wav":
		ext = "wav"
	case ".flac":
		ext = "flac"
	case ".json":
		ext = "json"
	default:
		log.Fatal("stt: unknown file format: ", in)
	}

	found := false
	for _, m := range ml.Models {
		if m.Name == model {
			found = true
		}
	}
	if !found {
		log.Fatal("model not found")
	}

	is, err := os.Open(in)
	if err != nil {
		log.Fatal(err)
	}
	defer is.Close()

	os, err := os.Create(out)
	if err != nil {
		log.Fatal(err)
	}
	defer os.Close()

	tt, err := w.Recognize(is, model, ext)
	if err != nil {
		log.Fatal(err)
	}

	for _, w := range tt.Words {
		fmt.Printf("%v\n", w)
	}
}