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.

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