Esempio n. 1
0
func GridFSFileUpload(w http.ResponseWriter, rew *http.Request) {
	if auth := HttpAuthenticate(w, rew); auth {
		if rew.Method == "POST" {
			fmt.Println("METHOD WAS POST")
			GridFS := MongoDB.GridFS("fs")
			var file *mgo.GridFile
			var filename string

			rew.ParseMultipartForm(500000)
			formfileheaderarr := rew.MultipartForm.File["file"]
			formfileheader := formfileheaderarr[0]
			formfile, err := formfileheader.Open()

			//formfile, formfileheader, err := rew.FormFile("file")
			if err == nil {
				if rew.FormValue("filename") == "" {
					filename = formfileheader.Filename
				} else {
					filename = rew.FormValue("filename")
				}
				fmt.Println(filename)

				file, err = GridFS.Create(filename)

				if err == nil {
					_, err = io.Copy(file, formfile)
					if err == nil {
						file.SetContentType(formfileheader.Header.Get("Content-Type"))
						err = file.Close()
						if err == nil {
							Template(TemplateFileUploaded).Execute(w, "Grid/"+filename)
						}
					}
				}

			}

			if err != nil {
				io.WriteString(w, "Error occured: "+err.Error())
			}

			//GridFS.Create(
			//io.Copy()
			//rew.FormFile("file").
		} else if rew.Method == "GET" {
		}
	}
}
Esempio n. 2
0
func (s *Storage) Put(filename string, contentType string, body io.Reader, attachment *tenpu.Attachment) (err error) {
	var f *mgo.GridFile
	s.database.DatabaseDo(func(db *mgo.Database) {
		f, err = db.GridFS("fs").Create(filename)
		defer f.Close()
		if err != nil {
			panic(err)
		}
		if attachment.Id != "" {
			f.SetId(bson.ObjectIdHex(attachment.Id))
		}
		f.SetContentType(contentType)
		_, err = io.Copy(f, body)
	})

	if err == io.ErrUnexpectedEOF {
		attId := f.Id().(bson.ObjectId)
		s.Delete(attId.Hex())
		return
	}

	if attachment.Id == "" {
		attachment.Id = f.Id().(bson.ObjectId).Hex()
		attachment.ContentLength = f.Size()
		attachment.ContentType = f.ContentType()
		attachment.Filename = f.Name()
		attachment.MD5 = f.MD5()
		if attachment.IsImage() {
			s.database.DatabaseDo(func(db *mgo.Database) {
				f, err := db.GridFS("fs").OpenId(bson.ObjectIdHex(attachment.Id))
				if err == nil {
					config, _, err := image.DecodeConfig(f)
					f.Close()
					if err == nil {
						attachment.Width = config.Width
						attachment.Height = config.Height
					}
				}
			})
		}
	}

	return
}
Esempio n. 3
0
File: fs.go Progetto: athom/tenpu
func (s *Storage) Put(filename string, contentType string, body io.Reader, attachment *tenpu.Attachment) (err error) {
	var f *mgo.GridFile

	s.database.DatabaseDo(func(db *mgo.Database) {
		f, err = db.GridFS("fs").Create(filename)
		defer f.Close()
		if err != nil {
			panic(err)
		}
		f.SetContentType(contentType)
		io.Copy(f, body)

	})

	attachment.Id = f.Id().(bson.ObjectId).Hex()
	attachment.ContentLength = f.Size()
	attachment.ContentType = f.ContentType()
	attachment.Filename = f.Name()
	attachment.MD5 = f.MD5()
	return
}