Ejemplo n.º 1
0
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)
	}
}
Ejemplo n.º 2
0
func GenImageFilePath(img *models.Image, width int) string {
	var size string
	if width == 0 {
		size = "full"
	} else {
		size = utils.ToStr(width)
	}
	return GenImagePath(img) + size + img.GetExt()
}
Ejemplo n.º 3
0
func (this *QiniuUploadRouter) Post() {
	result := map[string]interface{}{
		"success": false,
	}

	defer func() {
		this.Data["json"] = &result
		this.ServeJson(this.Data)
	}()

	// check permition
	if !this.User.IsActive {
		return
	}

	// get file object
	file, handler, err := this.Req().FormFile("image")
	if err != nil {
		return
	}
	defer file.Close()

	t := time.Now()

	image := models.Image{}
	image.UserId = this.User.Id

	// get mime type
	mime := handler.Header.Get("Content-Type")

	// save and resize image
	if err := attachment.SaveImageToQiniu(&image, file, mime, handler.Filename, t, setting.QiniuPostBucket); err != nil {
		log.Error(err)
		return
	}

	result["link"] = image.LinkMiddle()
	result["success"] = true

}
Ejemplo n.º 4
0
func SaveImage(m *models.Image, r io.ReadSeeker, mime string, filename string, created time.Time) error {
	var ext string

	// test image mime type
	switch mime {
	case "image/jpeg":
		ext = ".jpg"

	case "image/png":
		ext = ".png"

	case "image/gif":
		ext = ".gif"

	default:
		ext = filepath.Ext(filename)
		switch ext {
		case ".jpg", ".png", ".gif":
		default:
			return fmt.Errorf("unsupport image format `%s`", filename)
		}
	}

	// decode image
	var img image.Image
	var err error
	switch ext {
	case ".jpg":
		m.Ext = 1
		img, err = jpeg.Decode(r)
	case ".png":
		m.Ext = 2
		img, err = png.Decode(r)
	case ".gif":
		m.Ext = 3
		img, err = gif.Decode(r)
	}

	if err != nil {
		return err
	}

	m.Width = img.Bounds().Dx()
	m.Height = img.Bounds().Dy()
	m.Created = created

	if err := models.Insert(m); err != nil || m.Id <= 0 {
		return err
	}

	m.Token = m.GetToken()
	if err := models.UpdateById(m.Id, m); err != nil {
		return err
	}

	path := GenImagePath(m)
	os.MkdirAll(path, 0755)

	fullPath := GenImageFilePath(m, 0)
	if _, err := r.Seek(0, 0); err != nil {
		return err
	}

	var file *os.File
	if f, err := os.OpenFile(fullPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644); err != nil {
		return err
	} else {
		file = f
	}

	if _, err := io.Copy(file, r); err != nil {
		os.RemoveAll(fullPath)
		return err
	}

	if ext != ".gif" {

		if m.Width > setting.ImageSizeSmall {
			if err := ImageResize(m, img, setting.ImageSizeSmall); err != nil {
				os.RemoveAll(fullPath)
				return err
			}
		}

		if m.Width > setting.ImageSizeMiddle {
			if err := ImageResize(m, img, setting.ImageSizeMiddle); err != nil {
				os.RemoveAll(fullPath)
				return err
			}
		}

	}

	return nil
}
Ejemplo n.º 5
0
func SaveImageToQiniu(m *models.Image, r goio.ReadSeeker, mime string, filename string, created time.Time, bucketName string) error {
	var ext string

	// check image mime type
	switch mime {
	case "image/jpeg":
		ext = ".jpg"

	case "image/png":
		ext = ".png"

	case "image/gif":
		ext = ".gif"

	default:
		ext = filepath.Ext(filename)
		switch ext {
		case ".jpg", ".png", ".gif":
		default:
			return fmt.Errorf("unsupport image format `%s`", filename)
		}
	}

	// decode image
	var img image.Image
	var err error
	switch ext {
	case ".jpg":
		m.Ext = 1
		img, err = jpeg.Decode(r)
	case ".png":
		m.Ext = 2
		img, err = png.Decode(r)
	case ".gif":
		m.Ext = 3
		img, err = gif.Decode(r)
	}

	if err != nil {
		return err
	}

	m.Width = img.Bounds().Dx()
	m.Height = img.Bounds().Dy()
	m.Created = created

	//save to database
	if err := models.Insert(m); err != nil || m.Id <= 0 {
		return err
	}

	m.Token = m.GetToken()
	if err := models.UpdateById(m.Id, m); err != nil {
		return err
	}

	var key = m.Token

	//reset reader pointer
	if _, err := r.Seek(0, 0); err != nil {
		return err
	}

	//save to qiniu
	var uptoken = utils.GetQiniuUptoken(bucketName)
	var putRet io.PutRet
	var putExtra = &io.PutExtra{
		MimeType: mime,
	}

	err = io.Put(nil, &putRet, uptoken, key, r, putExtra)
	if err != nil {
		return err
	}

	return nil
}