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.

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