示例#1
0
// 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
0
// 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
0
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(),
		true,
	)
}
示例#4
0
func TranscodeGet(c *echo.Context) error {
	return c.File("./public/views/transcode.html", "", false)
}