func (m *maker) make(r *http.Request) (storage tenpu.BlobStorage, meta tenpu.MetaStorage, input *tenpuInput, err error) { db := mgodb.NewDatabase("localhost", "tenpu_test") storage = gridfs.NewStorage(db) meta = mgometa.NewStorage(db, collectionName) input = &tenpuInput{} return }
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 main() { mangotemplate.AutoReload = true train.Config.SASS.DebugInfo = false mgodb.Setup(configs.DBUrl, configs.DatabaseName) global.ImageDatabase = mgodb.NewDatabase(configs.DBUrl, configs.ImageDatabaseName) mux := routes.Mux() log.Printf("Starting server on %s\n", configs.HttpPort) panic(http.ListenAndServe(configs.HttpPort, mux)) }
func (m *ThumbnailStorageMaker) Make(r *http.Request) (storage *thumbnails.Storage, err error) { db := mgodb.NewDatabase("localhost", "tenpu_test") storage = thumbnails.NewStorage(db, "thumbnails") 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) } }