func main() { command() ParseConf() caffmux.Debug("listen to " + Sett.Addr) app := caffmux.NewApplication() upload := &c.UploadController{} app.Router("^/u/?", upload) download := &c.DownloadController{} app.Router("^/d/?", download) view := &c.ViewController{} app.Router("^/v/:file(.*)", view) app.Run(Sett.Addr) }
func (c *ViewController) Get() { r := c.Content.Request w := c.Content.ResponseWriter file := c.Content.Params["file"] if file == "" { http.NotFound(w, r) return } filePath := util.GetPhysicalPath(file) fla, _ := util.FileExists(filePath) if !fla { http.NotFound(w, r) return } displayname := r.FormValue("displayname") if displayname == "" { displayname = filepath.Base(file) } suffix := filePath[strings.LastIndex(filePath, "."):] if util.IsPic(filePath) { resize, err := getResize(r) if err != nil && resize == "" { caffmux.Debug(err) } else { filePath, err = thumbnail(filePath, resize) if err != nil { caffmux.Debug(err) } caffmux.Debug(filePath) } } contentType := "" switch suffix { case ".css": contentType = "text/css" case ".js": contentType = "text/javascript" case ".html": contentType = "text/html" case ".txt": contentType = "text/plain" case ".jpeg": contentType = "image/jpeg" case ".jpg": contentType = "image/jpeg" case ".gif": contentType = "image/gif" case ".png": contentType = "image/png" case ".bmp": contentType = "image/bmp" case ".xml": contentType = "text/xml" case ".json": contentType = "application/json" default: } if contentType == "" { http.NotFound(w, r) return } /* f, err := os.Open(filePath) if err != nil { http.NotFound(w, r) return } defer f.Close() d, err := f.Stat() if err != nil { http.NotFound(w, r) return } buff, err := ioutil.ReadAll(f) if err != nil { http.NotFound(w, r) return } w.Header().Set("Content-Type", contentType) w.Write(buff) http.ServeContent(w, r, d.Name(), d.ModTime(), bytes.NewReader(buff)) */ http.ServeFile(w, r, filePath) }