Example #1
0
// @router /test/file/download [get]
func (c *TestController) Download() {
	filename := c.GetString("name")
	if filename == "" {
		http.Error(c.Ctx.ResponseWriter, "Not Found", 404)
		return
	}
	filename = fileDir + filename
	_, err := os.Stat(filename)
	if os.IsNotExist(err) {
		http.Error(c.Ctx.ResponseWriter, "Not Found", 404)
		return
	}

	ctype := mime.TypeByExtension(filepath.Ext(filename))
	if ctype == "" {
		ctype = "application/oct-stream"
	}
	en := context.ParseEncoding(c.Ctx.Request)
	if en != "" {
		c.Ctx.ResponseWriter.Header().Set("Content-Encoding", en)
	}
	c.Ctx.ResponseWriter.Header().Set("Content-Type", ctype)

	f, err := os.Open(filename)
	context.WriteFile(en, c.Ctx.ResponseWriter, f)
}
Example #2
0
func serverStaticRouter(ctx *context.Context) {
	if ctx.Input.Method() != "GET" && ctx.Input.Method() != "HEAD" {
		return
	}

	forbidden, filePath, fileInfo, err := lookupFile(ctx)
	if err == errNotStaticRequest {
		return
	}

	if forbidden {
		exception("403", ctx)
		return
	}

	if filePath == "" || fileInfo == nil {
		if BConfig.RunMode == DEV {
			Warn("Can't find/open the file:", filePath, err)
		}
		http.NotFound(ctx.ResponseWriter, ctx.Request)
		return
	}
	if fileInfo.IsDir() {
		//serveFile will list dir
		http.ServeFile(ctx.ResponseWriter, ctx.Request, filePath)
		return
	}

	var enableCompress = BConfig.EnableGzip && isStaticCompress(filePath)
	var acceptEncoding string
	if enableCompress {
		acceptEncoding = context.ParseEncoding(ctx.Request)
	}
	b, n, sch, err := openFile(filePath, fileInfo, acceptEncoding)
	if err != nil {
		if BConfig.RunMode == DEV {
			Warn("Can't compress the file:", filePath, err)
		}
		http.NotFound(ctx.ResponseWriter, ctx.Request)
		return
	}

	if b {
		ctx.Output.Header("Content-Encoding", n)
	} else {
		ctx.Output.Header("Content-Length", strconv.FormatInt(sch.size, 10))
	}

	http.ServeContent(ctx.ResponseWriter, ctx.Request, filePath, sch.modTime, sch)
	return

}
Example #3
0
// @router /api/file/upload [post]
func (c *FileController) Upload() {

	result := map[string]interface{}{}

	defer func() {
		c.Data["json"] = &result
		c.ServeJSON()
	}()

	en := context.ParseEncoding(c.Ctx.Request)
	if en != "" {
		c.Ctx.ResponseWriter.Header().Set("Content-Encoding", en)
	}
	// todo: ?既然c.ServeJSON 已经明确设置该头了,为什么没有生效?
	c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json; charset=utf-8")
	f, fh, err := c.GetFile("file")
	if err != nil {
		c.Ctx.ResponseWriter.WriteHeader(400)
		result["retcode"] = 4000001
		result["msg"] = "no file to upload"
		return
	}
	defer f.Close()
	//ReadForm 返回的时候回删除可能使用到的临时文件的,GetFile返回的是文件的句柄,所以不需要显式删除临时文件,文件小于10MB不会用到临时文件

	bucket, err := oss.NewBucket()
	if err != nil {
		msg := err.Error()
		http.Error(c.Ctx.ResponseWriter, msg, 500)
		result["retcode"] = 5000001
		result["msg"] = msg
		return
	}

	err = bucket.PutObject(fh.Filename, f)
	if err != nil {
		msg := "upload file fail: " + err.Error()
		http.Error(c.Ctx.ResponseWriter, msg, 500)
		result["retcode"] = 5000002
		result["msg"] = msg
		return
	}

	endpoint := beego.AppConfig.String("oss.endpoint")
	url := "http://" + bucket.BucketName + endpoint + "/" + fh.Filename
	c.Ctx.ResponseWriter.WriteHeader(http.StatusCreated)
	result["retcode"] = 2000000
	result["msg"] = "created"
	result["body"] = map[string]string{"path": url}

}
Example #4
0
// @router /api/file/download [get]
func (c *FileController) Download() {

	bucket, err := oss.NewBucket()
	if err != nil {
		msg := err.Error()
		http.Error(c.Ctx.ResponseWriter, msg, 500)
		return
	}

	filename := c.GetString("name")
	if filename == "" {
		http.Error(c.Ctx.ResponseWriter, "need param name", 400)
		return
	}

	//	object, err := url.ParseRequestURI(file)
	//	if err != nil {
	//		http.Error(c.Ctx.ResponseWriter, "param name invalid", 400)
	//		return
	//	}
	//	filename := filepath.Base(file)
	r, err := bucket.GetObject(filename)
	if err != nil {
		beego.Debug(err)
		http.Error(c.Ctx.ResponseWriter, "Not Found", 404)
		return
	}

	ctype := mime.TypeByExtension(filepath.Ext(filename))
	if ctype == "" {
		ctype = "application/oct-stream"
	}
	en := context.ParseEncoding(c.Ctx.Request)
	if en != "" {
		c.Ctx.ResponseWriter.Header().Set("Content-Encoding", en)
	}
	c.Ctx.ResponseWriter.Header().Set("Content-Type", ctype)

	// todo: 这里需要优化
	content, err := ioutil.ReadAll(r)
	if err != nil {
		beego.Debug(err)
		http.Error(c.Ctx.ResponseWriter, "very said", 500)
		return
	}
	// todo: 这里应该提供一个支持ioReader的方法
	context.WriteBody(en, c.Ctx.ResponseWriter, content)
}