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.

228 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 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. // if UploadRequest.src
  102. // Determine the appropriate filename, then write to disk
  103. barename, extension := barePlusExt(upReq.filename)
  104. if upReq.randomBarename || len(barename) == 0 {
  105. barename = generateBarename()
  106. }
  107. if len(extension) == 0 {
  108. extension = "ext"
  109. }
  110. upload.Filename = strings.Join([]string{barename, extension}, ".")
  111. _, err = os.Stat(path.Join(Config.filesDir, upload.Filename))
  112. fileexists := err == nil
  113. for fileexists {
  114. counter, err := strconv.Atoi(string(barename[len(barename)-1]))
  115. if err != nil {
  116. barename = barename + "1"
  117. } else {
  118. barename = barename[:len(barename)-1] + strconv.Itoa(counter+1)
  119. }
  120. upload.Filename = strings.Join([]string{barename, extension}, ".")
  121. _, err = os.Stat(path.Join(Config.filesDir, upload.Filename))
  122. fileexists = err == nil
  123. }
  124. dst, err := os.Create(path.Join(Config.filesDir, upload.Filename))
  125. if err != nil {
  126. return
  127. }
  128. defer dst.Close()
  129. // Get the rest of the metadata needed for storage
  130. upload.Expiry = getFutureTimestamp(upReq.expiry)
  131. // If no delete key specified, pick a random one.
  132. if upReq.deletionKey == "" {
  133. upload.DeleteKey = randomString(30)
  134. } else {
  135. upload.DeleteKey = upReq.deletionKey
  136. }
  137. metadataWrite(upload.Filename, &upload)
  138. bytes, err := io.Copy(dst, upReq.src)
  139. if err != nil {
  140. return
  141. } else if bytes == 0 {
  142. os.Remove(path.Join(Config.filesDir, upload.Filename))
  143. os.Remove(path.Join(Config.metaDir, upload.Filename))
  144. return upload, errors.New("Empty file")
  145. }
  146. upload.Size = bytes
  147. return
  148. }
  149. func generateBarename() string {
  150. return randomString(8)
  151. }
  152. func generateJSONresponse(upload Upload) []byte {
  153. js, _ := json.Marshal(map[string]string{
  154. "url": Config.siteURL + upload.Filename,
  155. "filename": upload.Filename,
  156. "delete_key": upload.DeleteKey,
  157. "expiry": strconv.FormatInt(int64(upload.Expiry), 10),
  158. "size": strconv.FormatInt(upload.Size, 10),
  159. })
  160. return js
  161. }
  162. var barePlusRe = regexp.MustCompile(`[^A-Za-z0-9\-]`)
  163. func barePlusExt(filename string) (barename, extension string) {
  164. filename = strings.TrimSpace(filename)
  165. filename = strings.ToLower(filename)
  166. extension = path.Ext(filename)
  167. barename = filename[:len(filename)-len(extension)]
  168. extension = barePlusRe.ReplaceAllString(extension, "")
  169. barename = barePlusRe.ReplaceAllString(barename, "")
  170. return
  171. }
  172. func parseExpiry(expStr string) int32 {
  173. if expStr == "" {
  174. return 0
  175. } else {
  176. expiry, err := strconv.ParseInt(expStr, 10, 32)
  177. if err != nil {
  178. return 0
  179. } else {
  180. return int32(expiry)
  181. }
  182. }
  183. }