Ejemplo n.º 1
0
func main() {
	flag.Parse()
	if *dirPath == "" {
		log.Fatal("Please specify directory path for uploaded files")
	}

	var s *Server = NewServer()
	s.Run()
	//For listing the available GridFS objects

	defer s.session.Close()

	var result *mgo.GridFile
	iter := s.gfs.Find(nil).Iter()

	for s.gfs.OpenNext(iter, &result) {
		fmt.Printf("Filename: %s\n", result.Name())
	}
	if iter.Err() != nil {
		panic(iter.Err())
	}
	//

	//http.HandleFunc("/", handleHome)
	/*addr := fmt.Sprintf(":%d", *port)
	if err := http.ListenAndServe(addr, nil); err != nil {
		log.Fatal("Failed to run server: ", err)
	}*/

}
Ejemplo n.º 2
0
Archivo: dtc.go Proyecto: kurze/dtc
func launchToServer(cIn chan StructFile) {
	var (
		err       error
		sf        StructFile
		dbF       *mgo.GridFile
		fi        os.FileInfo
		notClosed bool
		dbTime    time.Time
		fsTime    time.Time
	)

	for notClosed = true; notClosed; sf, notClosed = <-cIn {

		sf.Name = strings.Replace(sf.Name, dirName, "", 1)

		dbF = GetGridFile(sf.Name)
		if dbF == nil {
			log.Println("add : " + sf.Name)
			SetGridFile(sf)
		} else {

			dbTime = dbF.UploadDate()
			fi, err = sf.File.Stat()
			check(err)
			fsTime = fi.ModTime()

			if fsTime.After(dbTime) {
				log.Println("update : " + sf.Name)
				UpdateGridFile(sf)
			}
		}
	}
}
Ejemplo n.º 3
0
// ContentType returns the content type for the saved file indicated by key,
// or returns a FileNotFoundError if no such file exists.
func (s mongoStore) ContentType(key string) (ct string, err error) {
	var file mgo.GridFile
	q := s.gfs.Find(bson.M{"_id": key})
	err = q.One(&file)
	switch {
	case err == mgo.ErrNotFound:
		return ct, FileNotFoundError
	case err != nil:
		return
	}
	return file.ContentType(), nil
}
Ejemplo n.º 4
0
Archivo: dtc.go Proyecto: kurze/dtc
func GetFileData(name string) []byte {
	var (
		result []byte
		file   *mgo.GridFile
		err    error
	)

	file = GetGridFile(name)
	result = make([]byte, file.Size())
	_, err = file.Read(result)
	check(err)
	return result

}
Ejemplo n.º 5
0
Archivo: dtc.go Proyecto: kurze/dtc
func SetGridFile(sf StructFile) {
	var (
		dbFile *mgo.GridFile
		err    error
	)
	dbFile, err = gridFS.Create(sf.Name)
	check(err)

	io.Copy(dbFile, sf.File)
	check(err)
	err = dbFile.Close()
	check(err)

}
Ejemplo n.º 6
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" {
		}
	}
}
Ejemplo n.º 7
0
func (s *mongodbStorage) Cleanup(latest int) error {
	pids := s.getAllProblemIds()
	for _, v := range pids {
		s.doCleanup(v, latest)
	}

	pids = s.getAllGridPrefixes()
	iter := s.GridFS.Find(nil).Sort("filename").Iter()
	var f *mgo.GridFile
	for s.GridFS.OpenNext(iter, &f) {
		if !strings.HasPrefix(f.Name(), "problem/") {
			continue
		}
		if !hasAnyPrefix(f.Name(), pids) {
			fmt.Printf("Remove: %s\n", f.Name())
			s.GridFS.RemoveId(f.Id())
		}
	}
	return nil
}
Ejemplo n.º 8
0
// 查找文件
func FindAllFiles() {
	gfs := db.GridFS("fs")
	iter := gfs.Find(nil).Iter()
	var f *mgo.GridFile
	for gfs.OpenNext(iter, &f) {
		fmt.Printf("Filename: %s, %v\n", f.Name(), f.Id())
		b := make([]byte, 512)
		w, err := os.Create("upload/" + f.Name())
		if err != nil {
			continue
		}
		for {
			n, _ := f.Read(b)
			if n == 0 {
				break
			}
			w.Write(b[0:n])
		}
		defer w.Close()
	}
	if iter.Err() != nil {
		panic(iter.Err())
	}
}
Ejemplo n.º 9
0
Archivo: fs.go Proyecto: 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
}
Ejemplo n.º 10
0
func (s *S) TestGridFSOpenNext(c *C) {
	session, err := mgo.Dial("localhost:40011")
	c.Assert(err, IsNil)
	defer session.Close()

	db := session.DB("mydb")

	gfs := db.GridFS("fs")

	file, err := gfs.Create("myfile1.txt")
	c.Assert(err, IsNil)
	file.Write([]byte{'1'})
	file.Close()

	file, err = gfs.Create("myfile2.txt")
	c.Assert(err, IsNil)
	file.Write([]byte{'2'})
	file.Close()

	var f *mgo.GridFile
	var b [1]byte

	iter := gfs.Find(nil).Sort("-filename").Iter()

	ok := gfs.OpenNext(iter, &f)
	c.Assert(ok, Equals, true)
	c.Check(f.Name(), Equals, "myfile2.txt")

	_, err = f.Read(b[:])
	c.Assert(err, IsNil)
	c.Assert(string(b[:]), Equals, "2")

	ok = gfs.OpenNext(iter, &f)
	c.Assert(ok, Equals, true)
	c.Check(f.Name(), Equals, "myfile1.txt")

	_, err = f.Read(b[:])
	c.Assert(err, IsNil)
	c.Assert(string(b[:]), Equals, "1")

	ok = gfs.OpenNext(iter, &f)
	c.Assert(ok, Equals, false)
	c.Assert(iter.Close(), IsNil)
	c.Assert(f, IsNil)

	// Do it again with a more restrictive query to make sure
	// it's actually taken into account.
	iter = gfs.Find(bson.M{"filename": "myfile1.txt"}).Iter()

	ok = gfs.OpenNext(iter, &f)
	c.Assert(ok, Equals, true)
	c.Check(f.Name(), Equals, "myfile1.txt")

	ok = gfs.OpenNext(iter, &f)
	c.Assert(ok, Equals, false)
	c.Assert(iter.Close(), IsNil)
	c.Assert(f, IsNil)
}
Ejemplo n.º 11
0
func (s *mongodbStorage) Copy(localName, remoteName string, toRemote bool, checksum, moduleType string) (stat *contester_proto.FileStat, err error) {
	ec := tools.ErrorContext("mongodb.Copy")

	if toRemote {
		stat, err = tools.StatFile(localName, true)
		if err != nil {
			err = ec.NewError(err, "local.CalculateChecksum")
		}
		// If file doesn't exist then stat == nil.
		if err != nil || stat == nil {
			return
		}

		if checksum != "" && *stat.Checksum != checksum {
			return nil, ec.NewError(fmt.Errorf("Checksum mismatch, local %s != %s", stat.Checksum, checksum))
		}

		checksum = *stat.Checksum
	}

	var local *os.File
	if toRemote {
		local, err = os.Open(localName)
	} else {
		local, err = os.Create(localName)
	}

	if err != nil {
		return nil, ec.NewError(err, "local.Open")
	}
	defer local.Close()

	var remote *mgo.GridFile
	if toRemote {
		// Remove all files with the same remoteName.
		if err = s.GridFS.Remove(remoteName); err != nil {
			return nil, ec.NewError(err, "remote.Remove")
		}
		remote, err = s.GridFS.Create(remoteName)
	} else {
		remote, err = s.GridFS.Open(remoteName)
	}
	if err != nil {
		return nil, ec.NewError(err, "remote.Open")
	}
	defer remote.Close()

	var source io.ReadCloser
	if toRemote {
		source = local
	} else {
		source = remote
		var meta fileMetadata
		if err = remote.GetMeta(&meta); err != nil {
			return nil, ec.NewError(err, "remote.GetMeta")
		}
		if meta.CompressionType == "ZLIB" {
			source, err = zlib.NewReader(source)
			if err != nil {
				return nil, ec.NewError(err, "zlib.NewReader")
			}
		}
	}

	var destination io.WriteCloser
	if toRemote {
		destination = zlib.NewWriter(remote)
	} else {
		destination = local
	}

	size, err := io.Copy(destination, source)
	if err != nil {
		return nil, ec.NewError(err, "io.Copy")
	}

	if toRemote {
		var meta fileMetadata
		meta.OriginalSize = uint64(size)
		meta.CompressionType = "ZLIB"
		meta.Checksum = *stat.Checksum
		meta.ModuleType = moduleType

		remote.SetMeta(meta)
	}

	if err = destination.Close(); err != nil {
		return nil, ec.NewError(err, "destination.Close")
	}

	if err = source.Close(); err != nil {
		return nil, ec.NewError(err, "source.Close")
	}

	if !toRemote {
		stat, err = tools.StatFile(localName, true)
		if err != nil {
			return nil, ec.NewError(err, "StatFile")
		}
	}

	return stat, nil
}
Ejemplo n.º 12
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
}