func (im *Images) DELETE(c *kocha.Context) kocha.Result { // FIXME: auto-generated by kocha //iid := c.Params.Get("imageId") return kocha.Render(c) }
func (im *Images) GET(c *kocha.Context) kocha.Result { // FIXME: auto-generated by kocha return kocha.Render(c) }
func (im *Images) POST(c *kocha.Context) kocha.Result { //リクエスト容量が大きかったら最初から弾く if c.Request.ContentLength > MaxBytes { return kocha.RenderError(c, http.StatusBadRequest, "Size of your reqest is too large") } //FormDataの取得 f, h, err := c.Request.FormFile("image") if err != nil { return kocha.RenderError(c, http.StatusBadRequest, "Request has not been accepted") } defer f.Close() //ヘッダ情報からの形式取得 var imgf string ct := h.Header["Content-Type"][0] switch ct { case JpegCT: imgf = "jpeg" case GifCT: imgf = "gif" case PngCT: imgf = "png" default: return kocha.RenderError(c, http.StatusBadRequest, "Image format must be either jpeg, gif or png") } //画像のデコードとフォーマットバリデーション img, fm, err := image.Decode(f) if err != nil { return kocha.RenderError(c, http.StatusBadRequest, "Error has occured when decoding the reqested image") } else if fm != imgf { return kocha.RenderError(c, http.StatusBadRequest, "The format of actual image is not the same as the header") } //指定のDrawerを使って描画しつつエンコード b := new(bytes.Buffer) switch ct { case JpegCT: err = encodeJPEG(&img, b, drawLGTMWithRGBA) case GifCT: err = encodeGIF(&f, b, drawLGTMWithPaletted) case PngCT: err = encodePNG(&img, b, drawLGTMWithRGBA) } if err != nil { return kocha.RenderError(c, http.StatusBadRequest, "Error has occured when encoding the requested image") } else if b.Len() > MaxBytes { return kocha.RenderError(c, http.StatusBadRequest, "Actual size of requested image is too large") } // 配置用のパス決めてS3に配置 p := uuid.New() //ver4 if err = aws.LgtmBucket.Put(p, b.Bytes(), ct, s3.PublicRead); err != nil { return kocha.RenderError(c, http.StatusInternalServerError, "An error has occured when uploading image: "+p) } // TODO DBにIDを保存 return kocha.Render(c, kocha.Data{"imagePath": p}) }
func (ro *Root) GET(c *kocha.Context) kocha.Result { return kocha.Render(c) }