Exemplo n.º 1
0
Arquivo: sgw.go Projeto: icza/srtgears
// 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"))

	// To simulate command line arguments
	args := []string{"-debug=false"} // No debug, this is the web interface!

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

	in, inh, err := r.FormFile("in")
	if err == nil {
		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("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 in != nil {
		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)
	}
}
Exemplo n.º 2
0
package main

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

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

// 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 len(os.Args) < 2 { // 0 arguments, print usage