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.

200 lines
4.5 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
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. "github.com/pborman/uuid"
  13. "github.com/zenazn/goji/web"
  14. )
  15. // Describes metadata directly from the user request
  16. type UploadRequest struct {
  17. src io.Reader
  18. filename string
  19. expiry int32 // Seconds until expiry, 0 = never
  20. randomBarename bool
  21. deletionKey string // Empty string if not defined
  22. }
  23. // Metadata associated with a file as it would actually be stored
  24. type Upload struct {
  25. Filename string // Final filename on disk
  26. Size int64
  27. Expiry int32 // Unix timestamp of expiry, 0=never
  28. DeleteKey string // Deletion key, one generated if not provided
  29. DebugInfo string // Optional field to store whatever
  30. }
  31. func uploadHeaderProcess(r *http.Request, upReq *UploadRequest) {
  32. // For legacy reasons
  33. upReq.randomBarename = false
  34. if r.Header.Get("X-Randomized-Filename") == "yes" {
  35. upReq.randomBarename = true
  36. }
  37. if r.Header.Get("X-Randomized-Barename") == "yes" {
  38. upReq.randomBarename = true
  39. }
  40. upReq.deletionKey = r.Header.Get("X-Delete-Key")
  41. // Get seconds until expiry. Non-integer responses never expire.
  42. expStr := r.Header.Get("X-File-Expiry")
  43. if expStr == "" {
  44. upReq.expiry = 0
  45. } else {
  46. expiry, err := strconv.ParseInt(expStr, 10, 32)
  47. if err != nil {
  48. upReq.expiry = 0
  49. } else {
  50. upReq.expiry = int32(expiry)
  51. }
  52. }
  53. }
  54. func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  55. upReq := UploadRequest{}
  56. uploadHeaderProcess(r, &upReq)
  57. if r.Header.Get("Content-Type") == "application/octet-stream" {
  58. defer r.Body.Close()
  59. upReq.src = r.Body
  60. upReq.filename = r.URL.Query().Get("qqfile")
  61. } else {
  62. file, headers, err := r.FormFile("file")
  63. if err != nil {
  64. oopsHandler(c, w, r)
  65. return
  66. }
  67. defer file.Close()
  68. upReq.src = file
  69. upReq.filename = headers.Filename
  70. }
  71. upload, err := processUpload(upReq)
  72. if err != nil {
  73. oopsHandler(c, w, r)
  74. return
  75. }
  76. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  77. js, _ := json.Marshal(map[string]string{
  78. "filename": upload.Filename,
  79. "url": Config.siteURL + upload.Filename,
  80. })
  81. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  82. w.Write(js)
  83. } else {
  84. http.Redirect(w, r, "/"+upload.Filename, 301)
  85. }
  86. }
  87. func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  88. upReq := UploadRequest{}
  89. uploadHeaderProcess(r, &upReq)
  90. defer r.Body.Close()
  91. upReq.filename = c.URLParams["name"]
  92. upReq.src = r.Body
  93. upload, err := processUpload(upReq)
  94. if err != nil {
  95. oopsHandler(c, w, r)
  96. return
  97. }
  98. fmt.Fprintf(w, Config.siteURL+upload.Filename)
  99. }
  100. func processUpload(upReq UploadRequest) (upload Upload, err error) {
  101. // Determine the appropriate filename, then write to disk
  102. barename, extension := barePlusExt(upReq.filename)
  103. if upReq.randomBarename || len(barename) == 0 {
  104. barename = generateBarename()
  105. }
  106. if len(extension) == 0 {
  107. extension = "ext"
  108. }
  109. upload.Filename = strings.Join([]string{barename, extension}, ".")
  110. _, err = os.Stat(path.Join(Config.filesDir, upload.Filename))
  111. fileexists := err == nil
  112. for fileexists {
  113. counter, err := strconv.Atoi(string(barename[len(barename)-1]))
  114. if err != nil {
  115. barename = barename + "1"
  116. } else {
  117. barename = barename[:len(barename)-1] + strconv.Itoa(counter+1)
  118. }
  119. upload.Filename = strings.Join([]string{barename, extension}, ".")
  120. _, err = os.Stat(path.Join(Config.filesDir, upload.Filename))
  121. fileexists = err == nil
  122. }
  123. dst, err := os.Create(path.Join(Config.filesDir, upload.Filename))
  124. if err != nil {
  125. return
  126. }
  127. defer dst.Close()
  128. // Get the rest of the metadata needed for storage
  129. upload.Expiry = getFutureTimestamp(upReq.expiry)
  130. // If no delete key specified, pick a random one.
  131. if upReq.deletionKey == "" {
  132. upload.DeleteKey = uuid.New()[:30]
  133. } else {
  134. upload.DeleteKey = upReq.deletionKey
  135. }
  136. metadataWrite(upload.Filename, &upload)
  137. bytes, err := io.Copy(dst, upReq.src)
  138. if err != nil {
  139. return
  140. } else if bytes == 0 {
  141. return
  142. }
  143. upload.Size = bytes
  144. return
  145. }
  146. func generateBarename() string {
  147. return uuid.New()[:8]
  148. }
  149. var barePlusRe = regexp.MustCompile(`[^A-Za-z0-9\-]`)
  150. func barePlusExt(filename string) (barename, extension string) {
  151. filename = strings.TrimSpace(filename)
  152. filename = strings.ToLower(filename)
  153. extension = path.Ext(filename)
  154. barename = filename[:len(filename)-len(extension)]
  155. extension = barePlusRe.ReplaceAllString(extension, "")
  156. barename = barePlusRe.ReplaceAllString(barename, "")
  157. return
  158. }