github.com/louisevanderlith/droxolite@v1.20.2/mix/octet.go (about) 1 package mix 2 3 import ( 4 "bytes" 5 "io" 6 "strings" 7 ) 8 9 //Octet provides a io.Reader for serving data (octet-)streams 10 type octet struct { 11 filename string 12 mimetype string 13 headers map[string]string 14 data interface{} 15 } 16 17 func Octet(name string, data interface{}) Mixer { 18 if data == nil { 19 panic("data is nil") 20 } 21 22 r := &octet{ 23 data: data, 24 } 25 26 r.filename = name 27 ext := getExt(r.filename) 28 29 mimes := make(map[string]string) 30 mimes["js"] = "text/javascript" 31 mimes["css"] = "text/css" 32 mimes["html"] = "text/html" 33 mimes["ico"] = "image/x-icon" 34 mimes["font"] = "font/" + ext 35 mimes["jpeg"] = "image/jpeg" 36 mimes["jpg"] = "image/jpeg" 37 mimes["png"] = "image/png" 38 39 r.mimetype = mimes[ext] 40 41 return r 42 } 43 44 //Instead of assigning headers, returns headers that should be applied. 45 func (r *octet) Headers() map[string]string { 46 result := make(map[string]string) 47 48 result["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains" 49 result["Access-Control-Allow-Credentials"] = "true" 50 result["Server"] = "kettle" 51 result["X-Content-Type-Options"] = "nosniff" 52 53 result["Content-Description"] = "File Transfer" 54 result["Content-Transfer-Encoding"] = "binary" 55 result["Expires"] = "0" 56 result["Cache-Control"] = "must-revalidate" 57 result["Pragma"] = "public" 58 59 result["Content-Disposition"] = "attachment; filename=" + r.filename 60 result["Content-Type"] = r.mimetype 61 62 return result 63 } 64 65 //Reader configures the response for reading files. data can be either io.Reader or []byte 66 func (r *octet) Reader() io.Reader { 67 if readr, canRead := r.data.(io.Reader); canRead { 68 return readr 69 } 70 71 return bytes.NewReader(r.data.([]byte)) 72 } 73 74 func getName(path string) string { 75 slashIndex := strings.LastIndex(path, "/") 76 return path[slashIndex+1:] 77 } 78 79 func getExt(filename string) string { 80 dotIndex := strings.LastIndex(filename, ".") 81 return filename[dotIndex+1:] 82 } 83 84 /* 85 //ServeBinaryWithMIME is used to serve files such as images and documents. You must specify the MIME Type 86 func (ctx *Ctx) ServeBinaryStream(data io.Reader, filename, mimetype string) error { 87 88 } 89 90 //ServeBinary is used to serve files such as images and documents. 91 func (ctx *Ctx) ServeBinary(data []byte, filename string) error { 92 dataLen := len(data) 93 toTake := 512 94 95 if dataLen < 512 { 96 toTake = dataLen 97 } 98 99 mimetype := http.DetectContentType(data[:toTake]) 100 101 return ctx.ServeBinaryWithMIME(data, filename, mimetype) 102 } 103 104 //ServeBinaryWithMIME is used to serve files such as images and documents. You must specify the MIME Type 105 func (ctx *Ctx) ServeBinaryWithMIME(data []byte, filename, mimetype string) error { 106 ctx.SetHeader("Content-Description", "File Transfer") 107 ctx.SetHeader("Content-Disposition", "attachment; filename="+filename) 108 ctx.SetHeader("Content-Transfer-Encoding", "binary") 109 ctx.SetHeader("Expires", "0") 110 ctx.SetHeader("Cache-Control", "must-revalidate") 111 ctx.SetHeader("Pragma", "public") 112 113 ctx.SetHeader("Content-Type", mimetype) 114 115 _, err := ctx.WriteResponse(data) 116 117 return err 118 } 119 */