Contains the Concourse pipeline definition for building a line-server container
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.1 KiB

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/andreimarcu/linx-server/backends"
  8. "github.com/zeebo/bencode"
  9. "github.com/zenazn/goji/web"
  10. )
  11. func createTorrent(fileName string, r *http.Request) ([]byte, error) {
  12. url := fmt.Sprintf("%sselif/%s", getSiteURL(r), fileName)
  13. t, err := storageBackend.GetTorrent(fileName, url)
  14. if err != nil {
  15. return []byte{}, err
  16. }
  17. data, err := bencode.EncodeBytes(&t)
  18. if err != nil {
  19. return []byte{}, err
  20. }
  21. return data, nil
  22. }
  23. func fileTorrentHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  24. fileName := c.URLParams["name"]
  25. _, err := checkFile(fileName)
  26. if err == backends.NotFoundErr {
  27. notFoundHandler(c, w, r)
  28. return
  29. } else if err == backends.BadMetadata {
  30. oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
  31. return
  32. }
  33. encoded, err := createTorrent(fileName, r)
  34. if err != nil {
  35. oopsHandler(c, w, r, RespHTML, "Could not create torrent.")
  36. return
  37. }
  38. w.Header().Set(`Content-Disposition`, fmt.Sprintf(`attachment; filename="%s.torrent"`, fileName))
  39. http.ServeContent(w, r, "", time.Now(), bytes.NewReader(encoded))
  40. }