当前位置: 首页>>代码示例>>Golang>>正文


Golang Context.File方法代码示例

本文整理汇总了Golang中github.com/labstack/echo.Context.File方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.File方法的具体用法?Golang Context.File怎么用?Golang Context.File使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/labstack/echo.Context的用法示例。


在下文中一共展示了Context.File方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: GetCrop

// GetCrop - Fetch and crop an image on the fly
func (handler *CropHandler) GetCrop(c echo.Context) error {

	// Get height and width from url parameters
	width, _ := strconv.Atoi(c.QueryParam("width"))
	height, _ := strconv.Atoi(c.QueryParam("height"))

	// Get file parameter
	filename := c.QueryParam("file")
	ext := GetExtension(filename)

	// If no file given, not much we can do
	if filename == "" {
		return c.JSON(http.StatusNotFound, &Error{
			Message: "No file given",
			Code:    http.StatusNotFound,
		})
	}

	// Fetch image from S3
	file, err := handler.bucket.Get("content/" + filename)

	// If file can't be fetched, throw a 404
	if err != nil {
		return c.JSON(http.StatusNotFound, &Error{
			Message: "File not found in bucket",
			Code:    http.StatusNotFound,
		})
	}

	// Decode image from bytes to `image.Image`
	img, err := handler.decoder.DecodeBytes(file)

	// Crop image
	cropped, err := handler.cropper.Crop(uint(width), uint(height), img, ext)

	if err != nil {
		return c.JSON(http.StatusInternalServerError, &Error{
			Message: err,
			Code:    http.StatusInternalServerError,
		})
	}

	// Return cropped image
	return c.File(string(cropped))
}
开发者ID:EwanValentine,项目名称:Ice,代码行数:46,代码来源:crop_handler.go

示例2: GetResize

// GetRezize - On the fly cropping and resizing.
// Crops images based on url parameters. For example...
// /resize?file=123.jpg&width=34&height=56
func (handler *ResizeHandler) GetResize(c echo.Context) error {

	// Set height and width
	width, _ := strconv.Atoi(c.QueryParam("width"))
	height, _ := strconv.Atoi(c.QueryParam("height"))

	// Get file
	filename := c.QueryParam("file")
	ext := GetExtension(filename)

	// If STILL no file given, well, what else can we do?
	if filename == "" {
		return c.JSON(http.StatusNotFound, &Error{
			Message: "No file given",
			Code:    http.StatusNotFound,
		})
	}

	// Get image
	file, err := handler.bucket.Get("content/" + filename)

	// If file can't be fetched, throw a 404
	if err != nil {
		return c.JSON(http.StatusNotFound, &Error{
			Message: err,
			Code:    http.StatusNotFound,
		})
	}

	// Decode image from bytes to `image.Image`
	img, err := handler.decoder.DecodeBytes(file)

	// Resize image
	resized, err := handler.resizer.Resize(uint(width), uint(height), img, ext)

	if err != nil {
		return c.JSON(http.StatusInternalServerError, &Error{
			Message: err,
			Code:    http.StatusInternalServerError,
		})
	}

	// Return resized image
	return c.File(string(resized))
}
开发者ID:EwanValentine,项目名称:Ice,代码行数:48,代码来源:resize_handler.go

示例3: Get

func Get(c *echo.Context) error {
	filepath := c.Query("path")
	showHidden := c.Query("show_hidden") == "true"
	create := c.Query("create") == "true"

	if len(filepath) < 1 {
		return c.JSON(
			http.StatusBadRequest,
			hash{
				"error": "Path not specified",
			},
		)
	}

	s, err := os.Stat(filepath)
	if err != nil {
		log.Error(err.(*os.PathError).Err.Error())
		m := err.(*os.PathError).Err.Error()
		if m == "no such file or directory" || m == "The system cannot find the file specified." {
			if create {
				err := os.MkdirAll(filepath, 0777)
				if err != nil {
					return err
				}
				s, err = os.Stat(filepath)
				if err != nil {
					return err
				}
			} else {
				return c.JSON(
					http.StatusNotFound,
					hash{
						"error": "no such file or directory",
					},
				)
			}
		} else {
			return err
		}
	}

	if s.Mode().IsDir() {
		f, err := os.Open(filepath)
		if err != nil {
			return err
		}
		defer f.Close()

		files, err := f.Readdir(-1)
		if err != nil {
			return err
		}

		rt := make([]*file, 0)

		for _, fi := range files {
			name := fi.Name()
			if !showHidden && isFileHidden(fi) {
				continue
			}

			fullpath := path.Join(filepath, name)
			id, err := loadFileId(fullpath)
			if err != nil {
				log.Errorf("Cannot retrieve file id for file: %s: %s", fullpath, err.Error())
				continue
			}

			f := &file{
				Id:      id,
				ModTime: fi.ModTime().Unix(),
				Name:    name,
				Size:    fi.Size(),
			}
			if fi.IsDir() {
				f.Type = "directory"
			} else {
				f.Type = "regular file"
			}
			rt = append(rt, f)
		}
		/*
		 * The Content-Length is not set is the buffer length is more than 2048
		 */
		b, err := jsonapi.Marshal(rt)
		if err != nil {
			log.Error(err)
			return err
		}

		r := c.Response()
		r.Header().Set("Content-Length", strconv.Itoa(len(b)))
		r.Header().Set("Content-Type", "application/json; charset=utf-8")
		r.Write(b)
		return nil
	}

	return c.File(
		filepath,
		s.Name(),
//.........这里部分代码省略.........
开发者ID:Nanocloud,项目名称:community,代码行数:101,代码来源:files.go

示例4: TranscodeGet

func TranscodeGet(c *echo.Context) error {
	return c.File("./public/views/transcode.html", "", false)
}
开发者ID:fzakaria,项目名称:transcoding,代码行数:3,代码来源:server.go


注:本文中的github.com/labstack/echo.Context.File方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。