示例#1
0
文件: upload.go 项目: trigrass2/wego
func Image(ctx *tango.Context) {
	token := ctx.Params().Get(":path")

	// split token and file ext
	var filePath string
	if i := strings.IndexRune(token, '.'); i == -1 {
		return
	} else {
		filePath = token[i+1:]
		token = token[:i]
	}

	// decode token to file path
	var image models.Image
	if err := image.DecodeToken(token); err != nil {
		log.Info(err)
		return
	}

	// file real path
	filePath = attachment.GenImagePath(&image) + filePath

	// if x-send on then set header and http status
	// fall back use proxy serve file
	if setting.ImageXSend {
		//ext := filepath.Ext(filePath)
		// TODO:
		//ctx.Header().ContentType(ext)
		ctx.Header().Set(setting.ImageXSendHeader, "/"+filePath)
		ctx.WriteHeader(http.StatusOK)
	} else {
		// direct serve file use go
		ctx.ServeFile(filePath)
	}
}
示例#2
0
func QiniuImage(ctx *tango.Context) {
	var imageName = ctx.Params().Get(":path")
	var imageKey string
	var imageSize string
	if i := strings.IndexRune(imageName, '.'); i == -1 {
		return
	} else {
		imageSize = imageName[i+1:]
		if j := strings.IndexRune(imageSize, '.'); j == -1 {
			imageSize = "full"
		} else {
			imageSize = imageSize[:j]
		}
		imageKey = imageName[:i]
	}

	var image = models.Image{
		Token: imageKey,
	}
	err := models.GetByExample(&image)
	if err != nil {
		return
	}
	var imageWidth = image.Width
	var imageHeight = image.Height
	var zoomRatio = setting.ImageSizeMiddle / imageWidth
	if imageWidth > setting.ImageSizeMiddle {
		imageWidth = setting.ImageSizeMiddle
	}
	imageHeight *= zoomRatio

	var imageUrl = utils.GetQiniuPublicDownloadUrl(setting.QiniuPostDomain, imageKey)
	var zoomImageUrl = utils.GetQiniuZoomViewUrl(imageUrl, imageWidth, imageHeight)
	resp, err := http.Get(zoomImageUrl)
	if err != nil {
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	ctx.ResponseWriter.Write(body)
}