Пример #1
0
func TestPreview(t *testing.T) {
	playlist, err := generator.Preview(datastore.Program{
		UUID:       "some-fake-uuid",
		FunctionId: "iterator",
		StartAt:    time.Now(),
		Parameters: map[string]string{
			"start_at":        "0",
			"end_at":          "99",
			"target_duration": "5.0",
			"url_format":      testUrlFormat,
		},
	})

	if err != nil {
		t.Fatal("Error previewing playlist:", err)
	}

	for i := 0; i < 100; i++ {
		segment := playlist.Segments[i]
		if segment == nil {
			t.Error("Expected segment at", i, "but got nil")
			return
		}

		if segment.URI != fmt.Sprintf(testUrlFormat, i) {
			t.Error("Error: segment", i, "media url = ", segment.URI)
			return
		}
	}
}
Пример #2
0
func (p PreviewController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	urlBase := strings.Split(r.URL.Path, ".")[0]
	requestedProgram := urlBase

	if r.Method == "POST" {
		if newId := programPosted(w, r); newId == "" {
			return
		} else if requestedProgram == "new" || requestedProgram == "temp" {
			requestedProgram = newId
			fullUrl := fmt.Sprintf("/vod/%s.m3u8", requestedProgram)
			w.Header().Add("Location", fullUrl)
			w.Header().Add("X-Guide-Program", requestedProgram)
		}
	}

	program, err := datastore.GetProgram(requestedProgram)
	if err != nil {
		http.Error(w, "Program not found", 404)
		return
	}

	if urlBase != "temp" {
		program.Persist()
	}

	if filepath.Ext(r.URL.Path) == ".json" {
		w.Header().Add("Content-Type", "application/json")
		encoder := json.NewEncoder(w)
		if err := encoder.Encode(program); err != nil {
			http.Error(w, "Error serializing program", 500)
			return
		}
	} else {
		w.Header().Add("Content-Type", "application/x-mpegurl")
		playlist, _ := generator.Preview(program)
		fmt.Fprintln(w, playlist.Encode().String())
	}
}