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.

223 lines
5.1 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. }
  30. func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  31. upReq := UploadRequest{}
  32. uploadHeaderProcess(r, &upReq)
  33. if r.Header.Get("Content-Type") == "application/octet-stream" {
  34. if r.URL.Query().Get("randomize") == "true" {
  35. upReq.randomBarename = true
  36. }
  37. upReq.expiry = parseExpiry(r.URL.Query().Get("expires"))
  38. defer r.Body.Close()
  39. upReq.src = r.Body
  40. upReq.filename = r.URL.Query().Get("qqfile")
  41. } else {
  42. file, headers, err := r.FormFile("file")
  43. if err != nil {
  44. oopsHandler(c, w, r)
  45. return
  46. }
  47. defer file.Close()
  48. r.ParseForm()
  49. if r.Form.Get("randomize") == "true" {
  50. upReq.randomBarename = true
  51. }
  52. upReq.expiry = parseExpiry(r.Form.Get("expires"))
  53. upReq.src = file
  54. upReq.filename = headers.Filename
  55. }
  56. upload, err := processUpload(upReq)
  57. if err != nil {
  58. oopsHandler(c, w, r)
  59. return
  60. }
  61. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  62. js := generateJSONresponse(upload)
  63. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  64. w.Write(js)
  65. } else {
  66. http.Redirect(w, r, "/"+upload.Filename, 301)
  67. }
  68. }
  69. func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  70. upReq := UploadRequest{}
  71. uploadHeaderProcess(r, &upReq)
  72. defer r.Body.Close()
  73. upReq.filename = c.URLParams["name"]
  74. upReq.src = r.Body
  75. upload, err := processUpload(upReq)
  76. if err != nil {
  77. oopsHandler(c, w, r)
  78. return
  79. }
  80. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  81. js := generateJSONresponse(upload)
  82. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  83. w.Write(js)
  84. } else {
  85. fmt.Fprintf(w, Config.siteURL+upload.Filename)
  86. }
  87. }
  88. func uploadHeaderProcess(r *http.Request, upReq *UploadRequest) {
  89. // For legacy reasons
  90. if r.Header.Get("X-Randomized-Filename") == "yes" {
  91. upReq.randomBarename = true
  92. } else if r.Header.Get("X-Randomized-Barename") == "yes" {
  93. upReq.randomBarename = true
  94. }
  95. upReq.deletionKey = r.Header.Get("X-Delete-Key")
  96. // Get seconds until expiry. Non-integer responses never expire.
  97. expStr := r.Header.Get("X-File-Expiry")
  98. upReq.expiry = parseExpiry(expStr)
  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. func generateJSONresponse(upload Upload) []byte {
  150. js, _ := json.Marshal(map[string]string{
  151. "url": Config.siteURL + upload.Filename,
  152. "filename": upload.Filename,
  153. "delete_key": upload.DeleteKey,
  154. "expiry": strconv.FormatInt(int64(upload.Expiry), 10),
  155. "size": strconv.FormatInt(upload.Size, 10),
  156. })
  157. return js
  158. }
  159. var barePlusRe = regexp.MustCompile(`[^A-Za-z0-9\-]`)
  160. func barePlusExt(filename string) (barename, extension string) {
  161. filename = strings.TrimSpace(filename)
  162. filename = strings.ToLower(filename)
  163. extension = path.Ext(filename)
  164. barename = filename[:len(filename)-len(extension)]
  165. extension = barePlusRe.ReplaceAllString(extension, "")
  166. barename = barePlusRe.ReplaceAllString(barename, "")
  167. return
  168. }
  169. func parseExpiry(expStr string) int32 {
  170. if expStr == "" {
  171. return 0
  172. } else {
  173. expiry, err := strconv.ParseInt(expStr, 10, 32)
  174. if err != nil {
  175. return 0
  176. } else {
  177. return int32(expiry)
  178. }
  179. }
  180. }