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.

343 lines
8.4 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. "net/url"
  10. "os"
  11. "path"
  12. "path/filepath"
  13. "regexp"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "bitbucket.org/taruti/mimemagic"
  18. "github.com/dchest/uniuri"
  19. "github.com/zenazn/goji/web"
  20. )
  21. var fileBlacklist = map[string]bool{
  22. "favicon.ico": true,
  23. "index.htm": true,
  24. "index.html": true,
  25. "index.php": true,
  26. "robots.txt": true,
  27. "crossdomain.xml": true,
  28. }
  29. // Describes metadata directly from the user request
  30. type UploadRequest struct {
  31. src io.Reader
  32. filename string
  33. expiry time.Duration // Seconds until expiry, 0 = never
  34. randomBarename bool
  35. deletionKey string // Empty string if not defined
  36. }
  37. // Metadata associated with a file as it would actually be stored
  38. type Upload struct {
  39. Filename string // Final filename on disk
  40. Metadata Metadata
  41. }
  42. func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  43. if !strictReferrerCheck(r, Config.siteURL, []string{"Linx-Delete-Key", "Linx-Expiry", "Linx-Randomize"}) {
  44. badRequestHandler(c, w, r)
  45. return
  46. }
  47. upReq := UploadRequest{}
  48. uploadHeaderProcess(r, &upReq)
  49. contentType := r.Header.Get("Content-Type")
  50. if strings.HasPrefix(contentType, "multipart/form-data") {
  51. file, headers, err := r.FormFile("file")
  52. if err != nil {
  53. oopsHandler(c, w, r, RespHTML, "Could not upload file.")
  54. return
  55. }
  56. defer file.Close()
  57. r.ParseForm()
  58. if r.Form.Get("randomize") == "true" {
  59. upReq.randomBarename = true
  60. }
  61. upReq.expiry = parseExpiry(r.Form.Get("expires"))
  62. upReq.src = file
  63. upReq.filename = headers.Filename
  64. } else {
  65. if r.FormValue("content") == "" {
  66. oopsHandler(c, w, r, RespHTML, "Empty file")
  67. return
  68. }
  69. extension := r.FormValue("extension")
  70. if extension == "" {
  71. extension = "txt"
  72. }
  73. upReq.src = strings.NewReader(r.FormValue("content"))
  74. upReq.expiry = parseExpiry(r.FormValue("expires"))
  75. upReq.filename = r.FormValue("filename") + "." + extension
  76. }
  77. upload, err := processUpload(upReq)
  78. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  79. if err != nil {
  80. oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error())
  81. return
  82. }
  83. js := generateJSONresponse(upload)
  84. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  85. w.Write(js)
  86. } else {
  87. if err != nil {
  88. oopsHandler(c, w, r, RespHTML, "Could not upload file: "+err.Error())
  89. return
  90. }
  91. http.Redirect(w, r, "/"+upload.Filename, 301)
  92. }
  93. }
  94. func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  95. upReq := UploadRequest{}
  96. uploadHeaderProcess(r, &upReq)
  97. defer r.Body.Close()
  98. upReq.filename = c.URLParams["name"]
  99. upReq.src = r.Body
  100. upload, err := processUpload(upReq)
  101. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  102. if err != nil {
  103. oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error())
  104. return
  105. }
  106. js := generateJSONresponse(upload)
  107. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  108. w.Write(js)
  109. } else {
  110. if err != nil {
  111. oopsHandler(c, w, r, RespPLAIN, "Could not upload file: "+err.Error())
  112. return
  113. }
  114. fmt.Fprintf(w, Config.siteURL+upload.Filename)
  115. }
  116. }
  117. func uploadRemote(c web.C, w http.ResponseWriter, r *http.Request) {
  118. if r.FormValue("url") == "" {
  119. http.Redirect(w, r, "/", 301)
  120. return
  121. }
  122. upReq := UploadRequest{}
  123. grabUrl, _ := url.Parse(r.FormValue("url"))
  124. resp, err := http.Get(grabUrl.String())
  125. if err != nil {
  126. oopsHandler(c, w, r, RespAUTO, "Could not retrieve URL")
  127. return
  128. }
  129. upReq.filename = filepath.Base(grabUrl.Path)
  130. upReq.src = resp.Body
  131. upReq.deletionKey = r.FormValue("deletekey")
  132. upReq.expiry = parseExpiry(r.FormValue("expiry"))
  133. upload, err := processUpload(upReq)
  134. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  135. if err != nil {
  136. oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error())
  137. return
  138. }
  139. js := generateJSONresponse(upload)
  140. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  141. w.Write(js)
  142. } else {
  143. if err != nil {
  144. oopsHandler(c, w, r, RespHTML, "Could not upload file: "+err.Error())
  145. return
  146. }
  147. http.Redirect(w, r, "/"+upload.Filename, 301)
  148. }
  149. }
  150. func uploadHeaderProcess(r *http.Request, upReq *UploadRequest) {
  151. if r.Header.Get("Linx-Randomize") == "yes" {
  152. upReq.randomBarename = true
  153. }
  154. upReq.deletionKey = r.Header.Get("Linx-Delete-Key")
  155. // Get seconds until expiry. Non-integer responses never expire.
  156. expStr := r.Header.Get("Linx-Expiry")
  157. upReq.expiry = parseExpiry(expStr)
  158. }
  159. func processUpload(upReq UploadRequest) (upload Upload, err error) {
  160. // Determine the appropriate filename, then write to disk
  161. barename, extension := barePlusExt(upReq.filename)
  162. if upReq.randomBarename || len(barename) == 0 {
  163. barename = generateBarename()
  164. }
  165. var header []byte
  166. if len(extension) == 0 {
  167. // Pull the first 512 bytes off for use in MIME detection
  168. header = make([]byte, 512)
  169. n, _ := upReq.src.Read(header)
  170. if n == 0 {
  171. return upload, errors.New("Empty file")
  172. }
  173. header = header[:n]
  174. // Determine the type of file from header
  175. mimetype := mimemagic.Match("", header)
  176. // If the mime type is in our map, use that
  177. // otherwise just use "ext"
  178. if val, exists := mimeToExtension[mimetype]; exists {
  179. extension = val
  180. } else {
  181. extension = "ext"
  182. }
  183. }
  184. upload.Filename = strings.Join([]string{barename, extension}, ".")
  185. _, err = os.Stat(path.Join(Config.filesDir, upload.Filename))
  186. fileexists := err == nil
  187. // Check if the delete key matches, in which case overwrite
  188. if fileexists {
  189. metad, merr := metadataRead(upload.Filename)
  190. if merr == nil {
  191. if upReq.deletionKey == metad.DeleteKey {
  192. fileexists = false
  193. }
  194. }
  195. }
  196. for fileexists {
  197. counter, err := strconv.Atoi(string(barename[len(barename)-1]))
  198. if err != nil {
  199. barename = barename + "1"
  200. } else {
  201. barename = barename[:len(barename)-1] + strconv.Itoa(counter+1)
  202. }
  203. upload.Filename = strings.Join([]string{barename, extension}, ".")
  204. _, err = os.Stat(path.Join(Config.filesDir, upload.Filename))
  205. fileexists = err == nil
  206. }
  207. if fileBlacklist[strings.ToLower(upload.Filename)] {
  208. return upload, errors.New("Prohibited filename")
  209. }
  210. dst, err := os.Create(path.Join(Config.filesDir, upload.Filename))
  211. if err != nil {
  212. return
  213. }
  214. defer dst.Close()
  215. // Get the rest of the metadata needed for storage
  216. var expiry time.Time
  217. if upReq.expiry == 0 {
  218. expiry = neverExpire
  219. } else {
  220. expiry = time.Now().Add(upReq.expiry)
  221. }
  222. bytes, err := io.Copy(dst, io.MultiReader(bytes.NewReader(header), upReq.src))
  223. if bytes == 0 {
  224. os.Remove(path.Join(Config.filesDir, upload.Filename))
  225. return upload, errors.New("Empty file")
  226. } else if err != nil {
  227. os.Remove(path.Join(Config.filesDir, upload.Filename))
  228. return
  229. } else if bytes > Config.maxSize {
  230. os.Remove(path.Join(Config.filesDir, upload.Filename))
  231. return upload, errors.New("File too large")
  232. }
  233. upload.Metadata, err = generateMetadata(upload.Filename, expiry, upReq.deletionKey)
  234. if err != nil {
  235. os.Remove(path.Join(Config.filesDir, upload.Filename))
  236. os.Remove(path.Join(Config.metaDir, upload.Filename))
  237. return
  238. }
  239. err = metadataWrite(upload.Filename, &upload.Metadata)
  240. if err != nil {
  241. os.Remove(path.Join(Config.filesDir, upload.Filename))
  242. os.Remove(path.Join(Config.metaDir, upload.Filename))
  243. return
  244. }
  245. return
  246. }
  247. func generateBarename() string {
  248. return uniuri.NewLenChars(8, []byte("abcdefghijklmnopqrstuvwxyz0123456789"))
  249. }
  250. func generateJSONresponse(upload Upload) []byte {
  251. js, _ := json.Marshal(map[string]string{
  252. "url": Config.siteURL + upload.Filename,
  253. "filename": upload.Filename,
  254. "delete_key": upload.Metadata.DeleteKey,
  255. "expiry": strconv.FormatInt(upload.Metadata.Expiry.Unix(), 10),
  256. "size": strconv.FormatInt(upload.Metadata.Size, 10),
  257. "mimetype": upload.Metadata.Mimetype,
  258. "sha256sum": upload.Metadata.Sha256sum,
  259. })
  260. return js
  261. }
  262. var barePlusRe = regexp.MustCompile(`[^A-Za-z0-9\-]`)
  263. func barePlusExt(filename string) (barename, extension string) {
  264. filename = strings.TrimSpace(filename)
  265. filename = strings.ToLower(filename)
  266. extension = path.Ext(filename)
  267. barename = filename[:len(filename)-len(extension)]
  268. extension = barePlusRe.ReplaceAllString(extension, "")
  269. barename = barePlusRe.ReplaceAllString(barename, "")
  270. return
  271. }
  272. func parseExpiry(expStr string) time.Duration {
  273. if expStr == "" {
  274. return 0
  275. } else {
  276. expiry, err := strconv.ParseInt(expStr, 10, 64)
  277. if err != nil {
  278. return 0
  279. } else {
  280. return time.Duration(expiry) * time.Second
  281. }
  282. }
  283. }