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.

225 lines
5.3 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
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. "strconv"
  12. "strings"
  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 int64 // 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 int64 // 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 = randomString(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. os.Remove(path.Join(Config.filesDir, upload.Filename))
  142. os.Remove(path.Join(Config.metaDir, upload.Filename))
  143. return upload, errors.New("Empty file")
  144. }
  145. upload.Size = bytes
  146. return
  147. }
  148. func generateBarename() string {
  149. return randomString(8)
  150. }
  151. func generateJSONresponse(upload Upload) []byte {
  152. js, _ := json.Marshal(map[string]string{
  153. "url": Config.siteURL + upload.Filename,
  154. "filename": upload.Filename,
  155. "delete_key": upload.DeleteKey,
  156. "expiry": strconv.FormatInt(int64(upload.Expiry), 10),
  157. "size": strconv.FormatInt(upload.Size, 10),
  158. })
  159. return js
  160. }
  161. var barePlusRe = regexp.MustCompile(`[^A-Za-z0-9\-]`)
  162. func barePlusExt(filename string) (barename, extension string) {
  163. filename = strings.TrimSpace(filename)
  164. filename = strings.ToLower(filename)
  165. extension = path.Ext(filename)
  166. barename = filename[:len(filename)-len(extension)]
  167. extension = barePlusRe.ReplaceAllString(extension, "")
  168. barename = barePlusRe.ReplaceAllString(barename, "")
  169. return
  170. }
  171. func parseExpiry(expStr string) int64 {
  172. if expStr == "" {
  173. return 0
  174. } else {
  175. expiry, err := strconv.ParseInt(expStr, 10, 64)
  176. if err != nil {
  177. return 0
  178. } else {
  179. return int64(expiry)
  180. }
  181. }
  182. }