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.

152 lines
3.0 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
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. "fmt"
  5. "io"
  6. "net/http"
  7. "os"
  8. "path"
  9. "regexp"
  10. "strconv"
  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. oopsHandler(c, w, r)
  36. return
  37. }
  38. defer file.Close()
  39. upReq.src = file
  40. upReq.filename = headers.Filename
  41. }
  42. upload, err := processUpload(upReq)
  43. if err != nil {
  44. oopsHandler(c, w, r)
  45. return
  46. }
  47. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  48. js, _ := json.Marshal(map[string]string{
  49. "filename": upload.Filename,
  50. "url": Config.siteURL + upload.Filename,
  51. })
  52. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  53. w.Write(js)
  54. } else {
  55. http.Redirect(w, r, "/"+upload.Filename, 301)
  56. }
  57. }
  58. func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  59. upReq := UploadRequest{}
  60. defer r.Body.Close()
  61. upReq.filename = c.URLParams["name"]
  62. upReq.src = r.Body
  63. upload, err := processUpload(upReq)
  64. if err != nil {
  65. oopsHandler(c, w, r)
  66. return
  67. }
  68. fmt.Fprintf(w, Config.siteURL+upload.Filename)
  69. }
  70. func processUpload(upReq UploadRequest) (upload Upload, err error) {
  71. barename, extension := barePlusExt(upReq.filename)
  72. if upReq.randomBarename || len(barename) == 0 {
  73. barename = generateBarename()
  74. }
  75. if len(extension) == 0 {
  76. extension = "ext"
  77. }
  78. upload.Filename = strings.Join([]string{barename, extension}, ".")
  79. _, err = os.Stat(path.Join(Config.filesDir, upload.Filename))
  80. fileexists := err == nil
  81. for fileexists {
  82. counter, err := strconv.Atoi(string(barename[len(barename)-1]))
  83. if err != nil {
  84. barename = barename + "1"
  85. } else {
  86. barename = barename[:len(barename)-1] + strconv.Itoa(counter+1)
  87. }
  88. upload.Filename = strings.Join([]string{barename, extension}, ".")
  89. _, err = os.Stat(path.Join(Config.filesDir, upload.Filename))
  90. fileexists = err == nil
  91. }
  92. dst, err := os.Create(path.Join(Config.filesDir, upload.Filename))
  93. if err != nil {
  94. return
  95. }
  96. defer dst.Close()
  97. bytes, err := io.Copy(dst, upReq.src)
  98. if err != nil {
  99. return
  100. } else if bytes == 0 {
  101. return
  102. }
  103. upload.Size = bytes
  104. return
  105. }
  106. func generateBarename() string {
  107. return uuid.New()[:8]
  108. }
  109. var barePlusRe = regexp.MustCompile(`[^A-Za-z0-9\-]`)
  110. func barePlusExt(filename string) (barename, extension string) {
  111. filename = strings.TrimSpace(filename)
  112. filename = strings.ToLower(filename)
  113. extension = path.Ext(filename)
  114. barename = filename[:len(filename)-len(extension)]
  115. extension = barePlusRe.ReplaceAllString(extension, "")
  116. barename = barePlusRe.ReplaceAllString(barename, "")
  117. return
  118. }