本文整理汇总了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))
}
示例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))
}
示例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(),
//.........这里部分代码省略.........
示例4: TranscodeGet
func TranscodeGet(c *echo.Context) error {
return c.File("./public/views/transcode.html", "", false)
}