示例#1
0
*/
package main

import (
	"fmt"
	"github.com/gophergala2016/srtgears"
	"github.com/gophergala2016/srtgears/exec"
	"os"
	"path"
	"strings"
)

var Version = "dev" // Srtgears version, filled by build

// Our heart: the Executor
var e = exec.New(os.Stdout)

func main() {
	fmt.Printf("Srtgears %s, home page: %s\n", Version, srtgears.HomePage)

	e.FlagSet.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of srtgears:\n")
		e.FlagSet.PrintDefaults()
		fmt.Println(examples)
	}

	if err := e.ProcFlags(os.Args[1:]); err != nil {
		return
	}

	if err := readFiles(); err != nil {
示例#2
0
// sgwHandler handles the form submits.
func sgwHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	c.Debugf("Location: %s;%s;%s;%s", r.Header.Get("X-AppEngine-Country"), r.Header.Get("X-AppEngine-Region"),
		r.Header.Get("X-AppEngine-City"), r.Header.Get("X-AppEngine-CityLatLong"))

	args := []string{} // To simulate command line arguments

	// Form rewind: fill args slice with the posted form values

	in, inh, err := r.FormFile("in")
	if err != nil {
		c.Errorf("No input srt file 'in': %v", err)
		fmt.Fprint(w, "You must select an input srt file!")
		return
	}
	c.Debugf("Received uploaded file 'in': %s", inh.Filename)
	args = append(args, "-in", inh.Filename)

	in2, inh2, err := r.FormFile("in2")
	if err != nil {
		c.Debugf("No 2nd input srt 'in2': %v", err)
	} else {
		c.Debugf("Received uploaded file 'in2': %s", inh2.Filename)
		args = append(args, "-in2", inh2.Filename)
	}

	args = rewindForm(args, r)

	// Our heart: the Executor
	var e = exec.New(w)         // If there are errors, we want them generated on the response.
	e.FlagSet.Usage = func() {} // We don't want usage in the web response

	if err := e.ProcFlags(args); err != nil {
		return // Errors are already written to response.
	}

	// Read input files
	if e.Sp1, err = srtgears.ReadSrtFrom(in); err != nil {
		c.Errorf("Failed to parse uploaded file 'in': %v", err)
		fmt.Fprint(w, "Failed to parse uploaded file: ", err)
		return
	}

	if in2 != nil {
		if e.Sp2, err = srtgears.ReadSrtFrom(in2); err != nil {
			c.Errorf("Failed to parse uploaded file 'in2': %v", err)
			fmt.Fprint(w, "Failed to parse 2nd uploaded file: ", err)
			return
		}
	}

	// We want stats in plain text...
	e.BeforeStats = func() {
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	}
	// Perform transformations
	if err := e.GearIt(); err != nil {
		fmt.Fprint(w, err)
		return
	}

	if e.Stats {
		return // If stats was specified, response is already committed.
	}

	// Everything went ok. We can now generate and send the transformed subtitles.
	if err := sendSubs(w, e); err != nil {
		c.Errorf("Failed to send subtitles: %v", err)
	}
}