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.

135 lines
2.6 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "os"
  9. "path"
  10. "regexp"
  11. "strings"
  12. "code.google.com/p/go-uuid/uuid"
  13. "github.com/zenazn/goji/web"
  14. )
  15. type UploadRequest struct {
  16. src io.Reader
  17. filename string
  18. expiry int
  19. randomBarename bool
  20. }
  21. type Upload struct {
  22. Filename string
  23. Size int64
  24. Expiry int
  25. }
  26. func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  27. upReq := UploadRequest{}
  28. if r.Header.Get("Content-Type") == "application/octet-stream" {
  29. defer r.Body.Close()
  30. upReq.src = r.Body
  31. upReq.filename = r.URL.Query().Get("qqfile")
  32. } else {
  33. file, headers, err := r.FormFile("file")
  34. if err != nil {
  35. return
  36. }
  37. defer file.Close()
  38. upReq.src = file
  39. upReq.filename = headers.Filename
  40. }
  41. upload, err := processUpload(upReq)
  42. if err != nil {
  43. fmt.Fprintf(w, "Failed to upload: %v", err)
  44. return
  45. }
  46. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  47. js, _ := json.Marshal(map[string]string{
  48. "filename": upload.Filename,
  49. "url": Config.siteURL + upload.Filename,
  50. })
  51. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  52. w.Write(js)
  53. } else {
  54. http.Redirect(w, r, "/"+upload.Filename, 301)
  55. }
  56. }
  57. func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  58. upReq := UploadRequest{}
  59. defer r.Body.Close()
  60. upReq.src = r.Body
  61. upload, err := processUpload(upReq)
  62. if err != nil {
  63. fmt.Fprintf(w, "Failed to upload")
  64. return
  65. }
  66. fmt.Fprintf(w, "File %s uploaded successfully.", upload.Filename)
  67. }
  68. func processUpload(upReq UploadRequest) (upload Upload, err error) {
  69. barename, extension := barePlusExt(upReq.filename)
  70. if upReq.randomBarename || len(barename) == 0 {
  71. barename = generateBarename()
  72. }
  73. if len(extension) == 0 {
  74. extension = "ext"
  75. }
  76. upload.Filename = strings.Join([]string{barename, extension}, ".")
  77. dst, err := os.Create(path.Join("files/", upload.Filename))
  78. if err != nil {
  79. return
  80. }
  81. defer dst.Close()
  82. bytes, err := io.Copy(dst, upReq.src)
  83. if err != nil {
  84. return
  85. } else if bytes == 0 {
  86. err = errors.New("Empty file")
  87. return
  88. }
  89. upload.Size = bytes
  90. return
  91. }
  92. func generateBarename() string {
  93. return uuid.New()[:8]
  94. }
  95. func barePlusExt(filename string) (barename, extension string) {
  96. re := regexp.MustCompile(`[^A-Za-z0-9\-]`)
  97. filename = strings.TrimSpace(filename)
  98. filename = strings.ToLower(filename)
  99. extension = path.Ext(filename)
  100. barename = filename[:len(filename)-len(extension)]
  101. extension = re.ReplaceAllString(extension, "")
  102. barename = re.ReplaceAllString(barename, "")
  103. return
  104. }