func TestMakeClearUploader(t *testing.T) { mgodb.Setup("localhost", "tenpu_test") db := mgodb.NewDatabase("localhost", "tenpu_test") mgodb.CollectionDo(tenpu.CollectionName, func(c *mgo.Collection) { c.DropCollection() }) st := gridfs.NewStorage() http.HandleFunc("/upload_avatar", tenpu.MakeClearUploader("OwnerId", "posts", st)) http.HandleFunc("/load_avatar", tenpu.MakeFileLoader("id", st)) ts := httptest.NewServer(http.DefaultServeMux) defer ts.Close() //upload attachment repeatly req, _ := http.NewRequest("POST", ts.URL+"/upload_avatar", strings.NewReader(singlePartContent)) req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj") res, err := http.DefaultClient.Do(req) req, _ = http.NewRequest("POST", ts.URL+"/upload_avatar", strings.NewReader(singlePartContent)) req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj") res, err = http.DefaultClient.Do(req) req, _ = http.NewRequest("POST", ts.URL+"/upload_avatar", strings.NewReader(singlePartContent)) req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj") res, err = http.DefaultClient.Do(req) if err != nil { panic(err) } b, _ := ioutil.ReadAll(res.Body) strb := string(b) if !strings.Contains(strb, "4facead362911fa23c000002") { t.Errorf("%+v", strb) } dbc := tenpu.DatabaseClient{Database: db} atts := dbc.Attachments("4facead362911fa23c000002") if len(atts) != 1 { t.Errorf("%+v", atts[0]) } res, err = http.Get(ts.URL + "/load_avatar?id=" + atts[0].Id) if err != nil { panic(err) } b, _ = ioutil.ReadAll(res.Body) strb = string(b) if strb != "the file content c\n" { t.Errorf("%+v", strb) } }
func MakeLoader(config *Configuration) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id := r.URL.Query().Get(config.IdentifierName) if id == "" { http.NotFound(w, r) return } thumbName := r.URL.Query().Get(config.ThumbnailParamName) if thumbName == "" { http.NotFound(w, r) return } var spec *ThumbnailSpec for _, ts := range config.ThumbnailSpecs { if ts.Name == thumbName { spec = ts break } } if spec == nil { log.Println("tenpu/thumbnails: Can't find thumbnail spec %+v in %+v", thumbName, config.ThumbnailSpecs) http.NotFound(w, r) return } var att *tenpu.Attachment config.Storage.Database().FindOne(tenpu.CollectionName, bson.M{"_id": id}, &att) if att == nil { http.NotFound(w, r) return } var thumb *Thumbnail config.Storage.Find(CollectionName, bson.M{"parentid": id, "name": thumbName}, &thumb) if thumb == nil { var buf bytes.Buffer config.Storage.Copy(att, &buf) thumbAtt := &tenpu.Attachment{} body, width, height, err := resizeThumbnail(&buf, spec) if err != nil { log.Printf("tenpu/thumbnails: %+v", err) http.NotFound(w, r) return } config.Storage.Put(att.Filename, att.ContentType, body, thumbAtt) config.Storage.Database().Save(tenpu.CollectionName, thumbAtt) thumb = &Thumbnail{ Name: thumbName, ParentId: id, BodyId: thumbAtt.Id, Width: int64(width), Height: int64(height), } config.Storage.Database().Save(CollectionName, thumb) } dbc := tenpu.DatabaseClient{Database: config.Storage.Database()} thumbAttachment := dbc.AttachmentById(thumb.BodyId) if thumbAttachment == nil { log.Printf("tenpu/thumbnails: Can't find body attachment by %+v", thumb) http.NotFound(w, r) return } w.Header().Set("Content-Type", thumbAttachment.ContentType) w.Header().Set("Content-Length", fmt.Sprintf("%d", thumbAttachment.ContentLength)) err := config.Storage.Copy(thumbAttachment, w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } return } }
func TestThumbnailLoader(t *testing.T) { mgodb.Setup("localhost", "tenpu_test") db := mgodb.NewDatabase("localhost", "tenpu_test") mgodb.DropCollections(tenpu.CollectionName, thumbnails.CollectionName) st := gridfs.NewStorage() http.HandleFunc("/thumbpostupload", tenpu.MakeUploader("OwnerId", "posts", st)) http.HandleFunc("/thumbload", thumbnails.MakeLoader(&thumbnails.Configuration{ IdentifierName: "id", ThumbnailParamName: "thumb", Storage: gridfs.NewStorage(), ThumbnailSpecs: []*thumbnails.ThumbnailSpec{ {Name: "icon", Width: 100}, }, })) ts := httptest.NewServer(http.DefaultServeMux) defer ts.Close() var err error s := integrationtest.NewSession() res := integrationtest.Must(s.PostMultipart(ts.URL+"/thumbpostupload", func(w *multipart.Writer) { w.WriteField("OwnerId", "my12345") p1, err := w.CreateFormFile("t", "t.jpg") if err != nil { panic(err) } tf, err1 := os.Open("t.jpg") if err1 != nil { panic(err1) } defer tf.Close() io.Copy(p1, tf) })) b, _ := ioutil.ReadAll(res.Body) strb := string(b) if !strings.Contains(strb, "my12345") { t.Errorf("%+v", strb) } dbc := tenpu.DatabaseClient{Database: db} atts := dbc.Attachments("my12345") if len(atts) != 1 { t.Errorf("%+v", atts) } res, err = http.Get(ts.URL + fmt.Sprintf("/thumbload?id=%s&thumb=icon", atts[0].Id)) if err != nil { panic(err) } f, err2 := os.OpenFile("thumbGenerated.jpg", os.O_CREATE|os.O_RDWR, 0666) if err2 != nil { panic(err2) } defer f.Close() io.Copy(f, res.Body) http.Get(ts.URL + fmt.Sprintf("/thumbload?id=%s&thumb=icon", atts[0].Id)) var thumbs []thumbnails.Thumbnail mgodb.FindAll(thumbnails.CollectionName, bson.M{}, &thumbs) if len(thumbs) != 1 { t.Errorf("%+v", thumbs) } }