コード例 #1
0
ファイル: http_file.go プロジェクト: ylmbtm/nitori-go
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" {
		}
	}
}
コード例 #2
0
ファイル: fs.go プロジェクト: jmptrader/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)
		}
		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
}
コード例 #3
0
ファイル: fs.go プロジェクト: 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
}