func MovieTranscode(w http.ResponseWriter, r *http.Request) { id, err := movieIdFromRequest(r) if err != nil { return } movie, err := database.GetMovie(id) if err != nil { http.NotFound(w, r) return } log.Printf("Starting transcoding session for file: %s", movie.Filename) trans := transcoder.NewTranscoderSession(movie.Id, "/tmp/videos", movie.Filename) if err = trans.Open(); err != nil { http.Error(w, err.Error(), 500) return } rel, err := filepath.Rel("/tmp/videos", trans.OutputFile) if err != nil { http.Error(w, err.Error(), 500) return } p := filepath.Join("/videos/", rel) log.Printf("Serving output file: %s", p) http.Redirect(w, r, p, http.StatusTemporaryRedirect) }
func TestDeleteMovie(t *testing.T) { s := createTestServer() defer s.Close() defer os.Remove(TEST_DATABASE) m := testMovie() if err := database.SaveMovie(m); err != nil { t.Errorf("unexpected error: %v", err) } r, err := Delete(s.URL + "/api/movies/" + strconv.FormatInt(m.Id, 10)) if err != nil { t.Errorf("unexpected error: %v", err) } if r.StatusCode != http.StatusOK { t.Errorf("expected %d status code, got %d", http.StatusOK, r.StatusCode) } var rm Movie err = json.NewDecoder(r.Body).Decode(&rm) if err != nil { t.Errorf("unexpected error: %v", err) } if m.Id != rm.Id { t.Errorf("expected returned item id %d, got id %d", m.Id, rm.Id) } // Double check this doesn't exist in the database if movie, err := database.GetMovie(m.Id); err == nil { t.Errorf("expected error fetching deleted movie, got %v", movie) } }
// /movies/:id // Shows movie details for the movie specified by :id func MovieShow(w http.ResponseWriter, r *http.Request) error { id, err := movieIdFromRequest(r) if err != nil { return err } movie, err := database.GetMovie(id) if err != nil { return err } return RenderTemplate(w, r, "movies_show.html", movie) }
// Streams the actual video file directly. func MoviePlay(w http.ResponseWriter, r *http.Request) { id, err := movieIdFromRequest(r) if err != nil { return } movie, err := database.GetMovie(id) if err != nil { http.NotFound(w, r) return } http.ServeFile(w, r, movie.Filename) }
// GET /api/movies/:id // Shows details for a movie with :id func MovieShow(w http.ResponseWriter, r *http.Request) (interface{}, error) { idstr := getPathParam(r, "id") id, err := strconv.ParseInt(idstr, 10, 64) if err != nil { return nil, err } m, err := database.GetMovie(id) if err != nil { if err == sql.ErrNoRows { return nil, notFound{} } else { return nil, err } } return m, nil }
func TestCreateMovie(t *testing.T) { s := createTestServer() defer s.Close() defer os.Remove(TEST_DATABASE) movie := testMovie() b, err := json.Marshal(movie) if err != nil { t.Errorf("Unexpected error: %v", err) } r, err := Put(s.URL+"/api/movies", "application/json", strings.NewReader(fmt.Sprintf("%s", b))) if err != nil { t.Errorf("Unexpected error: %v", err) } var returnedMovie Movie err = json.NewDecoder(r.Body).Decode(&returnedMovie) if err != nil { t.Errorf("Unexpected error: %v", err) } if returnedMovie.Id == 0 { t.Errorf("expected non-zero movie Id, got %v", returnedMovie.Id) } if returnedMovie.Title != movie.Title { t.Errorf("expected matching titles, got %v", returnedMovie) } // Check that it is actually in the database _, err = database.GetMovie(returnedMovie.Id) if err != nil { t.Errorf("Failed to find created movie in database: %v", err) } }